def findShortestPath(rDag, v, distance): shortest = maxint for edge in rDag[v]: d = distance[otherEnd(edge)] + length(edge) if d < shortest: shortest = d return shortest
def extractEdges(graph): edges = [] for node in graph: for edge in graph[node]: v = otherEnd(edge) if node < v: edges.append([length(edge), node, v]) return edges
def dijkstra(graph, start): distance = {} for node in graph: distance[node] = maxint # a very large number used for "infinity" distance[start] = 0 previous = {} previous[start] = 'nil' q = priorityQueue.makeQueue(distance) # priority queue using distance-values as keys while len(q) > 0: (value, u) = priorityQueue.deletemin(q) for edge in graph[u]: l = length(edge) v = otherEnd(edge) if distance[v] > distance[u] + l: distance[v] = distance[u] + l previous[v] = u priorityQueue.decreasekey(q, v, distance[v]) print "Decreasekey for", v, "to", distance[v] #print "Dijkstra from", start, ":" #print "Distances:", distance #print "Previous:", previous return distance