-
Notifications
You must be signed in to change notification settings - Fork 1
/
deltaStepping.py
203 lines (173 loc) · 6.48 KB
/
deltaStepping.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
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
#!/usr/bin/env python
"""
This code is implementation of sequential Delta Stepping
Author : Prateek Srivastava
Date Created : 09-23-2017
Base DeltaStepping Algorithm Acknowledgements:
Marcin J Zalewski - [email protected]
Paper :
@article{meyer_-stepping:_2003,
title = {Δ-stepping: a parallelizable shortest path algorithm},
volume = {49},
issn = {0196-6774},
shorttitle = {Δ-stepping},
url = {http://www.sciencedirect.com/science/article/pii/S0196677403000762},
doi = {10.1016/S0196-6774(03)00076-2},
"""
from math import floor, sqrt
import networkx as nx
import matplotlib.pyplot as plt
from pprint import pprint
class Algorithm:
"""
"""
def __init__(self):
"""
"""
self.distances = {}
self.delta = 5
self.property_map = {}
self.workItems = []
self.source_vertex = 2
self.infinity = float("inf")
self.totalNodes = 0
self.totalEdges = 0
self.B = {}
def relax(self, w, x):
"""
This function relaxes a bucket i.e. if the distance of a vertex is less than the already existing distance in
the property map then, the vertex is removed from the bucket and reinserted in the new bucket
x is the distance of the vertex and w is the index of the vertex in the property map
"""
# print("w=", w, "x=", x)
if x < self.property_map[w]:
# check if there is an entry of w in the dictionary B
if self.property_map[w] != self.infinity:
if w in self.B[floor(self.property_map[w] / self.delta)]:
# check if the vertex is in the wrong bucket
if floor(x / self.delta) != floor(self.property_map[w] / self.delta):
self.B[floor(self.property_map[w] / self.delta)].remove(w)
self.B[floor(x / self.delta)].append(w)
# if the dictionary entry does not exist
else:
if floor(x / self.delta) not in self.B:
self.B[floor(x / self.delta)] = [w]
else:
if w not in self.B[floor(x / self.delta)]:
self.B[floor(x / self.delta)].append(w)
# update the property map
self.property_map[w] = x
def find_requests(self, vertices, kind, g):
"""
returns a dictionary of neighboring edges with their weights but according to the kind i.e. light or heavy
:param vertices:
:param kind:
:param g:
:return:
"""
tmp = {}
# print("vertices=", vertices, "kind=", kind)
for u in vertices:
for v in g.neighbors(u):
# print(u, self.property_map[u], g.get_edge_data(u, v)['weight'])
edge_weight = self.property_map[u] + g.get_edge_data(u, v)['weight']
if kind == 'light':
if g.get_edge_data(u, v)['weight'] <= self.delta:
if v in tmp:
if edge_weight < tmp[v]:
tmp[v] = edge_weight
else:
tmp[v] = edge_weight
elif kind == 'heavy':
if g.get_edge_data(u, v)['weight'] > self.delta:
if v in tmp:
if edge_weight < tmp[v]:
tmp[v] = edge_weight
else:
tmp[v] = edge_weight
else:
return "Error: No such kind of edges " + kind
# print("tmp=", tmp)
return tmp
def relax_requests(self, request):
"""
:param request:
:return:
"""
for key, value in request.items():
self.relax(key, value)
def delta_stepping(self, g):
"""
This is the main function to implement the algorithm
:param g:
:return:
"""
for node in g.nodes():
self.property_map[node] = self.infinity
self.relax(self.source_vertex, 0)
# print(self.B, self.property_map)
ctr = 0
while self.B:
# print("Parent Iteration=", ctr)
# print("bucket=", self.B)
i = min(self.B.keys())
sub_ctr = 0
r = []
while i in self.B:
# print("Child Iteration=", sub_ctr)
# print("B[i]=", self.B[i])
req = self.find_requests(self.B[i], 'light', g)
# print("req=", req)
r += self.B[i]
del self.B[i]
self.relax_requests(req)
sub_ctr += 1
# print(self.B)
# print("child ends")
# print("r=", r)
req = self.find_requests(r, 'heavy', g)
self.relax_requests(req)
ctr += 1
def validate(self, g):
"""
:param g:
:return:
"""
self.property_map = {k: v for k, v in self.property_map.items() if v != self.infinity}
p = nx.single_source_dijkstra(g, self.source_vertex)
if p[0] == self.property_map:
return True
else:
print("Error: The algorithm is faulty!!!")
for k, v in p[0].items():
if p[0][k] != self.property_map[k]:
print("vertex ", k, " value in ground truth is ", p[0][k], " and value in delta stepping is ",
self.property_map[k])
return False
def main():
make_graph = False
g = nx.read_edgelist('sample2', nodetype=int, data=(('weight', int),), create_using=nx.DiGraph())
print("\nGraph Information..")
print("===================\n")
print(nx.info(g))
print("\nCalculating shortest path..")
a = Algorithm()
a.source_vertex = 2
a.delta_stepping(g)
print("\nValidating Solution..")
if not a.validate(g):
exit(1)
else:
print("\nThe shortest path from ", a.source_vertex, " is:")
pprint(a.property_map)
# visualize the graph
if make_graph:
pos = nx.spring_layout(g, k=5 / sqrt(g.order()))
nx.draw_networkx(g, pos)
edge_labels = dict([((u, v,), d['weight'])
for u, v, d in g.edges(data=True)])
nx.draw_networkx_edge_labels(g, pos=pos, edge_labels=edge_labels, label_pos=0.3, font_size=7)
plt.show(block=False)
plt.savefig("sample1_graph.png")
if __name__ == '__main__':
main()