Guidelines

λ„ˆλΉ„ μš°μ„  탐색(BFS) κ΅¬ν˜„ 방법

1. 큐 μ‚¬μš©

  • BFSλŠ” 큐λ₯Ό μ‚¬μš©ν•˜μ—¬ 탐색할 λ…Έλ“œλ“€μ„ κ΄€λ¦¬ν•©λ‹ˆλ‹€. 큐λ₯Ό μ‚¬μš©ν•˜μ—¬ 각 λ ˆλ²¨λ³„λ‘œ λ…Έλ“œλ₯Ό 순차적으둜 νƒμƒ‰ν•©λ‹ˆλ‹€.
큐 μ‚¬μš© μ˜ˆμ‹œ
def bfs(graph, start): visited = [] queue = [start] while queue: node = queue.pop(0) if node not in visited: visited.append(node) # 리슀트λ₯Ό μ΄μš©ν•˜μ—¬ 쀑볡 제거 queue.extend([n for n in graph[node] if n not in visited]) return visited

2. λ ˆλ²¨λ³„ 탐색

  • BFSλŠ” 각 λ…Έλ“œμ˜ λ ˆλ²¨λ³„λ‘œ 인접 λ…Έλ“œλ₯Ό νƒμƒ‰ν•©λ‹ˆλ‹€. μ΄λŠ” 큐의 μ„ μž…μ„ μΆœ(FIFO) νŠΉμ„±μ„ ν™œμš©ν•©λ‹ˆλ‹€.
λ ˆλ²¨λ³„ 탐색 μ˜ˆμ‹œ
while queue: node = queue.pop(0) if node not in visited: visited.append(node) # 리슀트λ₯Ό μ΄μš©ν•˜μ—¬ 쀑볡 제거 queue.extend([n for n in graph[node] if n not in visited])

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

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

    • μ—¬κΈ°μ„œ VλŠ” μ •μ μ˜ 수, EλŠ” κ°„μ„ μ˜ 수λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€. 각 정점은 ν•œ λ²ˆμ”© 방문되며, 각 간선은 ν•œ λ²ˆμ”© κ²€μ‚¬λ©λ‹ˆλ‹€.
  2. μ΅œλ‹¨ 경둜 탐색: BFSλŠ” 무방ν–₯ κ·Έλž˜ν”„μ—μ„œ μ‹œμž‘ λ…Έλ“œμ—μ„œ λͺ©ν‘œ λ…Έλ“œκΉŒμ§€μ˜ μ΅œλ‹¨ 경둜λ₯Ό μ°ΎλŠ” 데 νš¨κ³Όμ μž…λ‹ˆλ‹€.

  3. 넓은 λ²”μœ„ 탐색: BFSλŠ” λ ˆλ²¨λ³„ 탐색을 톡해 넓은 λ²”μœ„μ˜ λ…Έλ“œλ“€μ„ 효과적으둜 탐색할 수 μžˆμŠ΅λ‹ˆλ‹€.

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result