Untitled
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