comparison env/lib/python3.9/site-packages/networkx/generators/tests/test_harary_graph.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 """Unit tests for the :mod:`networkx.generators.harary_graph` module.
2 """
3
4 import pytest
5
6 import networkx as nx
7 from networkx.generators.harary_graph import hnm_harary_graph
8 from networkx.generators.harary_graph import hkn_harary_graph
9 from networkx.algorithms.isomorphism.isomorph import is_isomorphic
10
11
12 class TestHararyGraph:
13 """
14 Suppose n nodes, m >= n-1 edges, d = 2m // n, r = 2m % n
15 """
16
17 def test_hnm_harary_graph(self):
18 # When d is even and r = 0, the hnm_harary_graph(n,m) is
19 # the circulant_graph(n, list(range(1,d/2+1)))
20 for (n, m) in [(5, 5), (6, 12), (7, 14)]:
21 G1 = hnm_harary_graph(n, m)
22 d = 2 * m // n
23 G2 = nx.circulant_graph(n, list(range(1, d // 2 + 1)))
24 assert is_isomorphic(G1, G2)
25
26 # When d is even and r > 0, the hnm_harary_graph(n,m) is
27 # the circulant_graph(n, list(range(1,d/2+1)))
28 # with r edges added arbitrarily
29 for (n, m) in [(5, 7), (6, 13), (7, 16)]:
30 G1 = hnm_harary_graph(n, m)
31 d = 2 * m // n
32 G2 = nx.circulant_graph(n, list(range(1, d // 2 + 1)))
33 assert set(G2.edges) < set(G1.edges)
34 assert G1.number_of_edges() == m
35
36 # When d is odd and n is even and r = 0, the hnm_harary_graph(n,m)
37 # is the circulant_graph(n, list(range(1,(d+1)/2) plus [n//2])
38 for (n, m) in [(6, 9), (8, 12), (10, 15)]:
39 G1 = hnm_harary_graph(n, m)
40 d = 2 * m // n
41 L = list(range(1, (d + 1) // 2))
42 L.append(n // 2)
43 G2 = nx.circulant_graph(n, L)
44 assert is_isomorphic(G1, G2)
45
46 # When d is odd and n is even and r > 0, the hnm_harary_graph(n,m)
47 # is the circulant_graph(n, list(range(1,(d+1)/2) plus [n//2])
48 # with r edges added arbitrarily
49 for (n, m) in [(6, 10), (8, 13), (10, 17)]:
50 G1 = hnm_harary_graph(n, m)
51 d = 2 * m // n
52 L = list(range(1, (d + 1) // 2))
53 L.append(n // 2)
54 G2 = nx.circulant_graph(n, L)
55 assert set(G2.edges) < set(G1.edges)
56 assert G1.number_of_edges() == m
57
58 # When d is odd and n is odd, the hnm_harary_graph(n,m) is
59 # the circulant_graph(n, list(range(1,(d+1)/2))
60 # with m - n*(d-1)/2 edges added arbitrarily
61 for (n, m) in [(5, 4), (7, 12), (9, 14)]:
62 G1 = hnm_harary_graph(n, m)
63 d = 2 * m // n
64 L = list(range(1, (d + 1) // 2))
65 G2 = nx.circulant_graph(n, L)
66 assert set(G2.edges) < set(G1.edges)
67 assert G1.number_of_edges() == m
68
69 # Raise NetworkXError if n<1
70 n = 0
71 m = 0
72 pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m)
73
74 # Raise NetworkXError if m < n-1
75 n = 6
76 m = 4
77 pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m)
78
79 # Raise NetworkXError if m > n(n-1)/2
80 n = 6
81 m = 16
82 pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m)
83
84 """
85 Suppose connectivity k, number of nodes n
86 """
87
88 def test_hkn_harary_graph(self):
89 # When k == 1, the hkn_harary_graph(k,n) is
90 # the path_graph(n)
91 for (k, n) in [(1, 6), (1, 7)]:
92 G1 = hkn_harary_graph(k, n)
93 G2 = nx.path_graph(n)
94 assert is_isomorphic(G1, G2)
95
96 # When k is even, the hkn_harary_graph(k,n) is
97 # the circulant_graph(n, list(range(1,k/2+1)))
98 for (k, n) in [(2, 6), (2, 7), (4, 6), (4, 7)]:
99 G1 = hkn_harary_graph(k, n)
100 G2 = nx.circulant_graph(n, list(range(1, k // 2 + 1)))
101 assert is_isomorphic(G1, G2)
102
103 # When k is odd and n is even, the hkn_harary_graph(k,n) is
104 # the circulant_graph(n, list(range(1,(k+1)/2)) plus [n/2])
105 for (k, n) in [(3, 6), (5, 8), (7, 10)]:
106 G1 = hkn_harary_graph(k, n)
107 L = list(range(1, (k + 1) // 2))
108 L.append(n // 2)
109 G2 = nx.circulant_graph(n, L)
110 assert is_isomorphic(G1, G2)
111
112 # When k is odd and n is odd, the hkn_harary_graph(k,n) is
113 # the circulant_graph(n, list(range(1,(k+1)/2))) with
114 # n//2+1 edges added between node i and node i+n//2+1
115 for (k, n) in [(3, 5), (5, 9), (7, 11)]:
116 G1 = hkn_harary_graph(k, n)
117 G2 = nx.circulant_graph(n, list(range(1, (k + 1) // 2)))
118 eSet1 = set(G1.edges)
119 eSet2 = set(G2.edges)
120 eSet3 = set()
121 half = n // 2
122 for i in range(0, half + 1):
123 # add half+1 edges between i and i+half
124 eSet3.add((i, (i + half) % n))
125 assert eSet1 == eSet2 | eSet3
126
127 # Raise NetworkXError if k<1
128 k = 0
129 n = 0
130 pytest.raises(nx.NetworkXError, hkn_harary_graph, k, n)
131
132 # Raise NetworkXError if n<k+1
133 k = 6
134 n = 6
135 pytest.raises(nx.NetworkXError, hkn_harary_graph, k, n)