Skip to content

Commit

Permalink
added dijkstras algorithm [Python]
Browse files Browse the repository at this point in the history
  • Loading branch information
anoop447 committed Oct 1, 2020
1 parent 2ee8a3d commit 92f300e
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Python/Dijkstras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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')

0 comments on commit 92f300e

Please sign in to comment.