-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass3.txt~
351 lines (199 loc) · 6.04 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
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