-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortestPathBfs.py
61 lines (51 loc) · 1.9 KB
/
shortestPathBfs.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
# Program to implement shortest path using breadth first search
# Instance of a graph stored as adjacency list using dictionary
# Keys : Nodes
# Values : Neighbours
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': ['G'],
'G': []
}
# finds shortest path between 2 nodes of a graph using BFS
def bfs_shortest_path(graph, start, goal):
# keep track of explored nodes
explored = []
# keep track of all the paths to be checked
queue = [[start]]
# return path if start is goal
if start == goal:
return "That was easy! Start = goal"
# keeps looping until all possible paths have been checked
while queue:
print("The paths are ", queue)
# pop the first path from the queue
path = queue.pop(0)
print("The path being containing the node being checked")
# get the last node from the path
node = path[-1]
print("The node ",node, "neighbours are being searched now.")
if node not in explored:
print("See all the neighbours of the node")
neighbours = graph[node]
# go through all neighbour nodes
# construct a new path and
# push it into the queue
for neighbour in neighbours:
new_path = list(path)
new_path.append(neighbour)
queue.append(new_path)
# return path if neighbour is goal
if neighbour == goal:
return new_path
# mark node as explored
explored.append(node)
# in case there's no path between the 2 nodes
return "So sorry, but a connecting path doesn't exist :("
sp = bfs_shortest_path(graph, 'A', 'D') # returns ['G', 'C', 'A', 'B', 'D']
print(sp)
print("The number of edges between given path is", len(sp))