-
-
Save mikezink/0ab6bc71f68503670c697cbad5832e5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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