-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxBandwidth.py
More file actions
356 lines (303 loc) · 10.2 KB
/
MaxBandwidth.py
File metadata and controls
356 lines (303 loc) · 10.2 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from MaxHeapV1 import MaxHeap
from Graph import Graph
import timeit
from HeapSort import HeapSort
import yappi
import sys
def MaxBandwidthDijkstraNoHeap(graph, source, destination):
"""
Gives out the maximum bandwidth path between source and destination.
:param graph:
:type graph: Graph
:param source: source vertex
:param destination: destination vertex
:return:
"""
def getFringeMaxBW(bw, fringes):
"""
returns the fringe vertex with max bandwidth
:param bw:
:return:
"""
vertex_with_max_bw = -1
maximum = 0
for vertex in fringes:
if bw[vertex] >= maximum:
vertex_with_max_bw = vertex
maximum = bw[vertex]
return vertex_with_max_bw
# 1.
fringes = []
status = [-1 for i in range(graph.numVertices)] # All vertices are marked as unseen
dad = [0 for i in range(graph.numVertices)]
bw = [0 for i in range(graph.numVertices)]
# 2.
status[source] = 1
# 3.
for w in graph.adjList[source]:
status[w] = 0 # make fringe
fringes.append(w)
dad[w] = source
bw[w] = graph.adjMatrix[source][w]
# 4.
while len(fringes) > 0:
v = getFringeMaxBW(bw, fringes)
status[v] = 1
fringes.remove(v)
for w in graph.adjList[v]:
if status[w] == -1:
status[w] = 0
fringes.append(w)
bw[w] = min(bw[v], graph.adjMatrix[v][w])
dad[w] = v
elif status[w] == 0 and bw[w] < min(bw[v], graph.adjMatrix[v][w]):
bw[w] = min(bw[v], graph.adjMatrix[v][w])
dad[w] = v
# 5.
if status[destination] != 1:
return ([])
else:
mini = sys.maxsize
result = []
x = destination
result.append(x)
# if bw[x] < mini:
# mini = bw[w]
while x != source:
if graph.adjMatrix[x][dad[x]] < mini:
mini = graph.adjMatrix[x][dad[x]]
x = dad[x]
result.append(x)
print(f"Max BW: {mini}")
return result
def MaxBandwidthDijkstraHeap(graph, source, destination):
"""
:param graph:
:type Graph
:param source:
:param destination:
:return:
"""
# 1.
fringes = MaxHeap(graph.numVertices)
status = [-1 for i in range(graph.numVertices)] # All vertices are marked as unseen
dad = [0 for i in range(graph.numVertices)]
bw = [0 for i in range(graph.numVertices)]
# 2.
status[source] = 1
# 3.
for w in graph.adjList[source]:
status[w] = 0 # make fringe
# fringes.append(w)
bw[w] = graph.adjMatrix[source][w]
dad[w] = source
# print(f"inserting {bw[w]}")
fringes.insert(w, bw[w])
# print(fringes.printHeap())
# 4.
while len(fringes) > 0:
v, _ = fringes.maximum()
status[v] = 1
# print(f"deleting {fringes.value_at_position[fringes.position_of_vertex[v]]}")
fringes.delete(v)
# fringes.printHeap()
for w in graph.adjList[v]:
if status[w] == -1:
status[w] = 0
bw[w] = min(bw[v], graph.adjMatrix[v][w])
dad[w] = v
# print(f"inserting {v},{w}, {bw[w]}")
fringes.insert(w, bw[w])
# print(fringes.printHeap())
elif status[w] == 0 and bw[w] < min(bw[v], graph.adjMatrix[v][w]):
bw[w] = min(bw[v], graph.adjMatrix[v][w])
dad[w] = v
# print(f"bw update rule delete {v} {w}")
# print(f"deleting {fringes.value_at_position[fringes.position_of_vertex[w]]}")
fringes.delete(w)
# fringes.printHeap()
# print(f"inserting {bw[w]}")
fringes.insert(w, bw[w])
# 5.
if status[destination] != 1:
return ([])
else:
mini = sys.maxsize
result = []
x = destination
result.append(x)
# if bw[x] < mini:
# mini = bw[w]
while x != source:
if graph.adjMatrix[x][dad[x]] < mini:
mini = graph.adjMatrix[x][dad[x]]
x = dad[x]
result.append(x)
print(f"Max BW: {mini}")
return result
def maxBandwidthKruskals(graph, source, destination):
"""
:param graph:
:type Graph
:param source:
:param destination:
:return:
"""
parentArray = []
heightArray = []
def find(vertex):
w = vertex
while parentArray[w] != -1:
w = parentArray[w]
return w
def makeSet(graph):
"""
:param graph:
:type Graph
:return:
"""
for i in range(graph.numVertices):
parentArray.append(-1)
heightArray.append(0)
def union(r1, r2):
if heightArray[r1] > heightArray[r2]:
parentArray[r2] = r1
elif heightArray[r2] > heightArray[r1]:
parentArray[r1] = r2
else:
parentArray[r2] = r1
heightArray[r1] += 1
# def sortEdgesB(graph):
# start = timeit.default_timer()
# sorted_edges = []
# edges_value_vertices = graph.getEdgesListB()
# for x in range(len(edges_value_vertices) - 1, -1, -1):
# for ver_pair in edges_value_vertices[x]:
# sorted_edges.append(ver_pair)
# end = timeit.default_timer()
# # print(f"time for sorting {end-start}")
# return sorted_edges
def sortEdges(graph):
start = timeit.default_timer()
sorted_edges = []
edges_values, edges_vertices = graph.getEdgesList()
edges = HeapSort(edges_values)
edges_value_sorted, edges_index = edges.heapSort()
for i in range(len(edges_index)):
sorted_edges.append(edges_vertices[edges_index[i]])
end = timeit.default_timer()
# print(f"time taken for sort: {end - start}")
return sorted_edges
# edges_values = []
# edges_vertices = []
# name_index = 0
# for i in range(0, graph.numVertices):
# for j in range(i + 1, graph.numVertices):
# if graph.adjMatrix[i][j] != 0:
# # edges.append([self.adjMatrix[i][j], (i, j)])
# edges_vertices.append((i, j))
# edges_values.append(graph.adjMatrix[i][j])
# edge_heap.insert(name_index, graph.adjMatrix[i][j])
# name_index += 1
# end = timeit.default_timer()
# print(f"heap creation time {end - start}")
# for name, edge_value in enumerate(edges_values):
# edge_heap.insert(name, edge_value)
#
# for i in range(len(edges_values)):
# edge_heap.insert(i, edges_values[i])
# start = timeit.default_timer()
# while len(edge_heap) > 0:
# popped_edge_name, popped_weight = edge_heap.popMax()
# sorted_edges.append(edges_vertices[popped_edge_name])
# # print(f"popping {popped_weight}")
# end = timeit.default_timer()
# print(f"sorting time {end- start}")
# return sorted_edges
def maxSpanningTree(graph):
"""
:param graph:
:type Graph
:return:
"""
spanningTree = [[] for i in range(graph.numVertices)]
sorted_edges = sortEdges(graph)
makeSet(graph)
for i in range(len(sorted_edges)):
u, v = sorted_edges[i]
r1 = find(u)
r2 = find(v)
if r1 != r2:
union(r1, r2)
spanningTree[u].append(v)
spanningTree[v].append(u)
return spanningTree
def buildPath(max_spanning_tree, source, destination, graph):
# bfs
# O(n)
# 0 - > white, 1- gray, -1->black
path = []
parent = [-1 for i in range(len(max_spanning_tree))]
q = []
color = [0 for i in range(len(max_spanning_tree))]
q.append(source)
color[source] = -1
while len(q) > 0:
v = q.pop()
for w in max_spanning_tree[v]:
if color[w] == 0:
q.append(w)
parent[w] = v
color[w] = -1
x = destination
path.append(x)
mini = sys.maxsize
while x != source:
weight = graph.adjMatrix[x][parent[x]]
if weight < mini:
mini = weight
x = parent[x]
path.append(x)
print(f"Max BW : {mini}")
return path
start = timeit.default_timer()
max_spanning_tree = maxSpanningTree(graph)
end = timeit.default_timer()
# print(f"time taken to build spanning tree {end - start}")
# print(max_spanning_tree)
start = timeit.default_timer()
path = buildPath(max_spanning_tree, source, destination,graph)
end = timeit.default_timer()
# print(f"time taken for path building {end - start}")
return path
def main():
source = 3
destination = 1887
start = timeit.default_timer()
sparse_graph = Graph(5000, 1000, 1000)
end = timeit.default_timer()
print(f"Time to build the graph: {end - start}s")
# dense_graph = Graph(5000, 1000, 1000)
start = timeit.default_timer()
path = MaxBandwidthDijkstraNoHeap(sparse_graph, source, destination)
end = timeit.default_timer()
time = end - start
print(f"Time taken for Djikstra without heap: {time}s")
start = timeit.default_timer()
path_1 = MaxBandwidthDijkstraHeap(sparse_graph, source, destination)
end = timeit.default_timer()
print(f"Time taken Djikstra with heap: {(end - start)}s")
# yappi.set_clock_type("cpu") # Use set_clock_type("wall") for wall time
# yappi.start()
start = timeit.default_timer()
path_k = maxBandwidthKruskals(sparse_graph, source, destination)
end = timeit.default_timer()
print(f"Time taken kruskals: {end - start}s")
# yappi.get_func_stats().print_all()
# yappi.get_thread_stats().print_all()
print(f"heap path: {path_1}")
print(f"no heap path: {path}")
print(f"kruskals path: {path_k}")
# print(sparse_graph.adjMatrix)
if __name__ == "__main__":
main()