Breadth-First Search
code: python
from collections import deque
# V: 頂点 E: 辺
# O(V + E), O(V)
def bfs(graph, start):
visited = set()
res = []
while queue:
# FIFO
now = queue.popleft()
if now not in visited:
visited.add(now)
res.append(now)
queue.append(v)
return res
graph = {
}
def test_bfs():
assert bfs(graph, 'A') == 'A', 'B', 'C', 'D', 'E', 'F', 'H', 'I', 'J', 'K', 'G', 'L'