Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/tests/test_betweenness_centrality.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 import networkx as nx | |
2 from networkx.testing import almost_equal | |
3 | |
4 | |
5 def weighted_G(): | |
6 G = nx.Graph() | |
7 G.add_edge(0, 1, weight=3) | |
8 G.add_edge(0, 2, weight=2) | |
9 G.add_edge(0, 3, weight=6) | |
10 G.add_edge(0, 4, weight=4) | |
11 G.add_edge(1, 3, weight=5) | |
12 G.add_edge(1, 5, weight=5) | |
13 G.add_edge(2, 4, weight=1) | |
14 G.add_edge(3, 4, weight=2) | |
15 G.add_edge(3, 5, weight=1) | |
16 G.add_edge(4, 5, weight=4) | |
17 return G | |
18 | |
19 | |
20 class TestBetweennessCentrality: | |
21 def test_K5(self): | |
22 """Betweenness centrality: K5""" | |
23 G = nx.complete_graph(5) | |
24 b = nx.betweenness_centrality(G, weight=None, normalized=False) | |
25 b_answer = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0} | |
26 for n in sorted(G): | |
27 assert almost_equal(b[n], b_answer[n]) | |
28 | |
29 def test_K5_endpoints(self): | |
30 """Betweenness centrality: K5 endpoints""" | |
31 G = nx.complete_graph(5) | |
32 b = nx.betweenness_centrality(G, weight=None, normalized=False, endpoints=True) | |
33 b_answer = {0: 4.0, 1: 4.0, 2: 4.0, 3: 4.0, 4: 4.0} | |
34 for n in sorted(G): | |
35 assert almost_equal(b[n], b_answer[n]) | |
36 # normalized = True case | |
37 b = nx.betweenness_centrality(G, weight=None, normalized=True, endpoints=True) | |
38 b_answer = {0: 0.4, 1: 0.4, 2: 0.4, 3: 0.4, 4: 0.4} | |
39 for n in sorted(G): | |
40 assert almost_equal(b[n], b_answer[n]) | |
41 | |
42 def test_P3_normalized(self): | |
43 """Betweenness centrality: P3 normalized""" | |
44 G = nx.path_graph(3) | |
45 b = nx.betweenness_centrality(G, weight=None, normalized=True) | |
46 b_answer = {0: 0.0, 1: 1.0, 2: 0.0} | |
47 for n in sorted(G): | |
48 assert almost_equal(b[n], b_answer[n]) | |
49 | |
50 def test_P3(self): | |
51 """Betweenness centrality: P3""" | |
52 G = nx.path_graph(3) | |
53 b_answer = {0: 0.0, 1: 1.0, 2: 0.0} | |
54 b = nx.betweenness_centrality(G, weight=None, normalized=False) | |
55 for n in sorted(G): | |
56 assert almost_equal(b[n], b_answer[n]) | |
57 | |
58 def test_sample_from_P3(self): | |
59 G = nx.path_graph(3) | |
60 b_answer = {0: 0.0, 1: 1.0, 2: 0.0} | |
61 b = nx.betweenness_centrality(G, k=3, weight=None, normalized=False, seed=1) | |
62 for n in sorted(G): | |
63 assert almost_equal(b[n], b_answer[n]) | |
64 b = nx.betweenness_centrality(G, k=2, weight=None, normalized=False, seed=1) | |
65 # python versions give different results with same seed | |
66 b_approx1 = {0: 0.0, 1: 1.5, 2: 0.0} | |
67 b_approx2 = {0: 0.0, 1: 0.75, 2: 0.0} | |
68 for n in sorted(G): | |
69 assert b[n] in (b_approx1[n], b_approx2[n]) | |
70 | |
71 def test_P3_endpoints(self): | |
72 """Betweenness centrality: P3 endpoints""" | |
73 G = nx.path_graph(3) | |
74 b_answer = {0: 2.0, 1: 3.0, 2: 2.0} | |
75 b = nx.betweenness_centrality(G, weight=None, normalized=False, endpoints=True) | |
76 for n in sorted(G): | |
77 assert almost_equal(b[n], b_answer[n]) | |
78 # normalized = True case | |
79 b_answer = {0: 2 / 3, 1: 1.0, 2: 2 / 3} | |
80 b = nx.betweenness_centrality(G, weight=None, normalized=True, endpoints=True) | |
81 for n in sorted(G): | |
82 assert almost_equal(b[n], b_answer[n]) | |
83 | |
84 def test_krackhardt_kite_graph(self): | |
85 """Betweenness centrality: Krackhardt kite graph""" | |
86 G = nx.krackhardt_kite_graph() | |
87 b_answer = { | |
88 0: 1.667, | |
89 1: 1.667, | |
90 2: 0.000, | |
91 3: 7.333, | |
92 4: 0.000, | |
93 5: 16.667, | |
94 6: 16.667, | |
95 7: 28.000, | |
96 8: 16.000, | |
97 9: 0.000, | |
98 } | |
99 for b in b_answer: | |
100 b_answer[b] /= 2 | |
101 b = nx.betweenness_centrality(G, weight=None, normalized=False) | |
102 for n in sorted(G): | |
103 assert almost_equal(b[n], b_answer[n], places=3) | |
104 | |
105 def test_krackhardt_kite_graph_normalized(self): | |
106 """Betweenness centrality: Krackhardt kite graph normalized""" | |
107 G = nx.krackhardt_kite_graph() | |
108 b_answer = { | |
109 0: 0.023, | |
110 1: 0.023, | |
111 2: 0.000, | |
112 3: 0.102, | |
113 4: 0.000, | |
114 5: 0.231, | |
115 6: 0.231, | |
116 7: 0.389, | |
117 8: 0.222, | |
118 9: 0.000, | |
119 } | |
120 b = nx.betweenness_centrality(G, weight=None, normalized=True) | |
121 for n in sorted(G): | |
122 assert almost_equal(b[n], b_answer[n], places=3) | |
123 | |
124 def test_florentine_families_graph(self): | |
125 """Betweenness centrality: Florentine families graph""" | |
126 G = nx.florentine_families_graph() | |
127 b_answer = { | |
128 "Acciaiuoli": 0.000, | |
129 "Albizzi": 0.212, | |
130 "Barbadori": 0.093, | |
131 "Bischeri": 0.104, | |
132 "Castellani": 0.055, | |
133 "Ginori": 0.000, | |
134 "Guadagni": 0.255, | |
135 "Lamberteschi": 0.000, | |
136 "Medici": 0.522, | |
137 "Pazzi": 0.000, | |
138 "Peruzzi": 0.022, | |
139 "Ridolfi": 0.114, | |
140 "Salviati": 0.143, | |
141 "Strozzi": 0.103, | |
142 "Tornabuoni": 0.092, | |
143 } | |
144 | |
145 b = nx.betweenness_centrality(G, weight=None, normalized=True) | |
146 for n in sorted(G): | |
147 assert almost_equal(b[n], b_answer[n], places=3) | |
148 | |
149 def test_les_miserables_graph(self): | |
150 """Betweenness centrality: Les Miserables graph""" | |
151 G = nx.les_miserables_graph() | |
152 b_answer = { | |
153 "Napoleon": 0.000, | |
154 "Myriel": 0.177, | |
155 "MlleBaptistine": 0.000, | |
156 "MmeMagloire": 0.000, | |
157 "CountessDeLo": 0.000, | |
158 "Geborand": 0.000, | |
159 "Champtercier": 0.000, | |
160 "Cravatte": 0.000, | |
161 "Count": 0.000, | |
162 "OldMan": 0.000, | |
163 "Valjean": 0.570, | |
164 "Labarre": 0.000, | |
165 "Marguerite": 0.000, | |
166 "MmeDeR": 0.000, | |
167 "Isabeau": 0.000, | |
168 "Gervais": 0.000, | |
169 "Listolier": 0.000, | |
170 "Tholomyes": 0.041, | |
171 "Fameuil": 0.000, | |
172 "Blacheville": 0.000, | |
173 "Favourite": 0.000, | |
174 "Dahlia": 0.000, | |
175 "Zephine": 0.000, | |
176 "Fantine": 0.130, | |
177 "MmeThenardier": 0.029, | |
178 "Thenardier": 0.075, | |
179 "Cosette": 0.024, | |
180 "Javert": 0.054, | |
181 "Fauchelevent": 0.026, | |
182 "Bamatabois": 0.008, | |
183 "Perpetue": 0.000, | |
184 "Simplice": 0.009, | |
185 "Scaufflaire": 0.000, | |
186 "Woman1": 0.000, | |
187 "Judge": 0.000, | |
188 "Champmathieu": 0.000, | |
189 "Brevet": 0.000, | |
190 "Chenildieu": 0.000, | |
191 "Cochepaille": 0.000, | |
192 "Pontmercy": 0.007, | |
193 "Boulatruelle": 0.000, | |
194 "Eponine": 0.011, | |
195 "Anzelma": 0.000, | |
196 "Woman2": 0.000, | |
197 "MotherInnocent": 0.000, | |
198 "Gribier": 0.000, | |
199 "MmeBurgon": 0.026, | |
200 "Jondrette": 0.000, | |
201 "Gavroche": 0.165, | |
202 "Gillenormand": 0.020, | |
203 "Magnon": 0.000, | |
204 "MlleGillenormand": 0.048, | |
205 "MmePontmercy": 0.000, | |
206 "MlleVaubois": 0.000, | |
207 "LtGillenormand": 0.000, | |
208 "Marius": 0.132, | |
209 "BaronessT": 0.000, | |
210 "Mabeuf": 0.028, | |
211 "Enjolras": 0.043, | |
212 "Combeferre": 0.001, | |
213 "Prouvaire": 0.000, | |
214 "Feuilly": 0.001, | |
215 "Courfeyrac": 0.005, | |
216 "Bahorel": 0.002, | |
217 "Bossuet": 0.031, | |
218 "Joly": 0.002, | |
219 "Grantaire": 0.000, | |
220 "MotherPlutarch": 0.000, | |
221 "Gueulemer": 0.005, | |
222 "Babet": 0.005, | |
223 "Claquesous": 0.005, | |
224 "Montparnasse": 0.004, | |
225 "Toussaint": 0.000, | |
226 "Child1": 0.000, | |
227 "Child2": 0.000, | |
228 "Brujon": 0.000, | |
229 "MmeHucheloup": 0.000, | |
230 } | |
231 | |
232 b = nx.betweenness_centrality(G, weight=None, normalized=True) | |
233 for n in sorted(G): | |
234 assert almost_equal(b[n], b_answer[n], places=3) | |
235 | |
236 def test_ladder_graph(self): | |
237 """Betweenness centrality: Ladder graph""" | |
238 G = nx.Graph() # ladder_graph(3) | |
239 G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5), (3, 5)]) | |
240 b_answer = {0: 1.667, 1: 1.667, 2: 6.667, 3: 6.667, 4: 1.667, 5: 1.667} | |
241 for b in b_answer: | |
242 b_answer[b] /= 2 | |
243 b = nx.betweenness_centrality(G, weight=None, normalized=False) | |
244 for n in sorted(G): | |
245 assert almost_equal(b[n], b_answer[n], places=3) | |
246 | |
247 def test_disconnected_path(self): | |
248 """Betweenness centrality: disconnected path""" | |
249 G = nx.Graph() | |
250 nx.add_path(G, [0, 1, 2]) | |
251 nx.add_path(G, [3, 4, 5, 6]) | |
252 b_answer = {0: 0, 1: 1, 2: 0, 3: 0, 4: 2, 5: 2, 6: 0} | |
253 b = nx.betweenness_centrality(G, weight=None, normalized=False) | |
254 for n in sorted(G): | |
255 assert almost_equal(b[n], b_answer[n]) | |
256 | |
257 def test_disconnected_path_endpoints(self): | |
258 """Betweenness centrality: disconnected path endpoints""" | |
259 G = nx.Graph() | |
260 nx.add_path(G, [0, 1, 2]) | |
261 nx.add_path(G, [3, 4, 5, 6]) | |
262 b_answer = {0: 2, 1: 3, 2: 2, 3: 3, 4: 5, 5: 5, 6: 3} | |
263 b = nx.betweenness_centrality(G, weight=None, normalized=False, endpoints=True) | |
264 for n in sorted(G): | |
265 assert almost_equal(b[n], b_answer[n]) | |
266 # normalized = True case | |
267 b = nx.betweenness_centrality(G, weight=None, normalized=True, endpoints=True) | |
268 for n in sorted(G): | |
269 assert almost_equal(b[n], b_answer[n] / 21) | |
270 | |
271 def test_directed_path(self): | |
272 """Betweenness centrality: directed path""" | |
273 G = nx.DiGraph() | |
274 nx.add_path(G, [0, 1, 2]) | |
275 b = nx.betweenness_centrality(G, weight=None, normalized=False) | |
276 b_answer = {0: 0.0, 1: 1.0, 2: 0.0} | |
277 for n in sorted(G): | |
278 assert almost_equal(b[n], b_answer[n]) | |
279 | |
280 def test_directed_path_normalized(self): | |
281 """Betweenness centrality: directed path normalized""" | |
282 G = nx.DiGraph() | |
283 nx.add_path(G, [0, 1, 2]) | |
284 b = nx.betweenness_centrality(G, weight=None, normalized=True) | |
285 b_answer = {0: 0.0, 1: 0.5, 2: 0.0} | |
286 for n in sorted(G): | |
287 assert almost_equal(b[n], b_answer[n]) | |
288 | |
289 | |
290 class TestWeightedBetweennessCentrality: | |
291 def test_K5(self): | |
292 """Weighted betweenness centrality: K5""" | |
293 G = nx.complete_graph(5) | |
294 b = nx.betweenness_centrality(G, weight="weight", normalized=False) | |
295 b_answer = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0} | |
296 for n in sorted(G): | |
297 assert almost_equal(b[n], b_answer[n]) | |
298 | |
299 def test_P3_normalized(self): | |
300 """Weighted betweenness centrality: P3 normalized""" | |
301 G = nx.path_graph(3) | |
302 b = nx.betweenness_centrality(G, weight="weight", normalized=True) | |
303 b_answer = {0: 0.0, 1: 1.0, 2: 0.0} | |
304 for n in sorted(G): | |
305 assert almost_equal(b[n], b_answer[n]) | |
306 | |
307 def test_P3(self): | |
308 """Weighted betweenness centrality: P3""" | |
309 G = nx.path_graph(3) | |
310 b_answer = {0: 0.0, 1: 1.0, 2: 0.0} | |
311 b = nx.betweenness_centrality(G, weight="weight", normalized=False) | |
312 for n in sorted(G): | |
313 assert almost_equal(b[n], b_answer[n]) | |
314 | |
315 def test_krackhardt_kite_graph(self): | |
316 """Weighted betweenness centrality: Krackhardt kite graph""" | |
317 G = nx.krackhardt_kite_graph() | |
318 b_answer = { | |
319 0: 1.667, | |
320 1: 1.667, | |
321 2: 0.000, | |
322 3: 7.333, | |
323 4: 0.000, | |
324 5: 16.667, | |
325 6: 16.667, | |
326 7: 28.000, | |
327 8: 16.000, | |
328 9: 0.000, | |
329 } | |
330 for b in b_answer: | |
331 b_answer[b] /= 2 | |
332 | |
333 b = nx.betweenness_centrality(G, weight="weight", normalized=False) | |
334 | |
335 for n in sorted(G): | |
336 assert almost_equal(b[n], b_answer[n], places=3) | |
337 | |
338 def test_krackhardt_kite_graph_normalized(self): | |
339 """Weighted betweenness centrality: | |
340 Krackhardt kite graph normalized | |
341 """ | |
342 G = nx.krackhardt_kite_graph() | |
343 b_answer = { | |
344 0: 0.023, | |
345 1: 0.023, | |
346 2: 0.000, | |
347 3: 0.102, | |
348 4: 0.000, | |
349 5: 0.231, | |
350 6: 0.231, | |
351 7: 0.389, | |
352 8: 0.222, | |
353 9: 0.000, | |
354 } | |
355 b = nx.betweenness_centrality(G, weight="weight", normalized=True) | |
356 | |
357 for n in sorted(G): | |
358 assert almost_equal(b[n], b_answer[n], places=3) | |
359 | |
360 def test_florentine_families_graph(self): | |
361 """Weighted betweenness centrality: | |
362 Florentine families graph""" | |
363 G = nx.florentine_families_graph() | |
364 b_answer = { | |
365 "Acciaiuoli": 0.000, | |
366 "Albizzi": 0.212, | |
367 "Barbadori": 0.093, | |
368 "Bischeri": 0.104, | |
369 "Castellani": 0.055, | |
370 "Ginori": 0.000, | |
371 "Guadagni": 0.255, | |
372 "Lamberteschi": 0.000, | |
373 "Medici": 0.522, | |
374 "Pazzi": 0.000, | |
375 "Peruzzi": 0.022, | |
376 "Ridolfi": 0.114, | |
377 "Salviati": 0.143, | |
378 "Strozzi": 0.103, | |
379 "Tornabuoni": 0.092, | |
380 } | |
381 | |
382 b = nx.betweenness_centrality(G, weight="weight", normalized=True) | |
383 for n in sorted(G): | |
384 assert almost_equal(b[n], b_answer[n], places=3) | |
385 | |
386 def test_les_miserables_graph(self): | |
387 """Weighted betweenness centrality: Les Miserables graph""" | |
388 G = nx.les_miserables_graph() | |
389 b_answer = { | |
390 "Napoleon": 0.000, | |
391 "Myriel": 0.177, | |
392 "MlleBaptistine": 0.000, | |
393 "MmeMagloire": 0.000, | |
394 "CountessDeLo": 0.000, | |
395 "Geborand": 0.000, | |
396 "Champtercier": 0.000, | |
397 "Cravatte": 0.000, | |
398 "Count": 0.000, | |
399 "OldMan": 0.000, | |
400 "Valjean": 0.454, | |
401 "Labarre": 0.000, | |
402 "Marguerite": 0.009, | |
403 "MmeDeR": 0.000, | |
404 "Isabeau": 0.000, | |
405 "Gervais": 0.000, | |
406 "Listolier": 0.000, | |
407 "Tholomyes": 0.066, | |
408 "Fameuil": 0.000, | |
409 "Blacheville": 0.000, | |
410 "Favourite": 0.000, | |
411 "Dahlia": 0.000, | |
412 "Zephine": 0.000, | |
413 "Fantine": 0.114, | |
414 "MmeThenardier": 0.046, | |
415 "Thenardier": 0.129, | |
416 "Cosette": 0.075, | |
417 "Javert": 0.193, | |
418 "Fauchelevent": 0.026, | |
419 "Bamatabois": 0.080, | |
420 "Perpetue": 0.000, | |
421 "Simplice": 0.001, | |
422 "Scaufflaire": 0.000, | |
423 "Woman1": 0.000, | |
424 "Judge": 0.000, | |
425 "Champmathieu": 0.000, | |
426 "Brevet": 0.000, | |
427 "Chenildieu": 0.000, | |
428 "Cochepaille": 0.000, | |
429 "Pontmercy": 0.023, | |
430 "Boulatruelle": 0.000, | |
431 "Eponine": 0.023, | |
432 "Anzelma": 0.000, | |
433 "Woman2": 0.000, | |
434 "MotherInnocent": 0.000, | |
435 "Gribier": 0.000, | |
436 "MmeBurgon": 0.026, | |
437 "Jondrette": 0.000, | |
438 "Gavroche": 0.285, | |
439 "Gillenormand": 0.024, | |
440 "Magnon": 0.005, | |
441 "MlleGillenormand": 0.036, | |
442 "MmePontmercy": 0.005, | |
443 "MlleVaubois": 0.000, | |
444 "LtGillenormand": 0.015, | |
445 "Marius": 0.072, | |
446 "BaronessT": 0.004, | |
447 "Mabeuf": 0.089, | |
448 "Enjolras": 0.003, | |
449 "Combeferre": 0.000, | |
450 "Prouvaire": 0.000, | |
451 "Feuilly": 0.004, | |
452 "Courfeyrac": 0.001, | |
453 "Bahorel": 0.007, | |
454 "Bossuet": 0.028, | |
455 "Joly": 0.000, | |
456 "Grantaire": 0.036, | |
457 "MotherPlutarch": 0.000, | |
458 "Gueulemer": 0.025, | |
459 "Babet": 0.015, | |
460 "Claquesous": 0.042, | |
461 "Montparnasse": 0.050, | |
462 "Toussaint": 0.011, | |
463 "Child1": 0.000, | |
464 "Child2": 0.000, | |
465 "Brujon": 0.002, | |
466 "MmeHucheloup": 0.034, | |
467 } | |
468 | |
469 b = nx.betweenness_centrality(G, weight="weight", normalized=True) | |
470 for n in sorted(G): | |
471 assert almost_equal(b[n], b_answer[n], places=3) | |
472 | |
473 def test_ladder_graph(self): | |
474 """Weighted betweenness centrality: Ladder graph""" | |
475 G = nx.Graph() # ladder_graph(3) | |
476 G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5), (3, 5)]) | |
477 b_answer = {0: 1.667, 1: 1.667, 2: 6.667, 3: 6.667, 4: 1.667, 5: 1.667} | |
478 for b in b_answer: | |
479 b_answer[b] /= 2 | |
480 b = nx.betweenness_centrality(G, weight="weight", normalized=False) | |
481 for n in sorted(G): | |
482 assert almost_equal(b[n], b_answer[n], places=3) | |
483 | |
484 def test_G(self): | |
485 """Weighted betweenness centrality: G""" | |
486 G = weighted_G() | |
487 b_answer = {0: 2.0, 1: 0.0, 2: 4.0, 3: 3.0, 4: 4.0, 5: 0.0} | |
488 b = nx.betweenness_centrality(G, weight="weight", normalized=False) | |
489 for n in sorted(G): | |
490 assert almost_equal(b[n], b_answer[n]) | |
491 | |
492 def test_G2(self): | |
493 """Weighted betweenness centrality: G2""" | |
494 G = nx.DiGraph() | |
495 G.add_weighted_edges_from( | |
496 [ | |
497 ("s", "u", 10), | |
498 ("s", "x", 5), | |
499 ("u", "v", 1), | |
500 ("u", "x", 2), | |
501 ("v", "y", 1), | |
502 ("x", "u", 3), | |
503 ("x", "v", 5), | |
504 ("x", "y", 2), | |
505 ("y", "s", 7), | |
506 ("y", "v", 6), | |
507 ] | |
508 ) | |
509 | |
510 b_answer = {"y": 5.0, "x": 5.0, "s": 4.0, "u": 2.0, "v": 2.0} | |
511 | |
512 b = nx.betweenness_centrality(G, weight="weight", normalized=False) | |
513 for n in sorted(G): | |
514 assert almost_equal(b[n], b_answer[n]) | |
515 | |
516 | |
517 class TestEdgeBetweennessCentrality: | |
518 def test_K5(self): | |
519 """Edge betweenness centrality: K5""" | |
520 G = nx.complete_graph(5) | |
521 b = nx.edge_betweenness_centrality(G, weight=None, normalized=False) | |
522 b_answer = dict.fromkeys(G.edges(), 1) | |
523 for n in sorted(G.edges()): | |
524 assert almost_equal(b[n], b_answer[n]) | |
525 | |
526 def test_normalized_K5(self): | |
527 """Edge betweenness centrality: K5""" | |
528 G = nx.complete_graph(5) | |
529 b = nx.edge_betweenness_centrality(G, weight=None, normalized=True) | |
530 b_answer = dict.fromkeys(G.edges(), 1 / 10) | |
531 for n in sorted(G.edges()): | |
532 assert almost_equal(b[n], b_answer[n]) | |
533 | |
534 def test_C4(self): | |
535 """Edge betweenness centrality: C4""" | |
536 G = nx.cycle_graph(4) | |
537 b = nx.edge_betweenness_centrality(G, weight=None, normalized=True) | |
538 b_answer = {(0, 1): 2, (0, 3): 2, (1, 2): 2, (2, 3): 2} | |
539 for n in sorted(G.edges()): | |
540 assert almost_equal(b[n], b_answer[n] / 6) | |
541 | |
542 def test_P4(self): | |
543 """Edge betweenness centrality: P4""" | |
544 G = nx.path_graph(4) | |
545 b = nx.edge_betweenness_centrality(G, weight=None, normalized=False) | |
546 b_answer = {(0, 1): 3, (1, 2): 4, (2, 3): 3} | |
547 for n in sorted(G.edges()): | |
548 assert almost_equal(b[n], b_answer[n]) | |
549 | |
550 def test_normalized_P4(self): | |
551 """Edge betweenness centrality: P4""" | |
552 G = nx.path_graph(4) | |
553 b = nx.edge_betweenness_centrality(G, weight=None, normalized=True) | |
554 b_answer = {(0, 1): 3, (1, 2): 4, (2, 3): 3} | |
555 for n in sorted(G.edges()): | |
556 assert almost_equal(b[n], b_answer[n] / 6) | |
557 | |
558 def test_balanced_tree(self): | |
559 """Edge betweenness centrality: balanced tree""" | |
560 G = nx.balanced_tree(r=2, h=2) | |
561 b = nx.edge_betweenness_centrality(G, weight=None, normalized=False) | |
562 b_answer = {(0, 1): 12, (0, 2): 12, (1, 3): 6, (1, 4): 6, (2, 5): 6, (2, 6): 6} | |
563 for n in sorted(G.edges()): | |
564 assert almost_equal(b[n], b_answer[n]) | |
565 | |
566 | |
567 class TestWeightedEdgeBetweennessCentrality: | |
568 def test_K5(self): | |
569 """Edge betweenness centrality: K5""" | |
570 G = nx.complete_graph(5) | |
571 b = nx.edge_betweenness_centrality(G, weight="weight", normalized=False) | |
572 b_answer = dict.fromkeys(G.edges(), 1) | |
573 for n in sorted(G.edges()): | |
574 assert almost_equal(b[n], b_answer[n]) | |
575 | |
576 def test_C4(self): | |
577 """Edge betweenness centrality: C4""" | |
578 G = nx.cycle_graph(4) | |
579 b = nx.edge_betweenness_centrality(G, weight="weight", normalized=False) | |
580 b_answer = {(0, 1): 2, (0, 3): 2, (1, 2): 2, (2, 3): 2} | |
581 for n in sorted(G.edges()): | |
582 assert almost_equal(b[n], b_answer[n]) | |
583 | |
584 def test_P4(self): | |
585 """Edge betweenness centrality: P4""" | |
586 G = nx.path_graph(4) | |
587 b = nx.edge_betweenness_centrality(G, weight="weight", normalized=False) | |
588 b_answer = {(0, 1): 3, (1, 2): 4, (2, 3): 3} | |
589 for n in sorted(G.edges()): | |
590 assert almost_equal(b[n], b_answer[n]) | |
591 | |
592 def test_balanced_tree(self): | |
593 """Edge betweenness centrality: balanced tree""" | |
594 G = nx.balanced_tree(r=2, h=2) | |
595 b = nx.edge_betweenness_centrality(G, weight="weight", normalized=False) | |
596 b_answer = {(0, 1): 12, (0, 2): 12, (1, 3): 6, (1, 4): 6, (2, 5): 6, (2, 6): 6} | |
597 for n in sorted(G.edges()): | |
598 assert almost_equal(b[n], b_answer[n]) | |
599 | |
600 def test_weighted_graph(self): | |
601 eList = [ | |
602 (0, 1, 5), | |
603 (0, 2, 4), | |
604 (0, 3, 3), | |
605 (0, 4, 2), | |
606 (1, 2, 4), | |
607 (1, 3, 1), | |
608 (1, 4, 3), | |
609 (2, 4, 5), | |
610 (3, 4, 4), | |
611 ] | |
612 G = nx.Graph() | |
613 G.add_weighted_edges_from(eList) | |
614 b = nx.edge_betweenness_centrality(G, weight="weight", normalized=False) | |
615 b_answer = { | |
616 (0, 1): 0.0, | |
617 (0, 2): 1.0, | |
618 (0, 3): 2.0, | |
619 (0, 4): 1.0, | |
620 (1, 2): 2.0, | |
621 (1, 3): 3.5, | |
622 (1, 4): 1.5, | |
623 (2, 4): 1.0, | |
624 (3, 4): 0.5, | |
625 } | |
626 for n in sorted(G.edges()): | |
627 assert almost_equal(b[n], b_answer[n]) | |
628 | |
629 def test_normalized_weighted_graph(self): | |
630 eList = [ | |
631 (0, 1, 5), | |
632 (0, 2, 4), | |
633 (0, 3, 3), | |
634 (0, 4, 2), | |
635 (1, 2, 4), | |
636 (1, 3, 1), | |
637 (1, 4, 3), | |
638 (2, 4, 5), | |
639 (3, 4, 4), | |
640 ] | |
641 G = nx.Graph() | |
642 G.add_weighted_edges_from(eList) | |
643 b = nx.edge_betweenness_centrality(G, weight="weight", normalized=True) | |
644 b_answer = { | |
645 (0, 1): 0.0, | |
646 (0, 2): 1.0, | |
647 (0, 3): 2.0, | |
648 (0, 4): 1.0, | |
649 (1, 2): 2.0, | |
650 (1, 3): 3.5, | |
651 (1, 4): 1.5, | |
652 (2, 4): 1.0, | |
653 (3, 4): 0.5, | |
654 } | |
655 norm = len(G) * (len(G) - 1) / 2 | |
656 for n in sorted(G.edges()): | |
657 assert almost_equal(b[n], b_answer[n] / norm) |