Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/generators/tests/test_classic.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 """ | |
2 ==================== | |
3 Generators - Classic | |
4 ==================== | |
5 | |
6 Unit tests for various classic graph generators in generators/classic.py | |
7 """ | |
8 import itertools | |
9 | |
10 import pytest | |
11 import networkx as nx | |
12 from networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic | |
13 from networkx.testing import assert_edges_equal | |
14 from networkx.testing import assert_nodes_equal | |
15 | |
16 is_isomorphic = graph_could_be_isomorphic | |
17 | |
18 | |
19 class TestGeneratorClassic: | |
20 def test_balanced_tree(self): | |
21 # balanced_tree(r,h) is a tree with (r**(h+1)-1)/(r-1) edges | |
22 for r, h in [(2, 2), (3, 3), (6, 2)]: | |
23 t = nx.balanced_tree(r, h) | |
24 order = t.order() | |
25 assert order == (r ** (h + 1) - 1) / (r - 1) | |
26 assert nx.is_connected(t) | |
27 assert t.size() == order - 1 | |
28 dh = nx.degree_histogram(t) | |
29 assert dh[0] == 0 # no nodes of 0 | |
30 assert dh[1] == r ** h # nodes of degree 1 are leaves | |
31 assert dh[r] == 1 # root is degree r | |
32 assert dh[r + 1] == order - r ** h - 1 # everyone else is degree r+1 | |
33 assert len(dh) == r + 2 | |
34 | |
35 def test_balanced_tree_star(self): | |
36 # balanced_tree(r,1) is the r-star | |
37 t = nx.balanced_tree(r=2, h=1) | |
38 assert is_isomorphic(t, nx.star_graph(2)) | |
39 t = nx.balanced_tree(r=5, h=1) | |
40 assert is_isomorphic(t, nx.star_graph(5)) | |
41 t = nx.balanced_tree(r=10, h=1) | |
42 assert is_isomorphic(t, nx.star_graph(10)) | |
43 | |
44 def test_balanced_tree_path(self): | |
45 """Tests that the balanced tree with branching factor one is the | |
46 path graph. | |
47 | |
48 """ | |
49 # A tree of height four has five levels. | |
50 T = nx.balanced_tree(1, 4) | |
51 P = nx.path_graph(5) | |
52 assert is_isomorphic(T, P) | |
53 | |
54 def test_full_rary_tree(self): | |
55 r = 2 | |
56 n = 9 | |
57 t = nx.full_rary_tree(r, n) | |
58 assert t.order() == n | |
59 assert nx.is_connected(t) | |
60 dh = nx.degree_histogram(t) | |
61 assert dh[0] == 0 # no nodes of 0 | |
62 assert dh[1] == 5 # nodes of degree 1 are leaves | |
63 assert dh[r] == 1 # root is degree r | |
64 assert dh[r + 1] == 9 - 5 - 1 # everyone else is degree r+1 | |
65 assert len(dh) == r + 2 | |
66 | |
67 def test_full_rary_tree_balanced(self): | |
68 t = nx.full_rary_tree(2, 15) | |
69 th = nx.balanced_tree(2, 3) | |
70 assert is_isomorphic(t, th) | |
71 | |
72 def test_full_rary_tree_path(self): | |
73 t = nx.full_rary_tree(1, 10) | |
74 assert is_isomorphic(t, nx.path_graph(10)) | |
75 | |
76 def test_full_rary_tree_empty(self): | |
77 t = nx.full_rary_tree(0, 10) | |
78 assert is_isomorphic(t, nx.empty_graph(10)) | |
79 t = nx.full_rary_tree(3, 0) | |
80 assert is_isomorphic(t, nx.empty_graph(0)) | |
81 | |
82 def test_full_rary_tree_3_20(self): | |
83 t = nx.full_rary_tree(3, 20) | |
84 assert t.order() == 20 | |
85 | |
86 def test_barbell_graph(self): | |
87 # number of nodes = 2*m1 + m2 (2 m1-complete graphs + m2-path + 2 edges) | |
88 # number of edges = 2*(nx.number_of_edges(m1-complete graph) + m2 + 1 | |
89 m1 = 3 | |
90 m2 = 5 | |
91 b = nx.barbell_graph(m1, m2) | |
92 assert nx.number_of_nodes(b) == 2 * m1 + m2 | |
93 assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1 | |
94 | |
95 m1 = 4 | |
96 m2 = 10 | |
97 b = nx.barbell_graph(m1, m2) | |
98 assert nx.number_of_nodes(b) == 2 * m1 + m2 | |
99 assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1 | |
100 | |
101 m1 = 3 | |
102 m2 = 20 | |
103 b = nx.barbell_graph(m1, m2) | |
104 assert nx.number_of_nodes(b) == 2 * m1 + m2 | |
105 assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1 | |
106 | |
107 # Raise NetworkXError if m1<2 | |
108 m1 = 1 | |
109 m2 = 20 | |
110 pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2) | |
111 | |
112 # Raise NetworkXError if m2<0 | |
113 m1 = 5 | |
114 m2 = -2 | |
115 pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2) | |
116 | |
117 # nx.barbell_graph(2,m) = nx.path_graph(m+4) | |
118 m1 = 2 | |
119 m2 = 5 | |
120 b = nx.barbell_graph(m1, m2) | |
121 assert is_isomorphic(b, nx.path_graph(m2 + 4)) | |
122 | |
123 m1 = 2 | |
124 m2 = 10 | |
125 b = nx.barbell_graph(m1, m2) | |
126 assert is_isomorphic(b, nx.path_graph(m2 + 4)) | |
127 | |
128 m1 = 2 | |
129 m2 = 20 | |
130 b = nx.barbell_graph(m1, m2) | |
131 assert is_isomorphic(b, nx.path_graph(m2 + 4)) | |
132 | |
133 pytest.raises( | |
134 nx.NetworkXError, nx.barbell_graph, m1, m2, create_using=nx.DiGraph() | |
135 ) | |
136 | |
137 mb = nx.barbell_graph(m1, m2, create_using=nx.MultiGraph()) | |
138 assert_edges_equal(mb.edges(), b.edges()) | |
139 | |
140 def test_binomial_tree(self): | |
141 for n in range(0, 4): | |
142 b = nx.binomial_tree(n) | |
143 assert nx.number_of_nodes(b) == 2 ** n | |
144 assert nx.number_of_edges(b) == (2 ** n - 1) | |
145 | |
146 def test_complete_graph(self): | |
147 # complete_graph(m) is a connected graph with | |
148 # m nodes and m*(m+1)/2 edges | |
149 for m in [0, 1, 3, 5]: | |
150 g = nx.complete_graph(m) | |
151 assert nx.number_of_nodes(g) == m | |
152 assert nx.number_of_edges(g) == m * (m - 1) // 2 | |
153 | |
154 mg = nx.complete_graph(m, create_using=nx.MultiGraph) | |
155 assert_edges_equal(mg.edges(), g.edges()) | |
156 | |
157 g = nx.complete_graph("abc") | |
158 assert_nodes_equal(g.nodes(), ["a", "b", "c"]) | |
159 assert g.size() == 3 | |
160 | |
161 def test_complete_digraph(self): | |
162 # complete_graph(m) is a connected graph with | |
163 # m nodes and m*(m+1)/2 edges | |
164 for m in [0, 1, 3, 5]: | |
165 g = nx.complete_graph(m, create_using=nx.DiGraph) | |
166 assert nx.number_of_nodes(g) == m | |
167 assert nx.number_of_edges(g) == m * (m - 1) | |
168 | |
169 g = nx.complete_graph("abc", create_using=nx.DiGraph) | |
170 assert len(g) == 3 | |
171 assert g.size() == 6 | |
172 assert g.is_directed() | |
173 | |
174 def test_circular_ladder_graph(self): | |
175 G = nx.circular_ladder_graph(5) | |
176 pytest.raises( | |
177 nx.NetworkXError, nx.circular_ladder_graph, 5, create_using=nx.DiGraph | |
178 ) | |
179 mG = nx.circular_ladder_graph(5, create_using=nx.MultiGraph) | |
180 assert_edges_equal(mG.edges(), G.edges()) | |
181 | |
182 def test_circulant_graph(self): | |
183 # Ci_n(1) is the cycle graph for all n | |
184 Ci6_1 = nx.circulant_graph(6, [1]) | |
185 C6 = nx.cycle_graph(6) | |
186 assert_edges_equal(Ci6_1.edges(), C6.edges()) | |
187 | |
188 # Ci_n(1, 2, ..., n div 2) is the complete graph for all n | |
189 Ci7 = nx.circulant_graph(7, [1, 2, 3]) | |
190 K7 = nx.complete_graph(7) | |
191 assert_edges_equal(Ci7.edges(), K7.edges()) | |
192 | |
193 # Ci_6(1, 3) is K_3,3 i.e. the utility graph | |
194 Ci6_1_3 = nx.circulant_graph(6, [1, 3]) | |
195 K3_3 = nx.complete_bipartite_graph(3, 3) | |
196 assert is_isomorphic(Ci6_1_3, K3_3) | |
197 | |
198 def test_cycle_graph(self): | |
199 G = nx.cycle_graph(4) | |
200 assert_edges_equal(G.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)]) | |
201 mG = nx.cycle_graph(4, create_using=nx.MultiGraph) | |
202 assert_edges_equal(mG.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)]) | |
203 G = nx.cycle_graph(4, create_using=nx.DiGraph) | |
204 assert not G.has_edge(2, 1) | |
205 assert G.has_edge(1, 2) | |
206 assert G.is_directed() | |
207 | |
208 G = nx.cycle_graph("abc") | |
209 assert len(G) == 3 | |
210 assert G.size() == 3 | |
211 g = nx.cycle_graph("abc", nx.DiGraph) | |
212 assert len(g) == 3 | |
213 assert g.size() == 3 | |
214 assert g.is_directed() | |
215 | |
216 def test_dorogovtsev_goltsev_mendes_graph(self): | |
217 G = nx.dorogovtsev_goltsev_mendes_graph(0) | |
218 assert_edges_equal(G.edges(), [(0, 1)]) | |
219 assert_nodes_equal(list(G), [0, 1]) | |
220 G = nx.dorogovtsev_goltsev_mendes_graph(1) | |
221 assert_edges_equal(G.edges(), [(0, 1), (0, 2), (1, 2)]) | |
222 assert nx.average_clustering(G) == 1.0 | |
223 assert sorted(nx.triangles(G).values()) == [1, 1, 1] | |
224 G = nx.dorogovtsev_goltsev_mendes_graph(10) | |
225 assert nx.number_of_nodes(G) == 29526 | |
226 assert nx.number_of_edges(G) == 59049 | |
227 assert G.degree(0) == 1024 | |
228 assert G.degree(1) == 1024 | |
229 assert G.degree(2) == 1024 | |
230 | |
231 pytest.raises( | |
232 nx.NetworkXError, | |
233 nx.dorogovtsev_goltsev_mendes_graph, | |
234 7, | |
235 create_using=nx.DiGraph, | |
236 ) | |
237 pytest.raises( | |
238 nx.NetworkXError, | |
239 nx.dorogovtsev_goltsev_mendes_graph, | |
240 7, | |
241 create_using=nx.MultiGraph, | |
242 ) | |
243 | |
244 def test_create_using(self): | |
245 G = nx.empty_graph() | |
246 assert isinstance(G, nx.Graph) | |
247 pytest.raises(TypeError, nx.empty_graph, create_using=0.0) | |
248 pytest.raises(TypeError, nx.empty_graph, create_using="Graph") | |
249 | |
250 G = nx.empty_graph(create_using=nx.MultiGraph) | |
251 assert isinstance(G, nx.MultiGraph) | |
252 G = nx.empty_graph(create_using=nx.DiGraph) | |
253 assert isinstance(G, nx.DiGraph) | |
254 | |
255 G = nx.empty_graph(create_using=nx.DiGraph, default=nx.MultiGraph) | |
256 assert isinstance(G, nx.DiGraph) | |
257 G = nx.empty_graph(create_using=None, default=nx.MultiGraph) | |
258 assert isinstance(G, nx.MultiGraph) | |
259 G = nx.empty_graph(default=nx.MultiGraph) | |
260 assert isinstance(G, nx.MultiGraph) | |
261 | |
262 G = nx.path_graph(5) | |
263 H = nx.empty_graph(create_using=G) | |
264 assert not H.is_multigraph() | |
265 assert not H.is_directed() | |
266 assert len(H) == 0 | |
267 assert G is H | |
268 | |
269 H = nx.empty_graph(create_using=nx.MultiGraph()) | |
270 assert H.is_multigraph() | |
271 assert not H.is_directed() | |
272 assert G is not H | |
273 | |
274 def test_empty_graph(self): | |
275 G = nx.empty_graph() | |
276 assert nx.number_of_nodes(G) == 0 | |
277 G = nx.empty_graph(42) | |
278 assert nx.number_of_nodes(G) == 42 | |
279 assert nx.number_of_edges(G) == 0 | |
280 | |
281 G = nx.empty_graph("abc") | |
282 assert len(G) == 3 | |
283 assert G.size() == 0 | |
284 | |
285 # create empty digraph | |
286 G = nx.empty_graph(42, create_using=nx.DiGraph(name="duh")) | |
287 assert nx.number_of_nodes(G) == 42 | |
288 assert nx.number_of_edges(G) == 0 | |
289 assert isinstance(G, nx.DiGraph) | |
290 | |
291 # create empty multigraph | |
292 G = nx.empty_graph(42, create_using=nx.MultiGraph(name="duh")) | |
293 assert nx.number_of_nodes(G) == 42 | |
294 assert nx.number_of_edges(G) == 0 | |
295 assert isinstance(G, nx.MultiGraph) | |
296 | |
297 # create empty graph from another | |
298 pete = nx.petersen_graph() | |
299 G = nx.empty_graph(42, create_using=pete) | |
300 assert nx.number_of_nodes(G) == 42 | |
301 assert nx.number_of_edges(G) == 0 | |
302 assert isinstance(G, nx.Graph) | |
303 | |
304 def test_ladder_graph(self): | |
305 for i, G in [ | |
306 (0, nx.empty_graph(0)), | |
307 (1, nx.path_graph(2)), | |
308 (2, nx.hypercube_graph(2)), | |
309 (10, nx.grid_graph([2, 10])), | |
310 ]: | |
311 assert is_isomorphic(nx.ladder_graph(i), G) | |
312 | |
313 pytest.raises(nx.NetworkXError, nx.ladder_graph, 2, create_using=nx.DiGraph) | |
314 | |
315 g = nx.ladder_graph(2) | |
316 mg = nx.ladder_graph(2, create_using=nx.MultiGraph) | |
317 assert_edges_equal(mg.edges(), g.edges()) | |
318 | |
319 def test_lollipop_graph(self): | |
320 # number of nodes = m1 + m2 | |
321 # number of edges = nx.number_of_edges(nx.complete_graph(m1)) + m2 | |
322 for m1, m2 in [(3, 5), (4, 10), (3, 20)]: | |
323 b = nx.lollipop_graph(m1, m2) | |
324 assert nx.number_of_nodes(b) == m1 + m2 | |
325 assert nx.number_of_edges(b) == m1 * (m1 - 1) / 2 + m2 | |
326 | |
327 # Raise NetworkXError if m<2 | |
328 pytest.raises(nx.NetworkXError, nx.lollipop_graph, 1, 20) | |
329 | |
330 # Raise NetworkXError if n<0 | |
331 pytest.raises(nx.NetworkXError, nx.lollipop_graph, 5, -2) | |
332 | |
333 # lollipop_graph(2,m) = path_graph(m+2) | |
334 for m1, m2 in [(2, 5), (2, 10), (2, 20)]: | |
335 b = nx.lollipop_graph(m1, m2) | |
336 assert is_isomorphic(b, nx.path_graph(m2 + 2)) | |
337 | |
338 pytest.raises( | |
339 nx.NetworkXError, nx.lollipop_graph, m1, m2, create_using=nx.DiGraph | |
340 ) | |
341 | |
342 mb = nx.lollipop_graph(m1, m2, create_using=nx.MultiGraph) | |
343 assert_edges_equal(mb.edges(), b.edges()) | |
344 | |
345 g = nx.lollipop_graph([1, 2, 3, 4], "abc") | |
346 assert len(g) == 7 | |
347 assert g.size() == 9 | |
348 | |
349 def test_null_graph(self): | |
350 assert nx.number_of_nodes(nx.null_graph()) == 0 | |
351 | |
352 def test_path_graph(self): | |
353 p = nx.path_graph(0) | |
354 assert is_isomorphic(p, nx.null_graph()) | |
355 | |
356 p = nx.path_graph(1) | |
357 assert is_isomorphic(p, nx.empty_graph(1)) | |
358 | |
359 p = nx.path_graph(10) | |
360 assert nx.is_connected(p) | |
361 assert sorted(d for n, d in p.degree()) == [1, 1, 2, 2, 2, 2, 2, 2, 2, 2] | |
362 assert p.order() - 1 == p.size() | |
363 | |
364 dp = nx.path_graph(3, create_using=nx.DiGraph) | |
365 assert dp.has_edge(0, 1) | |
366 assert not dp.has_edge(1, 0) | |
367 | |
368 mp = nx.path_graph(10, create_using=nx.MultiGraph) | |
369 assert_edges_equal(mp.edges(), p.edges()) | |
370 | |
371 G = nx.path_graph("abc") | |
372 assert len(G) == 3 | |
373 assert G.size() == 2 | |
374 g = nx.path_graph("abc", nx.DiGraph) | |
375 assert len(g) == 3 | |
376 assert g.size() == 2 | |
377 assert g.is_directed() | |
378 | |
379 def test_star_graph(self): | |
380 star_graph = nx.star_graph | |
381 assert is_isomorphic(star_graph(0), nx.empty_graph(1)) | |
382 assert is_isomorphic(star_graph(1), nx.path_graph(2)) | |
383 assert is_isomorphic(star_graph(2), nx.path_graph(3)) | |
384 assert is_isomorphic(star_graph(5), nx.complete_bipartite_graph(1, 5)) | |
385 | |
386 s = star_graph(10) | |
387 assert sorted(d for n, d in s.degree()) == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10] | |
388 | |
389 pytest.raises(nx.NetworkXError, star_graph, 10, create_using=nx.DiGraph) | |
390 | |
391 ms = star_graph(10, create_using=nx.MultiGraph) | |
392 assert_edges_equal(ms.edges(), s.edges()) | |
393 | |
394 G = star_graph("abcdefg") | |
395 assert len(G) == 7 | |
396 assert G.size() == 6 | |
397 | |
398 def test_trivial_graph(self): | |
399 assert nx.number_of_nodes(nx.trivial_graph()) == 1 | |
400 | |
401 def test_turan_graph(self): | |
402 assert nx.number_of_edges(nx.turan_graph(13, 4)) == 63 | |
403 assert is_isomorphic( | |
404 nx.turan_graph(13, 4), nx.complete_multipartite_graph(3, 4, 3, 3) | |
405 ) | |
406 | |
407 def test_wheel_graph(self): | |
408 for n, G in [ | |
409 (0, nx.null_graph()), | |
410 (1, nx.empty_graph(1)), | |
411 (2, nx.path_graph(2)), | |
412 (3, nx.complete_graph(3)), | |
413 (4, nx.complete_graph(4)), | |
414 ]: | |
415 g = nx.wheel_graph(n) | |
416 assert is_isomorphic(g, G) | |
417 | |
418 g = nx.wheel_graph(10) | |
419 assert sorted(d for n, d in g.degree()) == [3, 3, 3, 3, 3, 3, 3, 3, 3, 9] | |
420 | |
421 pytest.raises(nx.NetworkXError, nx.wheel_graph, 10, create_using=nx.DiGraph) | |
422 | |
423 mg = nx.wheel_graph(10, create_using=nx.MultiGraph()) | |
424 assert_edges_equal(mg.edges(), g.edges()) | |
425 | |
426 G = nx.wheel_graph("abc") | |
427 assert len(G) == 3 | |
428 assert G.size() == 3 | |
429 | |
430 def test_complete_0_partite_graph(self): | |
431 """Tests that the complete 0-partite graph is the null graph.""" | |
432 G = nx.complete_multipartite_graph() | |
433 H = nx.null_graph() | |
434 assert_nodes_equal(G, H) | |
435 assert_edges_equal(G.edges(), H.edges()) | |
436 | |
437 def test_complete_1_partite_graph(self): | |
438 """Tests that the complete 1-partite graph is the empty graph.""" | |
439 G = nx.complete_multipartite_graph(3) | |
440 H = nx.empty_graph(3) | |
441 assert_nodes_equal(G, H) | |
442 assert_edges_equal(G.edges(), H.edges()) | |
443 | |
444 def test_complete_2_partite_graph(self): | |
445 """Tests that the complete 2-partite graph is the complete bipartite | |
446 graph. | |
447 | |
448 """ | |
449 G = nx.complete_multipartite_graph(2, 3) | |
450 H = nx.complete_bipartite_graph(2, 3) | |
451 assert_nodes_equal(G, H) | |
452 assert_edges_equal(G.edges(), H.edges()) | |
453 | |
454 def test_complete_multipartite_graph(self): | |
455 """Tests for generating the complete multipartite graph.""" | |
456 G = nx.complete_multipartite_graph(2, 3, 4) | |
457 blocks = [(0, 1), (2, 3, 4), (5, 6, 7, 8)] | |
458 # Within each block, no two vertices should be adjacent. | |
459 for block in blocks: | |
460 for u, v in itertools.combinations_with_replacement(block, 2): | |
461 assert v not in G[u] | |
462 assert G.nodes[u] == G.nodes[v] | |
463 # Across blocks, all vertices should be adjacent. | |
464 for (block1, block2) in itertools.combinations(blocks, 2): | |
465 for u, v in itertools.product(block1, block2): | |
466 assert v in G[u] | |
467 assert G.nodes[u] != G.nodes[v] |