Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7-mong3 #26

Merged
merged 1 commit into from
Mar 18, 2024
Merged

7-mong3 #26

merged 1 commit into from
Mar 18, 2024

Conversation

mong3125
Copy link
Collaborator

@mong3125 mong3125 commented Mar 4, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ 13023๋ฒˆ ABCDE

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

3์‹œ๊ฐ„ ๋„˜๊ฒŒ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ์•„์ด๋””์–ด๋‚˜ ์ „์ฒด์ ์ธ ์ฝ”๋“œ๋Š” 1์‹œ๊ฐ„ ๋‚ด๋กœ ํ•ด๊ฒฐ์„ ํ–ˆ๋Š”๋ฐ..

์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ณ  ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ๋ฌธ์ œ๋ ๊ฒŒ ์—†๋Š”๊ฑฐ ๊ฐ™์€๋ฐ ๋ญ”์ง€ ๊ณ ๋ฏผํ•˜๊ณ  ์‹œ๊ฐ„์„ ์˜ค๋ž˜ ์“ฐ๋‹ค๊ฐ€ ๊นจ๋‹ฌ์•˜๋„ค์š”. ์•„๋‹ˆ ๊นจ๋‹ฌ์€๊ฑด ์•„๋‹ˆ๊ณ  ์–ด์ฐŒ์ €์ฐŒ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.
image
ํ”์ ์ž…๋‹ˆ๋‹ค..

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์นœ๊ตฌ๋Š” ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๋ผ์„œ ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๋กœ ๋ฌถ๊ณ  DFS๋กœ ๊นŠ์ด๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฏธ ํ•œ๋ฒˆ ๋ฐฉ๋ฌธํ•œ ์นœ๊ตฌ๋Š” ๋‹ค์‹œ ๋ฐฉ๋ฌธํ•˜๊ณ  ๊นŠ์ด๋ฅผ ๋”ํ•˜๋ฉด ์•ˆ๋˜์„œ visited๋ผ๋Š” ๋ฐฐ์—ด๋กœ ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•ด์คฌ์–ด์š”.

๋ฐฉ๋ฌธ์—ฌ๋ถ€ = true
for ๋ฐ˜๋ณต
    dfs()
๋ฐฉ๋ฌธ์—ฌ๋ถ€ = false

์ด๋ ‡๊ฒŒ ๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.

์‹ค์ œ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
image

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚œ ์ด์œ ๋Š” ์นœ๊ตฌ๋ฅผ ์ถ”๊ฐ€ํ• ๋•Œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  ๋ฐฐ์—ด๋กœ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

List<Integer>[] friends;
for (int i : friends[personId]) {
    if (!visited[i]) {
        dfs(i, depth + 1);
    }
}

์ด ๋ถ€๋ถ„์—์„œ ๋ณด๋ฉด for (int i : friends[personId]) ์ด python ์œผ๋กœ ๋”ฐ์ง€์ž๋ฉด for i in friends ์ธ๋ฐ์š”

์ „ ์ด๊ฑธ

boolean[][] isFreind;
for (int i = 0; i < N; i++) {
    if (isFreind[personId][i] && !visited[i]) {
       dfs(i, depth + 1);
    }
}

์ด๋ ‡๊ฒŒ ๋ฐฐ์—ด๋กœ ์ฒดํฌํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

  • ์ด๊ฒŒ ํšจ์œจ์ ์ด๋ž€๊ฑด ์•Œ์ง€๋งŒ ์•„์ง ์ดํ•ด๊ฐ€ ์•ˆ๋˜๊ธด ํ•˜๋„ค์š”. ์ด๊ฒƒ๋„ ์•ˆ๋˜์•ผ๋˜๋Š”๊ฒŒ ์•„๋‹Œ๊ฐ€์š”??
  • ๊ฐ€๋ น ๋ชจ๋‘๊ฐ€ ์„œ๋กœ ์นœ๊ตฌ์ธ ๊ฒฝ์šฐ๋ผ๋ฉด ๊ฒฐ๊ตญ ๋˜‘๊ฐ™์€๊ฒŒ ์•„๋‹Œ๊ฐ€...? ํ•˜๋Š”.. ๊ทธ๋ž˜์„œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๋ถ€์กฑ์ธ๊ฐ€? ์‹ถ์€ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค.
  • ์˜๋ฌธ์„ ์™„์ „ํžˆ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•œ์ฑ„๋กœ ์‹œ๊ฐ„์ด ์—†์–ด์„œ ์ œ์ถœํ•ด๋ด…๋‹ˆ๋‹ค.. ํ˜น์‹œ ์•„์‹œ๋Š”๋ถ„ ๊ณ„์‹œ๋ฉด ์•Œ๋ ค์ฃผ์„ธ์š”!

Copy link
Member

@kjs254 kjs254 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ทธ๋ž˜ํ”„ ์™„์ „ ํƒ์ƒ‰ ๋ฌธ์ œ๋กœ ํŒ๋‹จ๋˜์–ด์„œ ๋ฌธ์ œ ์ž์ฒด๋Š” DFS๋ฅผ ์•Œ๋ฉด ํฌ๊ฒŒ ์–ด๋ ค์›Œ ๋ณด์ด์ง€ ์•Š๋Š”๋ฐ ์ œ๊ฐ€ JAVA ์–ธ์–ด๋ฅผ ์ž˜ ๋ชฐ๋ผ์„œ ์งˆ๋ฌธ๋“œ๋ฆฝ๋‹ˆ๋‹ค..

์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ์‹  friends ๋ฆฌ์ŠคํŠธ์™€ visited ๋ฆฌ์ŠคํŠธ๊ฐ€ ์–ด๋–ค ๊ฐ’์ด๋ฉฐ ์ด ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ–ˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค

Comment on lines +53 to +57
visited[personId] = true;
for (int i : friends[personId]) {
if (!visited[i]) {
dfs(i, depth + 1);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visited ๋ฆฌ์ŠคํŠธ์™€ friends ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ •ํ™•ํžˆ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visited๋Š” DFS์—์„œ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ ๋ฐฐ์—ด์ด๋ฉฐ n๋ฒˆ์งธ ์š”์†Œ์˜ boolean ๊ฐ’์€ n๋ฒˆ์งธ ๋…ธ๋“œ์˜ ๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

friends๋Š” ๊ทธ๋ž˜ํ”„์—์„œ ์—ฃ์ง€๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜์˜€์œผ๋ฉฐ ๋ฆฌ์ŠคํŠธ์˜ ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. n๋ฒˆ์งธ ์š”์†Œ์˜ ๋ฆฌ์ŠคํŠธ๋Š” n๋ฒˆ์งธ ๋…ธ๋“œ์˜ ์—ฃ์ง€๋“ค์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์‚ฌ์‹ค ์ €๋„ ์ด ๋ฌธ์ œ์—์„œ ์–ด๋– ํ•œ ์นœ๊ตฌ๊ด€๊ณ„๋ฅผ ์›ํ•˜๋Š” ๊ฒƒ์ธ์ง€ ์ž˜์€ ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜,
์›ํ•˜๋Š” ๊ฒƒ์€, 5๋ช…์ด ๊ฑด๋„ˆ๊ฑด๋„ˆ ์„œ๋กœ ์•„๋Š” ๊ด€๊ณ„๋ฅผ ์›ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”.

์ฒ˜์Œ ๋ดค์„ ๋•Œ์— ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ ๋ฌธ์ œ์™€ ์œ ์‚ฌํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด, ์ด ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์‹œ๋ฉฐ ์ดํ•ด๋ฅผ ๋†’์ด์‹œ๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.

์‚ฌ์‹ค ์ €๋„ ์ด ๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•ด์•ผํ• ์ง€์— ๋Œ€ํ•˜์—ฌ๋Š” ์กฐ๊ธˆ ๊ณ ๋ฏผ์ด ๋˜๊ธด ํ•ฉ๋‹ˆ๋‹ค๋งŒ, ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ๋“ฑ์„ ์ด์šฉํ•ด์„œ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™๊ธด ํ•˜๋„ค์š”.

์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ ์ด์œ ๋Š”.,..2์ฐจ์› ๋ฐฐ์—ด ๋•Œ๋ฌธ์ผ๊นŒ์š”? ๋˜๊ฒŒ ๊ถ๊ธˆํ•˜๊ธด ํ•˜๋„ค์š”.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ณด์ž๋งˆ์ž DFS๋ผ๋Š” ์ง๊ฐ์ด ์™”๋Š”๋ฐ ์ž˜ ๊ตฌํ˜„ ํ•ด์ฃผ์…จ๋„ค์š”.
์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚œ ๊ฑด ์•„๋งˆ ์กฐ๊ฑด๋ฌธ ํ•˜๋‚˜์— ์กฐ๊ฑด ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์„œ ์—ฐ์‚ฐ๋Ÿ‰ ์ฐจ์ด ๋•Œ๋ฌธ์ด ์•„๋‹๊นŒ ์กฐ์‹ฌ์Šค๋Ÿฝ๊ฒŒ ์˜ˆ์ƒํ•ด๋ด…๋‹ˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants