-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortestpath.py
124 lines (104 loc) · 4 KB
/
shortestpath.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import sys
class Graph():
def __init__(self, vertices):
self.V = vertices
self.graph = [[0 for column in range(vertices)]
for row in range(vertices)]
self.time = []
self.sumtime_node = []
self.mintime_node = []
self.short_path = []
def printSolution(self, dist):
print ("Vertex tDistance from Source")
print(dist)
for node in range(self.V):
print (node,"t",dist[node])
# A utility function to find the vertex with
# minimum distance value, from the set of vertices
# not yet included in shortest path tree
def minDistance(self, dist, sptSet):
# Initilaize minimum distance for next node
min = sys.maxsize
# Search not nearest vertex not in the
# shortest path tree
for v in range(self.V):
if (dist[v] < min and sptSet[v] == False):
min = dist[v]
min_index = v
return min_index
# Funtion that implements Dijkstra's single source
# shortest path algorithm for a graph represented
# using adjacency matrix representation
def dijkstra(self, src):
dist = [sys.maxsize] * self.V
dist[src] = 0
sptSet = [False] * self.V
for cout in range(self.V):
# Pick the minimum distance vertex from
# the set of vertices not yet processed.
# u is always equal to src in first iteration
u = self.minDistance(dist, sptSet)
# Put the minimum distance vertex in the
# shotest path tree
sptSet[u] = True
# Update dist value of the adjacent vertices
# of the picked vertex only if the current
# distance is greater than new distance and
# the vertex in not in the shotest path tree
for v in range(self.V):
if(self.graph[u][v] > 0 and sptSet[v] == False and
dist[v] > dist[u] + self.graph[u][v]):
dist[v] = dist[u] + self.graph[u][v]
# print(self.graph[u][v], " ", dist[u], " ", dist[v])
self.time.append(self.graph[u][v])
self.sumtime_node.append(dist[u])
self.mintime_node.append(dist[v])
# self.printSolution(dist)
mintime = dist[10] # 4 -> destination node
while(True):
index = self.mintime_node.index(mintime)
self.short_path.append(self.time[index])
mintime = self.sumtime_node[index]
if(sum(self.short_path) == dist[10]):
break
print("shortest path with time :", list(reversed(self.short_path)))
# Driver program
# g = Graph(9)
# g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0],
# [4, 0, 8, 0, 0, 0, 0, 11, 0],
# [0, 8, 0, 7, 0, 4, 0, 0, 2],
# [0, 0, 7, 0, 9, 14, 0, 0, 0],
# [0, 0, 0, 9, 0, 10, 0, 0, 0],
# [0, 0, 4, 14, 10, 0, 2, 0, 0],
# [0, 0, 0, 0, 0, 2, 0, 1, 6],
# [8, 11, 0, 0, 0, 0, 1, 0, 7],
# [0, 0, 2, 0, 0, 0, 6, 7, 0]
# ];
# g.dijkstra(0);
# g = Graph(5)
# g.graph = [[0, 10, 0, 1, 0],
# [0, 0, 5, 15, 0],
# [5, 0, 0, 2, 4],
# [1, 15, 2, 0, 0],
# [0, 0, 4, 0, 0]
# ];
# g.dijkstra(0);
g = Graph(15)
# 0 1 2 3 4 5 6 7 8 9 1011121314
g.graph = [[0,2,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,10,0,0,0,0,0,0,0,0,0,1,0,0],
[10,0,0,12,0,0,0,0,0,0,0,0,0,0,0],
[0,0,12,0,11,0,0,2,0,0,0,0,0,0,0],
[0,0,0,11,0,15,0,0,0,0,0,0,0,0,0],
[0,0,0,0,15,0,4,0,0,0,0,0,0,0,9],
[0,0,0,0,0,4,0,5,0,0,0,0,0,1,0],
[0,0,0,2,0,0,5,0,0,0,0,4,0,0,0],
[0,0,0,0,0,0,0,0,0,5,10,0,0,0,0],
[0,0,0,0,0,0,0,0,5,0,0,0,0,0,3],
[0,0,0,0,0,0,0,0,10,0,0,0,0,1,0],
[0,0,0,0,0,0,0,4,0,0,0,0,3,0,0],
[0,1,0,0,0,0,0,0,0,0,0,3,0,0,0],
[0,0,0,0,0,0,1,0,0,0,1,0,0,0,0],
[0,0,0,0,0,9,0,0,0,3,0,0,0,0,0],
];
g.dijkstra(0);