Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
357 B
1
Indexable
Never
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')
Leave a Comment