(graph, start)
| 18 | |
| 19 | |
| 20 | def bfs(graph, start): |
| 21 | explored, queue = set(), [start] # collections.deque([start]) |
| 22 | explored.add(start) |
| 23 | while queue: |
| 24 | v = queue.pop(0) # queue.popleft() |
| 25 | for w in graph[v]: |
| 26 | if w not in explored: |
| 27 | explored.add(w) |
| 28 | queue.append(w) |
| 29 | return explored |
| 30 | |
| 31 | |
| 32 | G = {'A': ['B', 'C'], |