Guidelines

깊이 μš°μ„  탐색(DFS) κ΅¬ν˜„ 방법

1. μŠ€νƒ μ‚¬μš©

  • DFSλŠ” μŠ€νƒμ„ μ‚¬μš©ν•˜μ—¬ 이미 λ°©λ¬Έν•œ λ…Έλ“œλ₯Ό μΆ”μ ν•©λ‹ˆλ‹€. μž¬κ·€ ν˜ΈμΆœμ„ μ‚¬μš©ν•˜μ—¬ μŠ€νƒ κ΅¬ν˜„μ„ λ‚΄μž¬ν™”ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
μŠ€νƒ μ‚¬μš© μ˜ˆμ‹œ
def dfs(graph, start, visited=[]): if start not in visited: visited.append(start) for next in graph[start]: if next not in visited: dfs(graph, next, visited) return visited

2. μž¬κ·€μ  탐색

  • ν˜„μž¬ λ…Έλ“œμ—μ„œ λ°©λ¬Έν•˜μ§€ μ•Šμ€ 인접 λ…Έλ“œλ₯Ό μ°Ύμ•„ κ³„μ†ν•΄μ„œ 탐색을 μ§„ν–‰ν•©λ‹ˆλ‹€.
μž¬κ·€μ  탐색 μ˜ˆμ‹œ
for next in graph[start]: if next not in visited: dfs(graph, next, visited)

μ‹œκ°„ λ³΅μž‘λ„

  1. μ‹œκ°„ λ³΅μž‘λ„: (O(V + E))

    • μ—¬κΈ°μ„œ VλŠ” μ •μ μ˜ 수, EλŠ” κ°„μ„ μ˜ 수λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€. 각 정점은 ν•œ λ²ˆμ”© 방문되며, 각 간선은 두 번 κ²€μ‚¬λ©λ‹ˆλ‹€(μ–‘λ°©ν–₯).
  2. λΉ„μˆœν™˜ κ·Έλž˜ν”„μ—μ„œμ˜ νš¨μœ¨μ„±: DFSλŠ” λΉ„μˆœν™˜ κ·Έλž˜ν”„μ—μ„œ λͺ¨λ“  λ…Έλ“œλ₯Ό λ°©λ¬Έν•˜λŠ” 데 νš¨κ³Όμ μž…λ‹ˆλ‹€.

  3. μˆœν™˜ κ·Έλž˜ν”„μ—μ„œμ˜ 주의점: μˆœν™˜ κ²½λ‘œκ°€ μžˆλŠ” κ·Έλž˜ν”„μ—μ„œλŠ” λ¬΄ν•œ 루프에 빠질 μœ„ν—˜μ΄ μžˆμœΌλ―€λ‘œ, 이λ₯Ό λ°©μ§€ν•˜κΈ° μœ„ν•œ 좔가적인 λ©”μ»€λ‹ˆμ¦˜μ΄ ν•„μš”ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result