-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass3.txt
667 lines (472 loc) · 15.5 KB
/
class3.txt
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
Degrees and Paths in Graphs
Clique:
degree O(n) in relation to the number of nodes
path (longest path from one node to another node) O(1)
Ring:
degree O(1)
path O(n)
Balanced Tree:
degree O(1)
path O(logn)
Hypercube:
degree O(logn)
path O(logn)
CLUSTERING COEFFICIENT
CC(v):
V: a node
Kv: its degree
Nv: number of links between the neighbors of V
CC(v) = (2*Nv)/(Kv*(Kv-1))
This represents the fraction of possible interconnections
It has to be between 0 and 1. 0 if you have a star and 1 if you have a clique
CC(G) (clustering coefficient for a graph) = average of all CC(v)
DEPTH FIRST SEARCH
DFS RECURSIVE:
5.
##################################################################
# Traversal...
# Call this routine on nodes being visited for the first time
def mark_component(G, node, marked):
marked[node] = True
total_marked = 1
for neighbor in G[node]:
if neighbor not in marked:
total_marked += mark_component(G, neighbor, marked)
return total_marked
def check_connection(G, v1, v2):
# Return True if v1 is connected to v2 in G
# or False if otherwise
marked = {}
mark_component(G, v1, marked)
return v2 in marked
def make_link(G, node1, node2):
if node1 not in G:
G[node1] = {}
(G[node1])[node2] = 1
if node2 not in G:
G[node2] = {}
(G[node2])[node1] = 1
return G
def test():
edges = [('a', 'g'), ('a', 'd'), ('g', 'c'), ('g', 'd'),
('b', 'f'), ('f', 'e'), ('e', 'h')]
G = {}
for v1, v2 in edges:
make_link(G, v1, v2)
assert check_connection(G, "a", "c") == True
assert check_connection(G, 'a', 'b') == False
BREADTH FIRST SEARCH
AND DFS NON RECURSIVE (with open list)
FINDING BRIDGE EDGES
1) Build tree out of graph
2) Post-order nodes
3) Compute Number of descendants for each node in the graph (green edges only)
4) Lowest: green/one red
5) Highest: green/one red
6) Bridge edge:
has a green number (#5) that is smaller or equal to black number (post-order nodes, #2) AND the red number (#4) is bigger than blue number (#3) minus black number
HOMEWORK
6.
# Rewrite `mark_component` to not use recursion
# and instead use the `open_list` data structure
# discussed in lecture
#
RECURSIVE VERSION
def mark_component(G, node, marked):
marked[node] = True
total_marked = 1
for neighbor in G[node]:
if neighbor not in marked:
total_marked += mark_component(G, neighbor, marked)
return total_marked
NON RECURSIVE:
# Rewrite `mark_component` to not use recursion
# and instead use the `open_list` data structure
# discussed in lecture
#
def mark_component(G, node, marked):
open_list = [node]
total_marked = 0
while open_list:
current_node = open_list.pop(0)
marked[current_node] = True
total_marked += 1
for neighbor in G[current_node]:
if neighbor in marked:
continue
if neighbor not in open_list:
open_list.append(neighbor)
return total_marked
#########
# Code for testing
#
def make_link(G, node1, node2):
if node1 not in G:
G[node1] = {}
(G[node1])[node2] = 1
if node2 not in G:
G[node2] = {}
(G[node2])[node1] = 1
return G
def test():
test_edges = [(1, 2), (2, 3), (4, 5), (5, 6)]
G = {}
for n1, n2 in test_edges:
make_link(G, n1, n2)
marked = {}
assert mark_component(G, 1, marked) == 3
assert 1 in marked
assert 2 in marked
assert 3 in marked
assert 4 not in marked
assert 5 not in marked
assert 6 not in marked
PROF's answer:
def mark_component(G, node, marked):
open_list = [node]
total_marked = 1
marked[node] = True
while len(open_list) > 0:
node = open_list.pop()
for neighbor in G[node]:
if neighbor not in marked:
open_list.append(neighbor)
marked[neighbor] = True
total_marked += 1
return total_marked
7.
CENTRALITY AVERAGE
def centrality(G,v):
distance_from_start = {}
open_list = [v]
distance_from_start[v] = 0
while len(open_list) > 0:
current = open_list[0]
del open_list[0]
for neighbor in G[current].keys():
if neighbor not in distance_from_start:
distance_from_start[neighbor] = distance_from_start[current] + 1
open_list.append(neighbor)
return (sum(distance_from_start.values())+0.0)/len(distance_from_start)
CENTRALITY MAX
#
# Write centrality_max to return the maximum distance
# from a node to all the other nodes it can reach
#
def centrality_max(G,v):
distance_from_start = {}
open_list = [v]
distance_from_start[v] = 0
while open_list:
current = open_list.pop(0)
for neighbor in G[current]:
if neighbor not in distance_from_start:
distance_from_start[neighbor] = distance_from_start[current] + 1
open_list.append(neighbor)
return max(distance_from_start.values())
#################
# Testing code
#
def make_link(G, node1, node2):
if node1 not in G:
G[node1] = {}
(G[node1])[node2] = 1
if node2 not in G:
G[node2] = {}
(G[node2])[node1] = 1
return G
chain = ((1,2), (2,3), (3,4), (4,5), (5,6))
G = {}
for n1, n2 in chain:
make_link(G, n1, n2)
print centrality_max(G, 1)
def test():
chain = ((1,2), (2,3), (3,4), (4,5), (5,6))
G = {}
for n1, n2 in chain:
make_link(G, n1, n2)
assert centrality_max(G, 1) == 5
assert centrality_max(G, 3) == 3
tree = ((1, 2), (1, 3),
(2, 4), (2, 5),
(3, 6), (3, 7),
(4, 8), (4, 9),
(6, 10), (6, 11))
G = {}
for n1, n2 in tree:
make_link(G, n1, n2)
assert centrality_max(G, 1) == 3
assert centrality_max(G, 11) == 6
8.
FINDING BRIDGE EDGES (REALLY HARD AND COMPLICATED, NEED TO CHECK IT *****)
# Bridge Edges v4
#
# Find the bridge edges in a graph given the
# algorithm in lecture.
# Complete the intermediate steps
# - create_rooted_spanning_tree
# - post_order
# - number_of_descendants
# - lowest_post_order
# - highest_post_order
#
# And then combine them together in
# `bridge_edges`
# So far, we've represented graphs
# as a dictionary where G[n1][n2] == 1
# meant there was an edge between n1 and n2
#
# In order to represent a spanning tree
# we need to create two classes of edges
# we'll refer to them as "green" and "red"
# for the green and red edges as specified in lecture
#
# So, for example, the graph given in lecture
# G = {'a': {'c': 1, 'b': 1},
# 'b': {'a': 1, 'd': 1},
# 'c': {'a': 1, 'd': 1},
# 'd': {'c': 1, 'b': 1, 'e': 1},
# 'e': {'d': 1, 'g': 1, 'f': 1},
# 'f': {'e': 1, 'g': 1},
# 'g': {'e': 1, 'f': 1}
# }
# would be written as a spanning tree
# S = {'a': {'c': 'green', 'b': 'green'},
# 'b': {'a': 'green', 'd': 'red'},
# 'c': {'a': 'green', 'd': 'green'},
# 'd': {'c': 'green', 'b': 'red', 'e': 'green'},
# 'e': {'d': 'green', 'g': 'green', 'f': 'green'},
# 'f': {'e': 'green', 'g': 'red'},
# 'g': {'e': 'green', 'f': 'red'}
# }
#
def make_link(G, node1, node2, r_or_g):
# modified make_link to apply
# a color to the edge instead of just 1
if node1 not in G:
G[node1] = {}
(G[node1])[node2] = r_or_g
if node2 not in G:
G[node2] = {}
(G[node2])[node1] = r_or_g
return G
def create_rooted_spanning_tree(G, root):
# use DFS from the root to add edges and nodes
# to the tree. The first time we see a node
# the edge is green, but after that its red
open_list = [root]
S = {}
while open_list:
node = open_list.pop(0)
neighbors = G[node]
for n in neighbors:
if n not in S:
# we haven't seen this node, so
# need to use a green edge to connect
# it
make_link(S, node, n, 'green')
open_list.append(n)
else:
# we have seen this node,
# but, first make sure that
# don't already have the edge
# in S
if node not in S[n]:
make_link(S, node, n, 'red')
return S
# This is just one possible solution
# There are other ways to create a
# spanning tree, and the grader will
# accept any valid result
# feel free to edit the test to
# match the solution your program produces
def test_create_rooted_spanning_tree():
G = {'a': {'c': 1, 'b': 1},
'b': {'a': 1, 'd': 1},
'c': {'a': 1, 'd': 1},
'd': {'c': 1, 'b': 1, 'e': 1},
'e': {'d': 1, 'g': 1, 'f': 1},
'f': {'e': 1, 'g': 1},
'g': {'e': 1, 'f': 1}
}
S = create_rooted_spanning_tree(G, "a")
assert S == {'a': {'c': 'green', 'b': 'green'},
'b': {'a': 'green', 'd': 'red'},
'c': {'a': 'green', 'd': 'green'},
'd': {'c': 'green', 'b': 'red', 'e': 'green'},
'e': {'d': 'green', 'g': 'green', 'f': 'green'},
'f': {'e': 'green', 'g': 'red'},
'g': {'e': 'green', 'f': 'red'}
}
test_create_rooted_spanning_tree()
###########
def _post_order(S, root, parent, val, po):
left = get_left_child(S, root, parent)
right = get_right_child(S, root, parent, left)
if left:
val = _post_order(S, left, root, val, po)
if right:
val = _post_order(S, right, root, val, po)
po[root] = val
return val + 1
def get_left_child(S, node, parent):
children = [n for n, e in S[node].items() if not n == parent and e == 'green']
if not children:
return None
return min(children)
def get_right_child(S, node, parent, left):
children = [n for n, e in S[node].items() if not n == parent and not n == left and e == 'green']
if not children:
return None
return max(children)
def post_order(S, root):
po = {}
_post_order(S, root, None, 1, po)
return po
# This is just one possible solution
# There are other ways to create a
# spanning tree, and the grader will
# accept any valid result.
# feel free to edit the test to
# match the solution your program produces
def test_post_order():
S = {'a': {'c': 'green', 'b': 'green'},
'b': {'a': 'green', 'd': 'red'},
'c': {'a': 'green', 'd': 'green'},
'd': {'c': 'green', 'b': 'red', 'e': 'green'},
'e': {'d': 'green', 'g': 'green', 'f': 'green'},
'f': {'e': 'green', 'g': 'red'},
'g': {'e': 'green', 'f': 'red'}
}
po = post_order(S, 'a')
assert po == {'a':7, 'b':1, 'c':6, 'd':5, 'e':4, 'f':2, 'g':3}
test_post_order()
##############
def number_of_descendants(S, root):
# return mapping between nodes of S and the number of descendants
# of that node
nd = {}
_number_of_descendants(S, root, nd, None)
return nd
def _number_of_descendants(S, root, nd, parent):
children = get_children(S, root, parent)
num = 1
for c in children:
num += _number_of_descendants(S, c, nd, root)
nd[root] = num
return num
def get_children(S, root, parent):
return [n for n, e in S[root].items() if not n == parent and e == 'green']
def test_number_of_descendants():
S = {'a': {'c': 'green', 'b': 'green'},
'b': {'a': 'green', 'd': 'red'},
'c': {'a': 'green', 'd': 'green'},
'd': {'c': 'green', 'b': 'red', 'e': 'green'},
'e': {'d': 'green', 'g': 'green', 'f': 'green'},
'f': {'e': 'green', 'g': 'red'},
'g': {'e': 'green', 'f': 'red'}
}
nd = number_of_descendants(S, 'a')
assert nd == {'a':7, 'b':1, 'c':5, 'd':4, 'e':3, 'f':1, 'g':1}
test_number_of_descendants()
###############
def _general_post_order(S, root, parent, po, gpo, comp):
green, red = get_children_all(S, root, parent)
val = po[root]
for c in green:
# recursively find the low/high post order value of the children
test = _general_post_order(S, c, root, po, gpo, comp)
# and save the low/highest one
if comp(val, test):
val = test
for c in red:
test = po[c]
# and also look at the direct children
# from following red edges
# and save the low/highest one if needed
if comp(val, test):
val = test
gpo[root] = val
return val
def lowest_post_order(S, root, po):
# return a mapping of the nodes in S
# to the lowest post order value
# below that node
# (and you're allowed to follow 1 red edge)
lpo = {}
_general_post_order(S, root, None, po, lpo, lambda x, y: x>y)
return lpo
def get_children_all(S, root, parent):
"""returns the children from following
green edges and the children from following
red edges"""
green = []
red = []
for n, e in S[root].items():
if n == parent:
continue
if e == 'green':
green.append(n)
if e == 'red':
red.append(n)
return green, red
def test_lowest_post_order():
S = {'a': {'c': 'green', 'b': 'green'},
'b': {'a': 'green', 'd': 'red'},
'c': {'a': 'green', 'd': 'green'},
'd': {'c': 'green', 'b': 'red', 'e': 'green'},
'e': {'d': 'green', 'g': 'green', 'f': 'green'},
'f': {'e': 'green', 'g': 'red'},
'g': {'e': 'green', 'f': 'red'}
}
po = post_order(S, 'a')
l = lowest_post_order(S, 'a', po)
assert l == {'a':1, 'b':1, 'c':1, 'd':1, 'e':2, 'f':2, 'g':2}
test_lowest_post_order()
################
def highest_post_order(S, root, po):
hpo = {}
_general_post_order(S, root, None, po, hpo, lambda x, y: x<y)
return hpo
def test_highest_post_order():
S = {'a': {'c': 'green', 'b': 'green'},
'b': {'a': 'green', 'd': 'red'},
'c': {'a': 'green', 'd': 'green'},
'd': {'c': 'green', 'b': 'red', 'e': 'green'},
'e': {'d': 'green', 'g': 'green', 'f': 'green'},
'f': {'e': 'green', 'g': 'red'},
'g': {'e': 'green', 'f': 'red'}
}
po = post_order(S, 'a')
h = highest_post_order(S, 'a', po)
assert h == {'a':7, 'b':5, 'c':6, 'd':5, 'e':4, 'f':3, 'g':3}
test_highest_post_order()
#################
def bridge_edges(G, root):
rooted_spanning_tree = create_rooted_spanning_tree(G, root)
po = post_order(rooted_spanning_tree, root)
nod = number_of_descendants(rooted_spanning_tree, root)
lpo = lowest_post_order(rooted_spanning_tree, root, po)
hpo = highest_post_order(rooted_spanning_tree, root, po)
bridges = []
open_list = [(root, None)]
while open_list:
node, parent = open_list.pop()
for n in get_children(rooted_spanning_tree, node, parent):
open_list.append((n, node))
if hpo[n] <= po[n] and lpo[n] > po[n] - nod[n]:
bridges.append((node, n))
return bridges
def test_bridge_edges():
G = {'a': {'c': 1, 'b': 1},
'b': {'a': 1, 'd': 1},
'c': {'a': 1, 'd': 1},
'd': {'c': 1, 'b': 1, 'e': 1},
'e': {'d': 1, 'g': 1, 'f': 1},
'f': {'e': 1, 'g': 1},
'g': {'e': 1, 'f': 1}
}
bridges = bridge_edges(G, 'a')
print bridges
assert bridges == [('d', 'e')]
test_bridge_edges()