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

14-avocado 13 #67

Merged
merged 2 commits into from
Feb 20, 2024
Merged

14-avocado 13 #67

merged 2 commits into from
Feb 20, 2024

Conversation

avocado-13
Copy link
Member

@avocado-13 avocado-13 commented Feb 15, 2024

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

๋ฐฑ์ค€ | DFS์™€ BFS

๋ฌธ์ œ ์„ค๋ช…

๊ทธ๋ž˜ํ”„๋ฅผ DFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ์™€ BFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ข…๋ฃŒํ•œ๋‹ค. ์ •์  ๋ฒˆํ˜ธ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€์ด๋‹ค

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N(1 โ‰ค N โ‰ค 1,000), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M(1 โ‰ค M โ‰ค 10,000), ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ V๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์–ด๋–ค ๋‘ ์ •์  ์‚ฌ์ด์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ„์„ ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๊ฐ„์„ ์€ ์–‘๋ฐฉํ–ฅ์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— DFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ, ๊ทธ ๋‹ค์Œ ์ค„์—๋Š” BFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. V๋ถ€ํ„ฐ ๋ฐฉ๋ฌธ๋œ ์ ์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

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

1์‹œ๊ฐ„

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

  1. ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ ๋ฐ ์ž…๋ ฅ: ์ฝ”๋“œ์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ n, m, **start**๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ณ , ๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ **graph**๋Š” ๊ฐ ์ •์ ์—์„œ ์ด์–ด์ง„ ๋‹ค๋ฅธ ์ •์ ๋“ค์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฆฌ์ŠคํŠธ์ด๋‹ค. ๊ฐ ์ •์ ๋งˆ๋‹ค ์ด์–ด์ง„ ๋‹ค๋ฅธ ์ •์ ๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•œ๋‹ค.
  2. BFS ํ•จ์ˆ˜: BFS๋Š” ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•จ์ˆ˜๋Š” ์‹œ์ž‘ ๋…ธ๋“œ๋ถ€ํ„ฐ ์ด์–ด์ง„ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•˜๊ณ , ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ํƒ์ƒ‰ํ•œ๋‹ค. ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋Š” visited ๋ฆฌ์ŠคํŠธ์— True๋กœ ํ‘œ์‹œํ•œ๋‹ค.
  3. DFS ํ•จ์ˆ˜: DFS๋Š” ์Šคํƒ์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•จ์ˆ˜๋Š” ์‹œ์ž‘ ๋…ธ๋“œ๋ถ€ํ„ฐ ์ด์–ด์ง„ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•˜๊ณ , ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ ์žฌ๊ท€์ ์œผ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค. ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋Š” visited ๋ฆฌ์ŠคํŠธ์— True๋กœ ํ‘œ์‹œํ•œ๋‹ค.
  4. ์ •์  ๋ฐฉ๋ฌธ ์ˆœ์„œ ์ €์žฅ: DFS์™€ BFS ํ•จ์ˆ˜์—์„œ ๋ฐฉ๋ฌธํ•œ ์ •์ ์„ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด result ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๊ฐ ํ•จ์ˆ˜์—์„œ๋Š” ๋ฐฉ๋ฌธํ•œ ์ •์ ์„ result ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  5. โญ์ž‘์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธโญ: DFS์™€ BFS ํ•จ์ˆ˜์—์„œ ์ •์ ์„ ํƒ์ƒ‰ํ•  ๋•Œ์—๋Š” ์ž‘์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฅผ ์œ„ํ•ด ๊ฐ ์ •์ ์˜ ์ธ์ ‘ ์ •์ ์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์ „์— ์ •๋ ฌํ•œ๋‹ค(sorted ์ด์šฉ). ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ž‘์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค.
  6. ๊ฒฐ๊ณผ ์ถœ๋ ฅ: ๋งˆ์ง€๋ง‰์œผ๋กœ, DFS์™€ BFS ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐฉ๋ฌธํ•œ ์ •์ ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. result ๋ฆฌ์ŠคํŠธ์— ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ๋ณธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ธํŒจํ‚นํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.

์–ธํŒจํ‚น์ด๋ž€?

์–ธํŒจํ‚น(Unpacking)์€ ํŒŒ์ด์ฌ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐœ๋…์œผ๋กœ, ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ ๋“ฑ์˜ iterableํ•œ ๊ฐ์ฒด๋ฅผ ํ’€์–ด์„œ(iterable์˜ ๊ฐ ์š”์†Œ๋ฅผ ๊ฐœ๋ณ„์ ์ธ ๊ฐ’์œผ๋กœ) ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์–ธํŒจํ‚น์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋‚˜ ๋‘ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ฒฐํ•ฉํ•˜๊ฑฐ๋‚˜ ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

e.g) ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋„ ์‚ฌ์šฉ

def my_function(a, b, c):
    print(a, b, c)

my_vars = [1, 2, 3]
my_function(*my_vars)

์ตœ์ข… ์ฝ”๋“œ

from collections import deque

# ์–‘๋ฐฉํ–ฅ ๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ธฐ
n,m,start = map(int,input().split())
graph = [[] for i in range(n+1)] 
for _ in range(m):
    a,b= map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)
    
dfs_visited = [False] * (n+1)
bfs_visited = [False] * (n+1)

# bfs ๋ฉ”์„œ๋“œ ์ •์˜
def bfs(graph, start, visited):
    queue = deque([start])
    visited[start] = True # ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ 
    result = [start] # ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
    while queue:
        v = queue.popleft()
        for i in sorted(graph[v]): # ์ž‘์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ์ •๋ ฌ
            if not visited[i]:
                queue.append(i)
                visited[i] = True
                result.append(i) # ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ
    return result

# dfs ๋ฉ”์„œ๋“œ ์ •์˜
def dfs(graph,start,visited):
    visited[start] = True # ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ 
    result = [start] # ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
    for i in sorted(graph[start]): # ์ž‘์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ์ •๋ ฌ
        if not visited[i]:
            result.extend(dfs(graph,i,visited)) # ์žฌ๊ท€ ํ˜ธ์ถœ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
    return result

print(*dfs(graph, start, dfs_visited)) # ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ธํŒจํ‚นํ•˜์—ฌ ์ถœ๋ ฅ
print(*bfs(graph, start, bfs_visited)) # ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ธํŒจํ‚นํ•˜์—ฌ ์ถœ๋ ฅ

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

์–ธํŒจํ‚น์— ๋Œ€ํ•ด ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ  ๋‹ค์–‘ํ•˜๊ฒŒ ํ™œ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

Copy link
Collaborator

@Hwangyerin Hwangyerin left a comment

Choose a reason for hiding this comment

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

  1. ์ด ๋ฌธ์ œ์—์„œ "๋‹จ, ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ข…๋ฃŒํ•œ๋‹ค." ๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์€ ์ •์ ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธ ํ•ด์•ผ๋˜๋Š” ๊ฒƒ์€ ์ดํ•ด๋ฉ๋‹ˆ๋‹ค! ๊ทธ๋Ÿฐ๋ฐ PR์— ์จ์ฃผ์‹  "DFS์™€ BFS ํ•จ์ˆ˜์—์„œ ์ •์ ์„ ํƒ์ƒ‰ํ•  ๋•Œ์—๋Š” ์ž‘์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•œ๋‹ค."๋Š” ์–ด๋–ค ์ด์œ ์—์„œ DFS์™€ BFSํ•จ์ˆ˜์—์„œ ์ž‘์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ตœ์ ์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ ์ผ๊นŒ์š”? ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ท.!๐Ÿง๐Ÿง
  2. ํŒŒ์ด์ฌ์„ ๋ฐฐ์šฐ๋Š” ์ดˆ๋ฐ˜์— '*'์„ ๋ฆฌ์ŠคํŠธ ์•ž์— ๋ถ™์—ฌ ์ถœ๋ ฅ์‹œ ๊ด„ํ˜ธ๋ฅผ ์—†์• ์ฃผ๋Š” ์—ญํ• ์ด๋ผ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์ง€๊ธˆ๋ณด๋‹ˆ ์–ธํŒจํ‚น ์ด์—ˆ๊ตฐ์š”! ์‹œํ˜„๋‹˜ ๋•๋ถ„์— ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋„ ์–ธํŒจํ‚น์„ ํ†ตํ•ด ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ๋„ ์•Œ์•„๊ฐ‘๋‹ˆ๋‹ค๐Ÿ”ฅ๐Ÿ”ฅ

@avocado-13
Copy link
Member Author

avocado-13 commented Feb 17, 2024

  1. ์ด ๋ฌธ์ œ์—์„œ "๋‹จ, ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ข…๋ฃŒํ•œ๋‹ค." ๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์€ ์ •์ ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธ ํ•ด์•ผ๋˜๋Š” ๊ฒƒ์€ ์ดํ•ด๋ฉ๋‹ˆ๋‹ค! ๊ทธ๋Ÿฐ๋ฐ PR์— ์จ์ฃผ์‹  "DFS์™€ BFS ํ•จ์ˆ˜์—์„œ ์ •์ ์„ ํƒ์ƒ‰ํ•  ๋•Œ์—๋Š” ์ž‘์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•œ๋‹ค."๋Š” ์–ด๋–ค ์ด์œ ์—์„œ DFS์™€ BFSํ•จ์ˆ˜์—์„œ ์ž‘์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ตœ์ ์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ ์ผ๊นŒ์š”? ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ท.!๐Ÿง๐Ÿง
  2. ํŒŒ์ด์ฌ์„ ๋ฐฐ์šฐ๋Š” ์ดˆ๋ฐ˜์— '*'์„ ๋ฆฌ์ŠคํŠธ ์•ž์— ๋ถ™์—ฌ ์ถœ๋ ฅ์‹œ ๊ด„ํ˜ธ๋ฅผ ์—†์• ์ฃผ๋Š” ์—ญํ• ์ด๋ผ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์ง€๊ธˆ๋ณด๋‹ˆ ์–ธํŒจํ‚น ์ด์—ˆ๊ตฐ์š”! ์‹œํ˜„๋‹˜ ๋•๋ถ„์— ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋„ ์–ธํŒจํ‚น์„ ํ†ตํ•ด ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ๋„ ์•Œ์•„๊ฐ‘๋‹ˆ๋‹ค๐Ÿ”ฅ๐Ÿ”ฅ

BFS์™€ DFS์—์„œ๋Š” ์ž‘์€ ๋…ธ๋“œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด ๋‹น์—ฐํ•œ ๊ฒƒ์œผ๋กœ ์ผ๋ฐ˜ํ™”ํ•ด์„œ ์ฝํžˆ๋„๋ก ์ž‘์„ฑ์„ ํ•˜์˜€๋„ค์š” ๐Ÿ˜‚ ์ €๋„ ํ—ท๊ฐˆ๋ ค์„œ ์ฐพ์•„๋ณด๋‹ˆ, ์ˆœ์„œ์™€ ์ƒ๊ด€์—†์ด ์ฒ˜๋ฆฌํ•ด๋„ ๋˜์ง€๋งŒ, ์ฝ”ํ…Œ์—์„œ๋Š” ๋‚ฎ์€ ์ˆœ์„œ๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋ช…์‹œํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ์–ด์„œ ๊ด€ํ–‰์ ์œผ๋กœ ๋ฒˆํ˜ธ๊ฐ€ ๋‚ฎ์€ ์ˆœ์„œ๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ํŽธ์ด๋ผ๊ณ  ํ•˜๋„ค์š”! ๐Ÿค“๐Ÿ‘๐Ÿป

Copy link
Collaborator

@LJEDD2 LJEDD2 left a comment

Choose a reason for hiding this comment

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

์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์•„๋“ค์ด๊ณ , DFS์™€ BFS๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ์˜ฌ๋ฐ”๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฐ๊ฐ ์ž˜ ๊ตฌํ˜„ํ•˜์…จ์Šต๋‹ˆ๋‹ค :)
์ž‘์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ์ •๋ ฌํ•˜๋Š” ๋ถ€๋ถ„๋„ ์ž˜ ์ฒ˜๋ฆฌํ•˜์…จ์Šต๋‹ˆ๋‹ค !

DFS์™€ BFS ํ•จ์ˆ˜์— ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณผ์ •์„ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๋Š” ๋Œ€์‹ ,
ํ•จ์ˆ˜ ๋‚ด์—์„œ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ์ง์ ‘ ์ถœ๋ ฅํ•˜๋„๋ก ์ˆ˜์ •ํ•˜์—ฌ ๊ณต๊ฐ„์„ ์กฐ๊ธˆ ๋” ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# bfs 
while queue:
        v = queue.popleft()
        print(v, end=' ')  # ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ์ง์ ‘ ์ถœ๋ ฅ
        for i in sorted(graph[v]):
            if not visited[i]:
                queue.append(i)
                visited[i] = True
# dfs 
 while stack:
        v = stack.pop()
        if not visited[v]:
            print(v, end=' ')  # ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ์ง์ ‘ ์ถœ๋ ฅ
            visited[v] = True
            stack.extend(sorted(graph[v], reverse=True))
# DFS, BFS ํ˜ธ์ถœ
dfs(graph, start)
print()
bfs(graph, start)

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ๋” ๊ฐ„๊ฒฐํ•ด์ง€๊ณ , ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์กฐ๊ธˆ ๋” ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ! โœจ

+)
์ €๋Š” dfsํ•จ์ˆ˜๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์—ˆ๋Š”๋ฐ stack์œผ๋กœ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•๋„ ๋˜๊ฒŒ ์ƒˆ๋กญ๋„ค์š” ์ต์ˆ™ํ•ด์ ธ์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค !!

def dfs(x):
    print(x,end=" ")
    visited[x] = True
    for nx in sorted(board[x]):
        if not visited[nx]:
            dfs(nx)
            visited[nx] = True

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