-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS.py
80 lines (60 loc) · 2.06 KB
/
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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Breadth First Search
Given a graph which is represented by adjacent lists, starting node and destination node
For example:
graph = { "A" : ["B", "C"], "B": ["D"] } means there exists a path A -> B, A -> C and B -> D
Assume all paths have same length.
Return: a list, representing the shortest path from starting to destionation node
Example: bfs({ "A" : ["B", "C"], "B": ["D"] }, "A", "D") -> ["A", "B", "D"]
"""
from queue import Queue
from typing import Union
Node = Union[str, int]
Graph = dict[Node, list[Node]]
ParentDict = dict[Node, Node]
def get_parents(graph: Graph, start: Node) -> ParentDict:
"""Return a key-value pair of a node and its parent
when traversing down a path from a start node
>> get_parents({ "A": ["B", "C"], "B": ["D"] }, "A") -> {"B": "A", "C": "A", "D": "B"}
"""
queue = Queue()
parents: ParentDict = {}
explored: set[Node] = set()
explored.add(start)
queue.put(start)
while not queue.empty():
new_node: Node = queue.get()
if new_node not in graph:
continue
for neighbour in graph[new_node]:
if neighbour not in explored:
explored.add(neighbour)
parents[neighbour] = new_node
queue.put(neighbour)
return parents
def get_path(parent: ParentDict, start: Node, end: Node) -> list[Node]:
"""Get shortest path from start to end node from the parent dict
"""
path = [end]
while end != start:
path.append(parent[end])
end = parent[end]
path.reverse()
return path
def bfs(graph: Graph, start: Node, end: Node) -> list[Node]:
"""Get shortest path from start to end node from a graph
"""
parents = get_parents(graph, start)
if end in parents:
path = get_path(parents, start, end)
return path
return []
if __name__ == "__main__":
sample_graph = {
"A": ["B", "C"],
"B": ["D", "E"],
"C": ["B", "F"],
"E": ["F"]
}
shortest_path: list[Node] = bfs(sample_graph, "A", "F")
print(shortest_path) # ["A", "C", "F"]