forked from CodeSadhu/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dijkstras.py
50 lines (36 loc) · 1.34 KB
/
Dijkstras.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
import math
def Dijkstras(graph, start, goal):
unseenNodes = graph
predecessor = {}
shortest_distance = {}
path = []
infinty = math.inf
for node in unseenNodes:
shortest_distance[node] = infinty
shortest_distance[start] = 0
while unseenNodes:
minNode = None
for node in unseenNodes:
if minNode is None:
minNode = node
elif shortest_distance[node] < shortest_distance[minNode]:
minNode = node
for chilNode,weight in graph[minNode].items():
if weight + shortest_distance[minNode] < shortest_distance[chilNode]:
shortest_distance[chilNode] = weight + shortest_distance[minNode]
predecessor[chilNode] = minNode
unseenNodes.pop(minNode)
currenNode = goal
while currenNode != start:
try:
path.insert(0,currenNode)
currenNode = predecessor[currenNode]
except KeyError:
print('path not found')
break
path.insert(0,start)
if shortest_distance[goal] != infinty:
print('The shortest distance is ' + str(shortest_distance[goal]))
print('And the path is ' + str(path))
graph = {'a':{'b':10,'c':3},'b':{'c':1,'d':2},'c':{'b':4,'d':8,'e':2},'d':{'e':7},'e':{'d':9}}
Dijkstras(graph, 'a', 'd')