BFS
from collections import deque # BFS def bfs(graph, start, visited): queue = deque([start]) visited[start] = True while queue: v = queue.popleft() print(v, end=' ') for i in graph[v]: if visited[i] == False: visited[i] = True queue.append(i) graph = [ [], [2, 3, 8], [1, 7], [1, 4, 5], [3, 5], [3, 4], [7], [2, 6, 8], [1, 7], ] visited = [False] * 9 bfs(graph, 1, visited)
2021. 4. 9.