-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDijkstra.py
43 lines (35 loc) · 852 Bytes
/
Dijkstra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'Dijkstra'
__author__ = 'lxp'
#《大话数据结构》260页,基于邻接矩阵的最短路径Dijkstra算法
import adjacencyMatrix
def shortestPath_Dijkstra(G, v0):
final = [0] * G.numVertexes
P = [0] * G.numVertexes
D = []
for i in range(G.numVertexes):
D.append(G.arc[v0][i])
D[v0] = 1
for v in range(1, G.numVertexes):
min_ = float('inf')
for w in range(G.numVertexes):
if not final[w] and D[w] < min_:
k = w
min_ = D[w]
final[k] = 1
for w in range(G.numVertexes):
if not final[w] and (min_ + G.arc[k][w]) < D[w]:
D[w] = min_ + G.arc[k][w]
P[w] = k
return P
#test
def test():
sample = adjacencyMatrix.MGraph()
sample.createMGraph()
sample.showMGraph()
res = shortestPath_Dijkstra(sample, 0)
print(res)
return
if __name__ == '__main__':
test()