Example #1
0
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
    
Example #2
0
import pq

q = [(4, "a"), (5, "b"), (3, "c")]

print q
m = pq.deletemin(q)
print m, q
pq.decreasekey(q, "b", 2)
print q