forked from amybabay/overlay_topo_evaluation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspanner.py
331 lines (274 loc) · 11.1 KB
/
spanner.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import sys
import argparse
import re
import networkx as nx
from networkx.algorithms.shortest_paths.weighted import dijkstra_path_length, dijkstra_path
from networkx.algorithms.connectivity import node_disjoint_paths
from networkx.algorithms.flow import shortest_augmenting_path
import matplotlib.pyplot as plt
import statistics
from itertools import islice
import numpy as np
graph = {}
E = {}
disjoint = 4
def connectivity(topology):
for u in graph:
for v in graph:
if u != v:
#check that there are 3 paths less than 130 in the complete graph
ignore = []
paths_found = True
direct=False
for p in range(0,3):
length,path = dijkstras(graph,u,v,ignore,direct)
#test with 20ms difference
if length > 110:
paths_found=False
break
for node in path:
if node != u and node != v:
ignore.append(node)
if len(path) == 2:
direct=True
#if the complete graph containts 3 disjoint paths verify that the spanner does
if paths_found:
ignore = []
direct=False
for p in range(0,3):
length,path = dijkstras(topology,u,v,ignore,direct)
if not path:
print("spanner failed for u = " + str(u) + " v = " + str(v))
break
if length > 130:
print("spanner failed for u = " + str(u) + " v = " + str(v))
#break
for node in path:
if node != u and node != v:
ignore.append(node)
if len(path) == 2:
direct=True
#breadth first search with a set of nodes to ignore
def BFS(start, goal,t,ignore,topology):
explored = []
queue = [[start]]
value = {}
for a in graph: value[int(a)]=0
while queue:
path = queue.pop(0)
node = path[-1]
# Condition to check if the
# current node is not visited
if node not in explored:
neighbours = topology[node]
# Loop to iterate over the
# neighbours of the node
for neighbour in neighbours:
##print(graph[node][neighbour])
##print(t)
if value[node] + graph[node][neighbour] <= t and neighbour not in ignore:
if not("direct" in ignore and neighbour == goal and node == start):
new_path = list(path)
new_path.append(neighbour)
queue.append(new_path)
value[neighbour]= value[node] + graph[node][neighbour]
# Condition to check if the
# neighbour node is the goal
if neighbour == goal:
return (new_path, value[neighbour])
explored.append(node)
# Condition when the nodes
# are not connected
return None, 0
#runs dijkstras with a set of nodes to ignore
def dijkstras(topology, start, second, ignore, direct):
best = nx.DiGraph()
for n in topology:
for m in topology[n]:
if m not in ignore and n not in ignore:
if not(n==start and m==second and direct) and not(m==start and n==second and direct):
weight = topology[n][m]
best.add_edge(n, m, weight=weight)
best.add_edge(m, n, weight=weight)
try:
length = dijkstra_path_length(G=best, source=start, target=second, weight="weight")
except nx.NetworkXNoPath:
return 0,None
except nx.NodeNotFound:
return 0,None
path = dijkstra_path(G=best, source=start, target=second, weight="weight")
return length,path
def Max_Flow(topology,start,second,ignore,direct):
best = nx.DiGraph()
for n in topology:
for m in topology[n]:
if m not in ignore and n not in ignore:
if not(n==start and m==second) and not(m==start and n==second):
weight = topology[n][m]
best.add_edge(n, m, weight=weight)
best.add_edge(m, n, weight=weight)
try:
path = list(nx.node_disjoint_paths(best, start, second, flow_func=shortest_augmenting_path,cutoff=disjoint))
except (nx.NetworkXNoPath, nx.NetworkXError) as e :
return 0,None
return path
def Max_weight(path):
Max = 0
for i in path:
result = 0
for j in range(len(i)-1):
result += E[(i[j],i[j+1])]
Max = max(result,Max)
return Max
# k*graph[edge[0]][edge[1]], disjoint, edge[0], edge[1],topology,mode
def LBC(t, disjoint, start, second,topology,mode):
ignore = []
direct=False
#run either bfs and dijkstras a set number of times
if mode ==2:
path = Max_Flow(topology,start,second,ignore,direct)
if len(path) < disjoint or Max_weight(path) > t:
if(len(path) >= disjoint):
print( "add",start, second,path, Max_weight(path),t)
else:
print( "add", start, second,path,t)
return True
else:
print( "dont",start, second,path, Max_weight(path),t)
return False
for i in range(disjoint):
if mode == 0:
result, value = BFS(start,second,t,ignore,topology)
elif mode == 1:
value, result = dijkstras(topology,start,second,ignore,direct)
print(f"{start} -> {second} {result,value}")
if result is None or value > t:
return True
else:
#adding nodes in path to f
print(f"{start} -> {second} :{value} < {t}")
#add nodes into the ignore set
for node in result:
if node != start and node != second:
ignore.append(node)
if len(result) == 2:
ignore.append("direct")
direct = True
return False
def greedy(k, disjoint, topology,mode):
count = 0
#order the edges in terms of length
for edge in sorted(E.items(), key=lambda x: x[1]):
edge=edge[0]
if edge[0] in topology and edge[1] in topology[edge[0]]:
continue
ret = LBC(k*graph[edge[0]][edge[1]], disjoint , edge[0], edge[1],topology,mode)
#if lbc returns true add the edge to topology
if ret == True:
topology[edge[0]][edge[1]] = graph[edge[0]][edge[1]]
topology[edge[1]][edge[0]] = graph[edge[1]][edge[0]]
count = count + 1
return count
def read_positions(pos_file,ignore):
""" Read node positions from the given pos_file. Return a dictionary that
maps each node in the file to a tuple (x,y) with its x,y coordinates """
pos = {}
with open(pos_file) as f:
for line in f:
# each line is like: 1 41.505880 -81.609169 # Case Western
parts = line.split()
node = int(parts[0])
if node in ignore: continue # to ignore any nodes
lat = float(parts[1])
lon = float(parts[2])
pos[node] = (lon, lat)
return pos
def create_graph(input_graph,ignore):
with open(input_graph) as f:
for line in f:
parts = line.split()
if parts != []:
node1 = int(parts[0])
node2 = int(parts[1])
if node1 in ignore or node2 in ignore : continue
weight = int(parts[2])
E[(node1,node2)] = int(weight)
if not(node1 in graph):
graph[node1]={}
graph[node1][node2] = int(weight)
def compute_metrics(topology, k):
connectivity(topology)
total = 0
cnt = 0
for node in topology:
for edge in topology[node]:
cnt = cnt + 1
total = total + topology[node][edge]
average = total/cnt
prev = 0
num = 0
for node in graph:
for edge in graph[node]:
num = num+1
prev = prev + graph[node][edge]
avg = prev/num
print("edge count = " + str(cnt/2))
print("average hop distance " + str(average))
print()
def main(argv):
inputGraph = argv[0]
posGraph = argv[1]
kStart = float(argv[2])
kTries = int(argv[3])
mode = argv[4]
ignore = [int(i) for i in argv[5:]]
print ("--ignore nodes: ",ignore,"--\n")
create_graph(inputGraph,ignore)
pos = read_positions(posGraph,ignore)
if mode == 'b':
mode = 0
elif mode == 'd':
mode = 1
elif mode == "f":
mode = 2
k = kStart
topology = {}
#run on different values of k
for x in range(0, int(kTries)):
for a in graph:
topology[int(a)]={}
print("k value = " + str(k))
# 3 here refers to the number of disjoint paths to find
greedy(k,disjoint,topology,mode)
compute_metrics(topology, k)
#pos = read_positions("12node_pos.txt")
#draws the graph from position graph
draw = nx.DiGraph()
for n in topology:
for m in topology[n]:
weight = topology[n][m]
draw.add_edge(n, m, weight=weight)
draw.add_edge(m, n, weight=weight)
nx.draw_networkx(draw, pos)
plt.show()
#write the spanner to file
if mode == 0:
with open("bfs"+str(round(k, 1))+inputGraph, 'w') as f:
for u in topology:
for v in topology[u]:
f.write(str(u) + " " + str(v) + " " + str(topology[u][v]) + "\n")
elif mode == 1:
with open("dijk"+str(round(k, 1))+inputGraph, 'w') as f:
for u in topology:
for v in topology[u]:
f.write(str(u) + " " + str(v) + " " + str(topology[u][v]) + "\n")
elif mode == 2:
with open("flow"+str(round(k, 1))+inputGraph, 'w') as f:
for u in topology:
for v in topology[u]:
f.write(str(u) + " " + str(v) + " " + str(topology[u][v]) + "\n")
#update the k value
k=k+0.1
k = round(k,1)
if __name__ == "__main__":
main(sys.argv[1:])