-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathspalgm.py
More file actions
291 lines (250 loc) · 9.68 KB
/
spalgm.py
File metadata and controls
291 lines (250 loc) · 9.68 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""Implementations of Modified Label Correcting (MLC) Algorithm for
Single Source Shortest Path Problem (SSSP) including:
1. FIFO
2. Double-Ended Queue (Deque)
3. Minimum Distance Label (essentially Dijkstra's Algorithm)
07/19/20, Peiheng Li ([email protected])
"""
from time import time
import heapq
import collections
import SimpleDequeC
from classes import SimpleDequePy, SpecialDequePy
from utils import MAX_LABEL, dist_apsp, pred_apsp, \
GetNode, GetLink, GetNextNodeID, GetNumNodes
def CalculateSSSPFIFOI(srcNodeID, dist, pred):
""" FIFO implementation of MLC using built-in list and x in s operation
The time complexity of x in s operation for built-in list is O(n), where n
is the size of list at run time.
"""
dist[srcNodeID] = 0
# list
selist = []
selist.append(srcNodeID)
# label correcting
while selist:
i = selist.pop(0)
node = GetNode(i)
for linkID in node.GetOutgoingLinks():
link = GetLink(linkID)
j = link.GetDestNodeID()
if dist[j] > dist[i] + link.GetLen():
dist[j] = dist[i] + link.GetLen()
pred[j] = i
if j not in selist:
selist.append(j)
def CalculateSSSPFIFOII(srcNodeID, numNode, dist, pred):
""" FIFO implementation of MLC using built-in list and indicator array
x in s operation for built-in list can be replaced using an
indicator/status array. The time complexity is only O(1).
"""
status = [0] * numNode
dist[srcNodeID] = 0
# list
selist = []
selist.append(srcNodeID)
status[srcNodeID] = 1
# label correcting
while selist:
i = selist.pop(0)
status[i] = 0
node = GetNode(i)
for linkID in node.GetOutgoingLinks():
link = GetLink(linkID)
j = link.GetDestNodeID()
if dist[j] > dist[i] + link.GetLen():
dist[j] = dist[i] + link.GetLen()
pred[j] = i
if not status[j]:
selist.append(j)
status[j] = 1
def CalculateSSSPDEQI(srcNodeID, numNode, dist, pred):
""" Deque implementation of MLC using list and Dr. Zhou's approach.
The time complexities of pop(0) and insert(0, x) for built-in list are both
O(n), where n is the size of list at run time.
"""
status = [0] * numNode
dist[srcNodeID] = 0
# list
selist = []
selist.append(srcNodeID)
status[srcNodeID] = 1
# label correcting
while selist:
i = selist.pop(0)
# 2 indicates the current node p appeared in selist before
# but is no longer in it.
status[i] = 2
node = GetNode(i)
for linkID in node.GetOutgoingLinks():
link = GetLink(linkID)
j = link.GetDestNodeID()
if dist[j] > dist[i] + link.GetLen():
dist[j] = dist[i] + link.GetLen()
pred[j] = i
if status[j] == 2:
selist.insert(0, j)
status[j] = 1
elif status[j] == 0:
selist.append(j)
status[j] = 1
def CalculateSSSPDEQII(srcNodeID, numNode, dist, pred):
""" Deque implementation of MLC using deque and Dr. Zhou's approach.
The computation efficiency can be improve by replacing built-in list with
deque as well as the following operations:
1. popleft(),
2. appendleft(x).
Their running times are both O(1).
See https://github.com/jdlph/Path4GMNS for more efficient implementation
"""
status = [0] * numNode
dist[srcNodeID] = 0
# deque, choose one of the following three
# selist = collections.deque()
selist = SimpleDequePy(numNode)
# selist = SimpleDequeC.deque(numNode)
selist.append(srcNodeID)
status[srcNodeID] = 1
# label correcting
while selist:
i = selist.popleft()
# 2 indicates the current node p appeared in selist before
# but is no longer in it.
status[i] = 2
node = GetNode(i)
for linkID in node.GetOutgoingLinks():
link = GetLink(linkID)
j = link.GetDestNodeID()
if dist[j] > dist[i] + link.GetLen():
dist[j] = dist[i] + link.GetLen()
pred[j] = i
if status[j] == 2:
selist.appendleft(j)
status[j] = 1
elif status[j] == 0:
selist.append(j)
status[j] = 1
def CalculateSSSPDEQIII(srcNodeID, numNode, dist, pred):
""" Deque implementation of MLC using deque without status array
It is equivalent to shortest_path_n() in
https://github.com/jdlph/Path4GMNS/blob/dev/engine/path_engine.cpp
It is a little bit slower than CalculateSSSPDEQII() using SimpleDequePy.
For their C++ counterparts, CalculateSSSPDEQIII() outperforms
CalculateSSSPDEQII() by a 1% margin.
"""
dist[srcNodeID] = 0
# deque
selist = SpecialDequePy(numNode)
selist.append(srcNodeID)
# label correcting
while selist:
i = selist.popleft()
node = GetNode(i)
for linkID in node.GetOutgoingLinks():
link = GetLink(linkID)
j = link.GetDestNodeID()
if dist[j] > dist[i] + link.GetLen():
dist[j] = dist[i] + link.GetLen()
pred[j] = i
if selist.pastnode(j):
selist.appendleft(j)
elif selist.newnode(j):
selist.append(j)
def CalculateSSSPDijkstraI(srcNodeID, numNode, dist, pred):
""" Minimum Distance Label Implementation without heap
There are two major operations with this implementation:
1. Find the node with the minimum distance label from the scan eligible
list by looping through all the nodes in this list, which takes O(n)
time;
2. Remove this node from list by the built-in remove() operation, which
takes O(n) time as well.
The overall time complexity of these two operations is O(n), where, n is
the list size at run time.
"""
status = [0] * numNode
dist[srcNodeID] = 0
# list
selist = []
selist.append(srcNodeID)
status[srcNodeID] = 1
# label correcting
while selist:
i = GetNextNodeID(selist, dist)
selist.remove(i)
status[i] = 0
node = GetNode(i)
for linkID in node.GetOutgoingLinks():
link = GetLink(linkID)
j = link.GetDestNodeID()
if dist[j] > dist[i] + link.GetLen():
dist[j] = dist[i] + link.GetLen()
pred[j] = i
if not status[j]:
selist.append(j)
status[j] = 1
def CalculateSSSPDijkstraII(srcNodeID, dist, pred):
""" Minimum Distance Label Implementation using heap
heappop(h) from heapq involves two operations:
1. Find the object with the minimum key from heap h;
2. Delete the object with the minimum key from heap h.
As h is a binary heap, the two operations require O(1) time and O(logn)
time respectively. The overall time complexity of these two operations is
O(logn) compared to O(n) in the implementation without heap.
NOTE that this implementation is DIFFERENT with the standard heap
implementation of Dijkstra's Algorithm as there is no
decrease-key(h, newval, i) in heaqp from Python STL to reduce the key of an
object i from its current value to newval.
Omitting decrease-key(h, newval, i) WOULD NOT affect the correctness of the
implementation.
See https://github.com/jdlph/Path4GMNS for more efficient implementation
"""
dist[srcNodeID] = 0
# heap
selist = []
heapq.heapify(selist)
heapq.heappush(selist, (dist[srcNodeID], srcNodeID))
# label correcting
while selist:
(k, i) = heapq.heappop(selist)
node = GetNode(i)
for linkID in node.GetOutgoingLinks():
link = GetLink(linkID)
j = link.GetDestNodeID()
if dist[j] > k + link.GetLen():
dist[j] = k + link.GetLen()
pred[j] = i
heapq.heappush(selist, (dist[j], j))
def CalculateAPSP(method='dij'):
""" All Pair Shortest Paths (APSP) Algorithms.
Please choose one of the three implementations: fifo, deq, dij.
All pair shortest paths can be calculated by:
1. repeated Single-Source Shortest Path Algorithms
2. Floyd-Warshall Algorithm
"""
st = time()
# initialization
global dist_apsp
global pred_apsp
numNode = GetNumNodes()
dist_apsp = [[MAX_LABEL]*numNode for _ in range(numNode)]
pred_apsp = [[-1]*numNode for _ in range(numNode)]
if method.lower().startswith('dij'):
for i in range(numNode):
# CalculateSSSPDijkstraI(i, numNode, dist_apsp[i], pred_apsp[i])
CalculateSSSPDijkstraII(i, dist_apsp[i], pred_apsp[i])
elif method.lower().startswith('deq'):
for i in range(numNode):
# CalculateSSSPDEQI(i, numNode, dist_apsp[i], pred_apsp[i])
# CalculateSSSPDEQII(i, numNode, dist_apsp[i], pred_apsp[i])
CalculateSSSPDEQIII(i, numNode, dist_apsp[i], pred_apsp[i])
elif method.lower().startswith('fifo'):
for i in range(numNode):
CalculateSSSPFIFOI(i, dist_apsp[i], pred_apsp[i])
# CalculateSSSPFIFOII(i, numNode, dist_apsp[i], pred_apsp[i])
elif method.lower().startswith('fw'):
# do nothing
print("not implemented yet")
else:
raise Exception('Please choose correct shortest path algorithm: '
+'dij; deq; fifo; fw.')
print('Processing time for SPP\t: {0: .2f}'.format(time() - st)+' s.')