def dijkstra(weights, start, dest): cost = {start: 0} done = set() Q = minpriorityqueue() Q.push((0, start)) while not Q.empty(): rank, n = Q.pop() if n in done: continue done.add(n) if n == dest: return cost[n] for o in n.getNeighbors(): if o not in done: thiscost = cost[n] + weights[n,o] # weight from n to o shouldAdd = False if o in cost and thiscost < cost[o]: shouldAdd = True if o not in cost: shouldAdd = True if shouldAdd: cost[o] = thiscost Q.push((thiscost, o)) assert False
def dijkstra(weights, start, dest): cost = {start: 0} done = set() Q = minpriorityqueue() Q.push((0, start)) while not Q.empty(): rank, n = Q.pop() if n in done: continue done.add(n) if n == dest: return cost[n] for o in n.getNeighbors(): if o not in done: thiscost = cost[n] + weights[n, o] # weight from n to o shouldAdd = False if o in cost and thiscost < cost[o]: shouldAdd = True if o not in cost: shouldAdd = True if shouldAdd: cost[o] = thiscost Q.push((thiscost, o)) assert False
def run(): print "minpriorityqueue TEST STARTING" pq = minpriorityqueue() pq.push((100, 10)) pq.push((1, 5)) pq.push((4, 1000)) rank, key = pq.pop() assert rank == 1 assert key == 5 rank, key = pq.pop() assert rank == 4 assert key == 1000 rank, key = pq.pop() assert rank == 100 assert key == 10 assert pq.empty() print "minpriorityqueue TEST COMPLETED"