Untitled
unknown
plain_text
a year ago
357 B
8
Indexable
from collections import deque
graph = {'A': ['B', 'C'], 'B': ['D', 'E'], 'C': ['F'], 'D': [], 'E': ['F'], 'F': []}
def bfs(g, s):
v, q = set(), deque([s])
while q:
n = q.popleft()
print(n, end=' ')
for i in g[n]:
if i not in v:
v.add(i)
q.append(i)
bfs(graph, 'A')Editor is loading...
Leave a Comment