Skip to content

Instantly share code, notes, and snippets.

@mikezink
Created October 29, 2019 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikezink/0ab6bc71f68503670c697cbad5832e5c to your computer and use it in GitHub Desktop.
Save mikezink/0ab6bc71f68503670c697cbad5832e5c to your computer and use it in GitHub Desktop.
from Graph import Graph, Vertex
from PriorityQueue import PriorityQueue
def dijkstra(aGraph,start):
pq = PriorityQueue()
start.setDistance(0)
pq.buildHeap([(v.getDistance(),v) for v in aGraph])
while not pq.isEmpty():
currentVert = pq.delMin()
for nextVert in currentVert.getConnections():
newDist = currentVert.getDistance() \
+ currentVert.getWeight(nextVert)
if newDist < nextVert.getDistance():
nextVert.setDistance( newDist )
nextVert.setPred(currentVert)
pq.decreaseKey(nextVert,newDist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment