def dijkstraAlg(self, startCity, endCity, adjList): ''' Finds the shortest path between two cities ''' pQueue = Queue.PriorityQueue() for adj in adjList[startCity]: adj.accumDist = adj.distance pQueue.put((adj.accumDist, adj)) for iter in adjList: for adjC in adjList[iter]: if adjC.startCity == startCity: adjC.accumDist = adj.distance newAdj = Route(adjC.endCity, adjC.startCity, adjC.accumDist) pQueue.put((adjC.accumDist, newAdj)) itEdge = pQueue.get() adj = itEdge[1] while adj.endCity != endCity: itEdge[1].visited = True for adj in adjList[itEdge[1].endCity]: adj.parent = itEdge[1] if adj.visited == False: adj.accumDist = adj.parent.accumDist + adj.distance if adj.endCity == endCity: break for iter in adjList[adj.endCity]: if iter.accumDist < adj.accumDist and iter.visited == True: adj.parent = iter pQueue.put((adj.accumDist, adj)) for iter in adjList: for adjC in adjList[iter]: if adjC.endCity == itEdge[1].endCity: newAdj = Route(adjC.endCity, adjC.startCity, adjC.distance) newAdj.parent = itEdge[1] if newAdj.visited == False: newAdj.accumDist = newAdj.distance + newAdj.parent.accumDist if newAdj.startCity == endCity: adj = newAdj break for iter in adjList[newAdj.endCity]: if iter.accumDist < newAdj.accumDist and iter.visited == True: newAdj.parent = iter pQueue.put((adjC.accumDist, newAdj)) itEdge = pQueue.get() print 'Done! The path is:' print adj.endCity print adj.startCity while adj.startCity != startCity: adj = adj.parent print adj.endCity print adj.startCity