-
Notifications
You must be signed in to change notification settings - Fork 0
/
breadth_first_search.py
60 lines (52 loc) · 1.37 KB
/
breadth_first_search.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from collections import defaultdict, deque
from typing import Tuple
def bfs_shortest_path(edges: list[Tuple[int, int]], source: int, destination: int):
# Create two-way connection dict
adj_dict = defaultdict(list)
for start, stop in edges:
adj_dict[start].append(stop)
adj_dict[stop].append(start)
# Init variables
search = [source]
prev = {source: None}
visited = {source}
queue = deque()
queue.append(source)
# Start deque node and iterate
while len(queue) > 0:
current_node = queue.popleft()
adj_nodes = adj_dict[current_node]
# Visit nodes
for adj_node in adj_nodes:
if adj_node not in visited:
print(adj_node)
queue.append(adj_node)
visited.add(adj_node)
search.append(adj_node)
prev[adj_node] = current_node
# Backtrack path
search_node = prev[destination]
path = [destination]
while True:
if search_node == None:
return None
path.append(search_node)
if search_node == source:
break
search_node = prev[search_node]
path.reverse()
return path
edges = [
(0, 1),
(1, 2),
(2, 3),
(0, 3),
(3, 4),
(3, 5),
(4, 7),
(7, 8),
(7, 9),
(5, 10)
]
path = bfs_shortest_path(edges, 0 ,10)
print(path)