-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.py
More file actions
36 lines (32 loc) · 873 Bytes
/
bfs.py
File metadata and controls
36 lines (32 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from collections import deque
def bfs(graph, start, visited):
# bfs 구현 위해 사용하는 deque 라이브러리
queue = deque([start])
# 현재 노드 방문 처리
visited[start] = True
# 큐가 빌 때까지 반복
while queue:
# 방문 처리
v = queue.popleft()
print(v, end='')
# 아직 방문하지 않은 경우 큐에 삽입
for i in graph[v]:
if not visited[i]:
queue.append(i)
visited[i] = True
# 각 노드에 인접하게 연결되어 있는 노드들을 2차원 리스트로 표현
graph = [
[],
[2, 3, 8],
[1, 7],
[1, 4, 5],
[3, 5],
[3, 4],
[7],
[2, 6, 8],
[1, 7]
]
# 방문된 노드를 체크하기 위한 리스트
visited = [False] * 9
# bfs 함수 호출(노드 1부터 시작)
bfs(graph, 1, visited)