python 实现 Dijkstra最短路径问题

代码块

# -*- coding:utf-8 -*-
''' dijkstra 算法计算最短路径,通过优先队列Q优化,图G用邻接表保存 dij(G,s)返回以s为源点,到途中所有点的最短路径 优先队列Q的每个element保存结点和s到该结点的最短距离值 在所有未访问的点中,从Q的栈顶取出结点v和s到v的最短距离值 对所有从v出发的边的终点y,更新dis[y]=min(dis[y],dis[v]+l(v->y)) '''

class stack(list):
    add=list.append


def dij(G,s):
    ''' dis保存源点到所有点的最短距离值 '''
    dis=[1000000 for i in range(VN)]  #距离值初始化
    dis[0]=0
    dis[s]=0
    ''' 优先队列Q的每个结点保存vertice和s到该vertice的最短距离 '''
    Q=stack()
    Q.add([s,dis[s]])
    ''' 从Q的栈顶取出结点v和s到v的最短距离值 对所有从v出发的边的终点y,更新dis[y]=min(dis[y],dis[v]+l(v->y)) '''
    while Q:
        node=Q.pop()
        v,dsv=node[0],node[1]
        for i in range(len(G[v])):
            y=G[v][i]
            vout,dvy=y[0],y[1]
            if dis[vout]>dsv+dvy:       
                dis[vout]=dsv+dvy
                Q.add([y[0],dis[vout]])
    return dis







if __name__ == "__main__":
    fr=open('dijtest.txt')
    VN=15  #number of vertices+1
    arr1=fr.readlines()
    G= [[] for i in range(VN)]
    for line in arr1:
        line=line.split()
        ver=int(line[0])
        outver=len(line)-1
        G[ver]=[[] for i in range(outver)]
        for i in range(1,len(line)):
            outv=int(line[i].split(',')[0])
            outd=int(line[i].split(',')[1])
            G[ver][i-1].append(outv)
            G[ver][i-1].append(outd)

    dis=dij(G,13)
    print "The shortest distance between ver#13 and ver#5 is %d."%(dis[5])   
    #输出结点13到结点5的最短距离

>>> The shortest distance between ver#13 and ver#5 is 26.
dijtest.txt
The file contains an adjacency list representation of an undirected
weighted graph with 14 vertices labeled 1 to 14. Each row consists of
the node tuples that are adjacent to that particular vertex along with
the length of that edge. For example, the 6th row has 6 as the first
entry indicating that this row corresponds to the vertex labeled 6. The
next entry of this row "2,74" indicates that there is an edge
between vertex 6 and vertex 2 that has length 74. The rest of the
pairs of this row indicate the other vertices adjacent to vertex 6 and
the lengths of the corresponding edges.
0 1,28 2,4 3,53 4,95 5,59 7,48 8,84 10,27 11,7 12,91 13,96
1 0,28 2,79 5,30 7,59 8,62 9,20 11,15 12,16 13,96
2 0,4 1,79 3,80 5,19 6,74 8,59 10,80 11,81 12,65 13,7 14,13
3 0,53 2,80 4,87 5,42 6,70 7,8 12,44 13,92
4 0,95 3,87 5,86 6,46 7,44 9,12 10,39 12,14 13,95 14,19
5 0,59 1,30 2,19 3,42 4,86 6,12 7,0 8,72 10,92 11,54 12,0 13,68 14,22
6 2,74 3,70 4,46 5,12 7,86 9,76 10,68 11,32 12,62 13,34
7 0,48 1,59 3,8 4,44 5,0 6,86 8,38 9,34 10,12 11,55 12,25 14,9
8 0,84 1,62 2,59 5,72 7,38 10,66 11,31 12,95
9 1,20 4,12 6,76 7,34 10,63 11,37 12,93 13,79
10 0,27 2,80 4,39 5,92 6,68 7,12 8,66 9,63 12,45 14,78
11 0,7 1,15 2,81 5,54 6,32 7,55 8,31 9,37 12,75 13,87
12 0,91 1,16 2,65 3,44 4,14 5,0 6,62 7,25 8,95 9,93 10,45 11,75 13,46 14,63
13 0,96 1,96 2,7 3,92 4,95 5,68 6,34 9,79 11,87 12,46
14 2,13 4,19 5,22 7,9 10,78 12,63
    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/yyi_hkust/article/details/49873605
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞