from collections import deque
def bfs(graph, root):
# 빈 큐를 생성하고, 루트 노드를 큐에 추가합니다.
queue = deque([root])
while queue:
# 큐의 첫 번째 노드를 꺼냅니다.
node = queue.popleft()
# 현재 노드를 출력합니다.
print(node, end=' ')
# 현재 노드의 인접 노드들을
for nei_node in graph[node]:
# 큐에 추가합니다.
queue.append(nei_node)
graph = {
1: [2, 3, 8],
2: [7],
3: [4],
4: [5],
5: [],
6: [],
7: [6],
8: []
}
bfs(graph, 1)