1. 程式人生 > >python實現----dijkstra優先佇列

python實現----dijkstra優先佇列

眾所周知,求無負權的最短路問題使用dijkstra演算法解決,其可以用優先佇列優化,這裡給出python實現的版本

import time
import heapq

class Node:

     def __init__(self, name):
        self.name = name
        self.vis = False
        self.adjacenciesList = []
        self.pre = None
        self.dis = 0x3f3f3f3f    

     def __lt__(self, other):
        return self.dis < other.dis

class Edge:

    def __init__(self, weight, startvertex, endvertex):
        self.weight = weight
        self.startvertex = startvertex
        self.endvertex = endvertex

def calculateshortestpath(vertexlist, startvertex):
    q = []
    startvertex.dis = 0
    heapq.heappush(q, startvertex)

    while q:
        tmp = heapq.heappop(q)
        for edge in tmp.adjacenciesList:
            tempdist = edge.startvertex.dis + edge.weight
            if tempdist < edge.endvertex.dis:
                edge.endvertex.dis = tempdist
                edge.endvertex.pre = edge.startvertex
                heapq.heappush(q,edge.endvertex)
def getshortestpath(t):
    print("The value of it's minimum distance is: ",t.dis),
    node = t
    while node:
        print(node.name),
        node = node.pre
    print(" ")
#主函式部分
node=[]
edge=[]
n=int(raw_input())
m=int(raw_input())
for i in range(n):
     node.append(Node(i+1)) #生成節點的名字
for i in range(m):
     a,b,c = map(int, raw_input().split())  #a到b的距離為c
     edge.append(Edge(c,node[a-1],node[b-1]))
     node[a-1].adjacenciesList.append(edge[i])
begin=time.clock()
calculateshortestpath(node,node[0])
for i in range(n):
 getshortestpath(node[i])
end=time.clock()
end-=begin
print(" ")
print("read :%f s" % end)