-
Notifications
You must be signed in to change notification settings - Fork 0
/
breadthFirstSearch.py
29 lines (25 loc) · 926 Bytes
/
breadthFirstSearch.py
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
from collections import deque
def bfs_print(graph, start_node):
discovered = {start_node: None} # dictionary used to map vertex to edge used to discover it
queue = deque()
queue.append(start_node)
while queue:
# get adjacent nodes
curr = queue.popleft() # pop oldest node
process_node(curr) # perform some operation when visiting it
for node in graph[curr]: # add all of its non-discovered adjacent nodes to queue
if node not in discovered:
discovered[node] = curr
queue.append(node)
def process_node(node):
print(f"Visiting node {node}")
if __name__ == '__main__':
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
bfs_print(graph, 'A')