-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph2py.py
588 lines (409 loc) · 17.2 KB
/
graph2py.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import numpy as np
import bisect as bis
import heapq as hpq
from sys import getsizeof
from collections import deque
def getIndex(L,x):
i = 0
while L[i] != x:
i += 1
return i
def getSrtdSeqMedian(seq):
n = len(seq)
if n % 2:
return seq[n // 2]
else:
return (seq[n // 2] + seq[(n // 2) - 1]) / 2
# ---------------------------- 1st Class ------------------------------
class AbstractGraph:
def __init__(self, filename):
f = open(filename, 'r')
n_nodes = int(f.readline())
self._initialize(n_nodes)
self.n_nodes = n_nodes
self.n_edges = 0
for l in f:
self._update(l)
f.close()
self._finalize()
def _initialize(self, n_nodes):
self.graph = self._emptygraph(n_nodes)
def _update(self, l):
v,u = l.split()
v = int(v)
u = int(u)
self._addedge(v, u)
def _getdegrees(self):
degrees = []
for v in range(self.n_nodes):
d = self._getdegree(v + 1)
bis.insort(degrees, (d,v))
degrees = list(zip(*degrees))
return degrees
def _finalize(self):
self._savedegreeinfo()
def _savedegreeinfo(self):
degrees = self._getdegrees()
self.degree_min = degrees[0][0],degrees[1][0]
self.degree_median = getSrtdSeqMedian(degrees[0]),getSrtdSeqMedian(degrees[1])
self.degree_max = degrees[0][-1],degrees[1][-1]
self.degree_mean = 2*self.n_edges/self.n_nodes
def _writedegreeinfo(self, f):
f.write('Número de vértices = {}\n'.format(self.n_nodes))
f.write('Número de arestas = {}\n'.format(self.n_edges))
f.write('Grau mínimo = {}\n'.format(self.degree_min))
f.write('Grau mediano = {}\n'.format(self.degree_median))
f.write('Grau máximo = {}\n'.format(self.degree_max))
f.write('Grau médio = {}\n'.format(self.degree_mean))
def BFS(self, root, filename = None):
' breadth first search '
n_nodes = self.n_nodes
parent = np.full(n_nodes, -1, int)
level = np.full(n_nodes, -1, int)
queue = deque()
queue.append(root)
level[root - 1] = 0
while len(queue) >= 1:
v = queue.popleft()
neighbors = self._getneighbors(v)
for u in neighbors:
if level[u - 1] == -1:
level[u - 1] = level[v - 1] + 1
parent[u - 1] = v
queue.append(u)
if filename:
with open(filename, 'w') as f:
bfs_tree = [(i,j) for i,j in zip(parent,level)]
f.write(repr(n_nodes) + '\n')
for node in bfs_tree:
f.write(repr(node) + '\n')
return parent,level
def DFS(graph, root, filename = None):
' depth first search '
n_nodes = graph.n_nodes
parent = np.full(n_nodes, -1, int)
level = np.full(n_nodes, -1, int)
discovered = np.full(n_nodes, False)
stack = [root]
discovered[root - 1] = True
level[root - 1] = 0
while len(stack) >= 1:
v = stack.pop(0)
for u in range(n_nodes)[::-1]:
if graph._isedge(v, u + 1) and not discovered[u]:
discovered[u] = True
stack.append(u + 1)
if filename:
with open(filename, 'w') as f:
dfs_tree = [(i,j) for i,j in zip(parent,level)]
f.write(repr(n_nodes) + '\n')
for node in dfs_tree:
f.write(repr(node) + '\n')
return parent,level
def ConnectedComponents(self, filename = None):
discovered = 0
n_nodes = self.n_nodes
parent = np.full(n_nodes, -1, int)
level = np.full(n_nodes, -1, int)
component = np.full(n_nodes, -1, int)
components = []
while discovered < n_nodes:
root = getIndex(component, -1)
queue = deque()
queue.append(root + 1)
level[root] = 0
component[root] = root
discovered += 1
this = deque()
this.append(root)
while len(queue) >= 1:
v = queue.popleft()
neighbors = self._getneighbors(v)
for u in neighbors:
if level[u - 1] == -1:
discovered += 1
level[u - 1] = level[v - 1] + 1
parent[u - 1] = v
component[u - 1] = root
this.append(u - 1)
queue.append(u)
this.appendleft(len(this))
bis.insort(components, this)
if filename:
with open(filename, 'w') as f:
f.write(str(len(components)) + '\n \n')
for c in components[::-1]:
for l in c:
f.write(str(l) + '\n')
f.write('\n')
f.write('\n')
return discovered, parent, level, component
def Diameter(self):
diameter = 0
for v in range(self.n_nodes):
temp = max(self.BFS(v+1)[1])
diameter = max(temp, diameter)
return diameter
def PseudoDiameter(self):
v = np.random.randint(1,self.n_nodes+1)
bfs_v = self.BFS(v)[1]
u = np.argmax(bfs_v) + 1
pseudo_diam = [0,0,bfs_v[u - 1]]
while pseudo_diam[-1] > pseudo_diam[-2] and pseudo_diam[-2] >= pseudo_diam[-3]:
bfs_u = self.BFS(self,u)[1]
u = np.argmax(bfs_u) + 1
pseudo_diam += [bfs_u[u - 1]]
return pseudo_diam[-1]
def save(self, filename):
f = open(filename, 'w')
self._writedegreeinfo(f)
f.close()
# ---------------------------- 2nd Class ------------------------------
class ArrayGraph(AbstractGraph):
def _emptygraph(self, n_nodes):
return np.full((n_nodes, n_nodes), False, dtype=bool)
def _getdegree(self, v): # Use _isedge
d = 0
for u in range(self.n_nodes):
if self._isedge(v, u + 1):
d += 1
return d
def _addedge(self, v, u):
if not (self.graph[v - 1, u - 1] and self.graph[u - 1, v - 1]):
self.graph[v - 1, u - 1] = True
self.graph[u - 1, v - 1] = True
self.n_edges += 1
def _isedge(self, v, u):
return self.graph[v - 1, u - 1] and self.graph[v - 1, u - 1]
def _getneighbors(self, v): # Use _isedge
return [u + 1 for u in range(self.n_nodes) if self._isedge(v, u + 1)]
# ---------------------------- 3rd Class ------------------------------
class ListGraph(AbstractGraph):
def _emptygraph(self, n_nodes):
return [[] for _ in range(n_nodes)]
def _addedge(self, v, u):
v_edges = self.graph[v - 1]
u_edges = self.graph[u - 1]
if not self._isedge(v, u):
bis.insort(v_edges, u)
bis.insort(u_edges, v)
self.n_edges += 1
def _finalize(self):
self._casttondarray()
self._savedegreeinfo()
def _casttondarray(self):
for i in range(len(self.graph)):
self.graph[i] = np.array(self.graph[i])
self.graph = np.array(self.graph)
def _getdegree(self, v):
d = len(self.graph[v - 1])
return d
def _isedge(self, v, u):
return (u in self.graph[v - 1]) and (v in self.graph[u - 1])
def _getneighbors(self, v):
return self.graph[v - 1]
# ---------------------------- 4th Class ------------------------------
class AbstractWeightedGraph(AbstractGraph):
totalweight = 0
def _update(self, l):
v,u,w = l.split()
v = int(v)
u = int(u)
w = float(w)
if not self._isedge(u,v):
self.totalweight += w
self._addedge(v, u, w)
def _initialize(self, n_nodes):
self.graph = self._emptygraph(n_nodes)
self.weights = self._emptyweights(n_nodes)
def Dijkstra(self, root, target = False, path = False):
n_nodes = self.n_nodes
distance = np.full(n_nodes, np.Inf, float)
parent = np.full(n_nodes, -1, int)
explored = np.full(n_nodes, False, bool)
parent[root - 1] = root
distance[root - 1] = 0
priority_queue = []
hpq.heappush(priority_queue, (0, root))
while len(priority_queue) >= 1:
d, v = hpq.heappop(priority_queue)
if not explored[v - 1]:
explored[v - 1] = True
neighbors = self._getneighbors(v)
for u in neighbors:
w_vu = self._getweight(v, u)
if w_vu < 0:
raise Exception("Edge ({}, {}) has negative weight.".format(v,u))
if distance[u - 1] > distance[v - 1] + w_vu:
distance[u - 1] = distance[v - 1] + w_vu
parent[u - 1] = v
hpq.heappush(priority_queue, (distance[u - 1], u))
if target:
if path == True:
u = target
caminho = [u]
if parent[u - 1] == -1:
raise Exception("There is no path between ({} and {})." .format(root,target))
while(parent[u - 1] != root) :
u = parent[u - 1]
caminho.append(u)
caminho.append(root)
return distance[target-1],caminho[::-1]
return distance[target-1]
return distance, parent
def MSTPrim2(self, root, filename = None):
n_nodes = self.n_nodes
costs = np.full(n_nodes, np.Inf, float)
parent = np.full(n_nodes, -1, int)
explored = np.full(n_nodes, False, bool)
parent[root - 1] = root
costs[root - 1] = 0
priority_queue = []
hpq.heappush(priority_queue, (0, root))
while len(priority_queue) >= 1:
w, v = hpq.heappop(priority_queue)
if not explored[v - 1]:
explored[v - 1] = True
v_neighbors = self._getneighbors(v)
for u in v_neighbors:
w_uv = self._getweight(u, v)
if costs[u - 1] > w_uv and explored[u-1] == False:
parent[u - 1] = v
costs[u - 1] = w_uv
hpq.heappush(priority_queue, (costs[u - 1], u))
if filename == True:
pass
return costs, sum(costs)
def MSTPrim(self, root, filename = None):
n_nodes = self.n_nodes
costs = np.full(n_nodes, np.Inf, float)
explored = np.full(n_nodes, False, bool)
costs[root - 1] = 0
priority_queue = []
hpq.heappush(priority_queue, (0, root))
if filename is not None:
parent = np.full(n_nodes, -1, int)
parent[root - 1] = root
with open(filename + ".txt", 'w') as f:
f.write(str(n_nodes) + "\n")
while len(priority_queue) >= 1:
w, v = hpq.heappop(priority_queue)
if not explored[v - 1]:
explored[v - 1] = True
neighbors = self._getneighbors(v)
p = parent[v - 1]
for u in neighbors:
w_uv = self._getweight(u, v)
if u == p and p != v:
f.write(str(p) + " " + str(v) + " " + str(w_uv) + " " + "\n")
if costs[u - 1] > w_uv and explored[u - 1] == False:
parent[u - 1] = v
costs[u - 1] = w_uv
hpq.heappush(priority_queue, (costs[u - 1], u))
else:
while len(priority_queue) >= 1:
w, v = hpq.heappop(priority_queue)
if not explored[v - 1]:
explored[v - 1] = True
v_neighbors = self._getneighbors(v)
for u in v_neighbors:
w_uv = self._getweight(u, v)
if costs[u - 1] > w_uv and explored[u-1] == False:
costs[u - 1] = w_uv
hpq.heappush(priority_queue, (costs[u - 1], u))
return costs, sum(costs)
def Eccentricity_slow(self, root):
# Do Bellman-Ford then take max
n_nodes = self.n_nodes
distance = np.full(n_nodes, np.Inf, float)
distance[root - 1] = 0.
for length in range(1, n_nodes):
for v in range(1, n_nodes + 1):
neighbors = self._getneighbors(v)
for u in neighbors:
distance[v - 1] = min(distance[v - 1], distance[u - 1] + self._getweight(v, u))
verification = np.copy(distance)
for v in range(1, n_nodes + 1):
neighbors = self._getneighbors(v)
for u in neighbors:
verification[v - 1] = min(verification[v - 1], verification[u - 1] + self._getweight(v, u))
if not np.array_equal(distance, verification):
raise Exception("Graph contains negative cycle.")
return max(distance)
def Eccentricity(self, root):
n_nodes = self.n_nodes
distance = np.full(n_nodes, np.Inf, float)
explored = np.full(n_nodes, False, bool)
distance[root - 1] = 0
priority_queue = []
hpq.heappush(priority_queue, (0, root))
while len(priority_queue) >= 1:
d, v = hpq.heappop(priority_queue)
if not explored[v - 1]:
explored[v - 1] = True
neighbors = self._getneighbors(v)
for u in neighbors:
w_vu = self._getweight(v, u)
if w_vu < 0:
raise Exception("Edge ({}, {}) has negative weight.".format(v,u))
if distance[u - 1] > distance[v - 1] + w_vu:
distance[u - 1] = distance[v - 1] + w_vu
hpq.heappush(priority_queue, (distance[u - 1], u))
return max(distance)
def Diameter(self):
raise Exception("Undefined for Weighted Graphs.")
def PseudoDiameter(self):
raise Exception("Undefined for Weighted Graphs.")
def BFS(self, root, filename = None):
raise Exception("Undefined for Weighted Graphs.")
def DFS(self, root, filename = None):
raise Exception("Undefined for Weighted Graphs.")
# ---------------------------- 5th Class ------------------------------
class WeightedArrayGraph(ArrayGraph, AbstractWeightedGraph):
def _emptygraph(self, n_nodes):
return np.full((n_nodes, n_nodes), np.NaN, dtype=float)
def _emptyweights(self, n_nodes):
return self.graph
def _addedge(self, v, u, w):
if not self._isedge(v, u):
self.graph[v - 1, u - 1] = w
self.graph[u - 1, v - 1] = w
self.n_edges += 1
def _isedge(self, v, u):
return (not np.isnan(self.graph[v - 1, u - 1])) and (not np.isnan(self.graph[v - 1, u - 1]))
def _getweight(self, v, u):
return self.weights[v - 1, u - 1]
# ---------------------------- 6th Class ------------------------------
class WeightedListGraph(ListGraph, AbstractWeightedGraph):
def _emptygraph(self, n_nodes):
return [[] for _ in range(n_nodes)]
def _emptyweights(self, n_nodes):
return [[] for _ in range(n_nodes)]
def _addedge(self, v, u, w):
v_edges = self.graph[v - 1]
u_edges = self.graph[u - 1]
v_weights = self.weights[v - 1]
u_weights = self.weights[u - 1]
if not self._isedge(v, u):
bis.insort(v_edges, u)
bis.insort(u_edges, v)
self.n_edges += 1
u_idx = getIndex(v_edges, u)
v_idx = getIndex(u_edges, v)
v_weights.insert(u_idx, w)
u_weights.insert(v_idx, w)
def _finalize(self):
self._casttondarray()
self._savedegreeinfo()
def _casttondarray(self):
for i in range(len(self.graph)):
self.graph[i] = np.array(self.graph[i])
self.weights[i] = np.array(self.weights[i])
self.graph = np.array(self.graph)
self.weights = np.array(self.weights)
def _getweight(self, v, u):
if self._isedge(v, u):
u_idx = getIndex(self._getneighbors(v), u)
return self.weights[v - 1][u_idx]
else:
raise Exception("({}, {}) isn't an edge.".format(v, u))