forked from gisalgs/networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.py
79 lines (71 loc) · 2.19 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Dijkstra's shortest path algorithm
Contact:
Ningchuan Xiao
The Ohio State University
Columbus, OH
"""
__author__ = "Ningchuan Xiao <[email protected]>"
# from network2listmatrix import *
def dijkstra(source, distmatrix):
"""
Dijkstra's single source shortest path algorithm.
Input
source: index to source node
distmatrix: distance matrix where cell (i, j) is INF
if nodes i and j are not on an edge,
otherwise the value is the weight/distance
of the edge
Output
dist: cumulative distance from source to each node
prev: list of previous node for each node on the path
"""
n = len(distmatrix)
dist = [INF if i!=source else 0 for i in range(n)]
prev = [None for i in range(n)]
Q = list(range(n))
while len(Q)>0:
u = get_remove_min(Q, dist)
U = get_neighbor(u, distmatrix, n)
for v in U:
newd = dist[u] + distmatrix[u][v]
if newd < dist[v]:
dist[v] = newd
prev[v] = u
return dist, prev
def get_remove_min(Q, dist):
"""
Finds the node in Q with smallest distance in dist, and
removes the node from Q.
Input
Q: a list of candidate nodes
dist: a list of distances to each node from the source
Output
imin: index to the node with smallest distance
"""
dmin = INF
imin = -1
for i in Q:
if dist[i] < dmin:
dmin = dist[i]
imin = i
Q.remove(imin)
return imin
def get_neighbor(u, d, n):
neighbors = [i for i in range(n)
if d[i][u]!=INF and i!=u]
return neighbors
def shortest_path(source, destination, distmatrix):
dist, prev = dijkstra(source, distmatrix)
last = prev[destination]
path = [destination]
while last is not None:
path.append(last)
last = prev[last]
return path, dist[destination]
if __name__ == "__main__":
from network2listmatrix import *
fname = '../data/network-links'
a = network2distancematrix(fname, True)
print(shortest_path(1, 6, a))
print(shortest_path(0, 7, a))