test_kcomponents.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # Test for Moody and White k-components algorithm
  2. import pytest
  3. import networkx as nx
  4. from networkx.algorithms.connectivity.kcomponents import (
  5. _consolidate,
  6. build_k_number_dict,
  7. )
  8. ##
  9. # A nice synthetic graph
  10. ##
  11. def torrents_and_ferraro_graph():
  12. # Graph from https://arxiv.org/pdf/1503.04476v1 p.26
  13. G = nx.convert_node_labels_to_integers(
  14. nx.grid_graph([5, 5]), label_attribute="labels"
  15. )
  16. rlabels = nx.get_node_attributes(G, "labels")
  17. labels = {v: k for k, v in rlabels.items()}
  18. for nodes in [(labels[(0, 4)], labels[(1, 4)]), (labels[(3, 4)], labels[(4, 4)])]:
  19. new_node = G.order() + 1
  20. # Petersen graph is triconnected
  21. P = nx.petersen_graph()
  22. G = nx.disjoint_union(G, P)
  23. # Add two edges between the grid and P
  24. G.add_edge(new_node + 1, nodes[0])
  25. G.add_edge(new_node, nodes[1])
  26. # K5 is 4-connected
  27. K = nx.complete_graph(5)
  28. G = nx.disjoint_union(G, K)
  29. # Add three edges between P and K5
  30. G.add_edge(new_node + 2, new_node + 11)
  31. G.add_edge(new_node + 3, new_node + 12)
  32. G.add_edge(new_node + 4, new_node + 13)
  33. # Add another K5 sharing a node
  34. G = nx.disjoint_union(G, K)
  35. nbrs = G[new_node + 10]
  36. G.remove_node(new_node + 10)
  37. for nbr in nbrs:
  38. G.add_edge(new_node + 17, nbr)
  39. # This edge makes the graph biconnected; it's
  40. # needed because K5s share only one node.
  41. G.add_edge(new_node + 16, new_node + 8)
  42. for nodes in [(labels[(0, 0)], labels[(1, 0)]), (labels[(3, 0)], labels[(4, 0)])]:
  43. new_node = G.order() + 1
  44. # Petersen graph is triconnected
  45. P = nx.petersen_graph()
  46. G = nx.disjoint_union(G, P)
  47. # Add two edges between the grid and P
  48. G.add_edge(new_node + 1, nodes[0])
  49. G.add_edge(new_node, nodes[1])
  50. # K5 is 4-connected
  51. K = nx.complete_graph(5)
  52. G = nx.disjoint_union(G, K)
  53. # Add three edges between P and K5
  54. G.add_edge(new_node + 2, new_node + 11)
  55. G.add_edge(new_node + 3, new_node + 12)
  56. G.add_edge(new_node + 4, new_node + 13)
  57. # Add another K5 sharing two nodes
  58. G = nx.disjoint_union(G, K)
  59. nbrs = G[new_node + 10]
  60. G.remove_node(new_node + 10)
  61. for nbr in nbrs:
  62. G.add_edge(new_node + 17, nbr)
  63. nbrs2 = G[new_node + 9]
  64. G.remove_node(new_node + 9)
  65. for nbr in nbrs2:
  66. G.add_edge(new_node + 18, nbr)
  67. return G
  68. def test_directed():
  69. with pytest.raises(nx.NetworkXNotImplemented):
  70. G = nx.gnp_random_graph(10, 0.2, directed=True, seed=42)
  71. nx.k_components(G)
  72. # Helper function
  73. def _check_connectivity(G, k_components):
  74. for k, components in k_components.items():
  75. if k < 3:
  76. continue
  77. # check that k-components have node connectivity >= k.
  78. for component in components:
  79. C = G.subgraph(component)
  80. K = nx.node_connectivity(C)
  81. assert K >= k
  82. @pytest.mark.slow
  83. def test_torrents_and_ferraro_graph():
  84. G = torrents_and_ferraro_graph()
  85. result = nx.k_components(G)
  86. _check_connectivity(G, result)
  87. # In this example graph there are 8 3-components, 4 with 15 nodes
  88. # and 4 with 5 nodes.
  89. assert len(result[3]) == 8
  90. assert len([c for c in result[3] if len(c) == 15]) == 4
  91. assert len([c for c in result[3] if len(c) == 5]) == 4
  92. # There are also 8 4-components all with 5 nodes.
  93. assert len(result[4]) == 8
  94. assert all(len(c) == 5 for c in result[4])
  95. @pytest.mark.slow
  96. def test_random_gnp():
  97. G = nx.gnp_random_graph(50, 0.2, seed=42)
  98. result = nx.k_components(G)
  99. _check_connectivity(G, result)
  100. @pytest.mark.slow
  101. def test_shell():
  102. constructor = [(20, 80, 0.8), (80, 180, 0.6)]
  103. G = nx.random_shell_graph(constructor, seed=42)
  104. result = nx.k_components(G)
  105. _check_connectivity(G, result)
  106. def test_configuration():
  107. deg_seq = nx.random_powerlaw_tree_sequence(100, tries=5, seed=72)
  108. G = nx.Graph(nx.configuration_model(deg_seq))
  109. G.remove_edges_from(nx.selfloop_edges(G))
  110. result = nx.k_components(G)
  111. _check_connectivity(G, result)
  112. def test_karate():
  113. G = nx.karate_club_graph()
  114. result = nx.k_components(G)
  115. _check_connectivity(G, result)
  116. def test_karate_component_number():
  117. karate_k_num = {
  118. 0: 4,
  119. 1: 4,
  120. 2: 4,
  121. 3: 4,
  122. 4: 3,
  123. 5: 3,
  124. 6: 3,
  125. 7: 4,
  126. 8: 4,
  127. 9: 2,
  128. 10: 3,
  129. 11: 1,
  130. 12: 2,
  131. 13: 4,
  132. 14: 2,
  133. 15: 2,
  134. 16: 2,
  135. 17: 2,
  136. 18: 2,
  137. 19: 3,
  138. 20: 2,
  139. 21: 2,
  140. 22: 2,
  141. 23: 3,
  142. 24: 3,
  143. 25: 3,
  144. 26: 2,
  145. 27: 3,
  146. 28: 3,
  147. 29: 3,
  148. 30: 4,
  149. 31: 3,
  150. 32: 4,
  151. 33: 4,
  152. }
  153. G = nx.karate_club_graph()
  154. k_components = nx.k_components(G)
  155. k_num = build_k_number_dict(k_components)
  156. assert karate_k_num == k_num
  157. def test_davis_southern_women():
  158. G = nx.davis_southern_women_graph()
  159. result = nx.k_components(G)
  160. _check_connectivity(G, result)
  161. def test_davis_southern_women_detail_3_and_4():
  162. solution = {
  163. 3: [
  164. {
  165. "Nora Fayette",
  166. "E10",
  167. "Myra Liddel",
  168. "E12",
  169. "E14",
  170. "Frances Anderson",
  171. "Evelyn Jefferson",
  172. "Ruth DeSand",
  173. "Helen Lloyd",
  174. "Eleanor Nye",
  175. "E9",
  176. "E8",
  177. "E5",
  178. "E4",
  179. "E7",
  180. "E6",
  181. "E1",
  182. "Verne Sanderson",
  183. "E3",
  184. "E2",
  185. "Theresa Anderson",
  186. "Pearl Oglethorpe",
  187. "Katherina Rogers",
  188. "Brenda Rogers",
  189. "E13",
  190. "Charlotte McDowd",
  191. "Sylvia Avondale",
  192. "Laura Mandeville",
  193. }
  194. ],
  195. 4: [
  196. {
  197. "Nora Fayette",
  198. "E10",
  199. "Verne Sanderson",
  200. "E12",
  201. "Frances Anderson",
  202. "Evelyn Jefferson",
  203. "Ruth DeSand",
  204. "Helen Lloyd",
  205. "Eleanor Nye",
  206. "E9",
  207. "E8",
  208. "E5",
  209. "E4",
  210. "E7",
  211. "E6",
  212. "Myra Liddel",
  213. "E3",
  214. "Theresa Anderson",
  215. "Katherina Rogers",
  216. "Brenda Rogers",
  217. "Charlotte McDowd",
  218. "Sylvia Avondale",
  219. "Laura Mandeville",
  220. }
  221. ],
  222. }
  223. G = nx.davis_southern_women_graph()
  224. result = nx.k_components(G)
  225. for k, components in result.items():
  226. if k < 3:
  227. continue
  228. assert len(components) == len(solution[k])
  229. for component in components:
  230. assert component in solution[k]
  231. def test_set_consolidation_rosettacode():
  232. # Tests from http://rosettacode.org/wiki/Set_consolidation
  233. def list_of_sets_equal(result, solution):
  234. assert {frozenset(s) for s in result} == {frozenset(s) for s in solution}
  235. question = [{"A", "B"}, {"C", "D"}]
  236. solution = [{"A", "B"}, {"C", "D"}]
  237. list_of_sets_equal(_consolidate(question, 1), solution)
  238. question = [{"A", "B"}, {"B", "C"}]
  239. solution = [{"A", "B", "C"}]
  240. list_of_sets_equal(_consolidate(question, 1), solution)
  241. question = [{"A", "B"}, {"C", "D"}, {"D", "B"}]
  242. solution = [{"A", "C", "B", "D"}]
  243. list_of_sets_equal(_consolidate(question, 1), solution)
  244. question = [{"H", "I", "K"}, {"A", "B"}, {"C", "D"}, {"D", "B"}, {"F", "G", "H"}]
  245. solution = [{"A", "C", "B", "D"}, {"G", "F", "I", "H", "K"}]
  246. list_of_sets_equal(_consolidate(question, 1), solution)
  247. question = [
  248. {"A", "H"},
  249. {"H", "I", "K"},
  250. {"A", "B"},
  251. {"C", "D"},
  252. {"D", "B"},
  253. {"F", "G", "H"},
  254. ]
  255. solution = [{"A", "C", "B", "D", "G", "F", "I", "H", "K"}]
  256. list_of_sets_equal(_consolidate(question, 1), solution)
  257. question = [
  258. {"H", "I", "K"},
  259. {"A", "B"},
  260. {"C", "D"},
  261. {"D", "B"},
  262. {"F", "G", "H"},
  263. {"A", "H"},
  264. ]
  265. solution = [{"A", "C", "B", "D", "G", "F", "I", "H", "K"}]
  266. list_of_sets_equal(_consolidate(question, 1), solution)