-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS.py
31 lines (26 loc) · 781 Bytes
/
BFS.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
30
31
from collections import deque
def breadth_first_search(graph, start_node, target_node):
queue = deque([(start_node, [start_node])])
visited = set()
while queue:
current_vertex, path = queue.popleft()
if current_vertex not in visited:
if current_vertex == target_node:
return path
visited.add(current_vertex)
for neighbor in graph.get(current_vertex, []):
queue.append((neighbor, path + [neighbor]))
return None
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['G'],
'F': ['G'],
'G': []
}
start_node = 'A'
target_node = 'G'
result_path = breadth_first_search(graph, start_node, target_node)
print("BFS path:", result_path)