test_connectivity.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import pytest
  2. import networkx as nx
  3. from networkx.algorithms import approximation as approx
  4. def test_global_node_connectivity():
  5. # Figure 1 chapter on Connectivity
  6. G = nx.Graph()
  7. G.add_edges_from(
  8. [
  9. (1, 2),
  10. (1, 3),
  11. (1, 4),
  12. (1, 5),
  13. (2, 3),
  14. (2, 6),
  15. (3, 4),
  16. (3, 6),
  17. (4, 6),
  18. (4, 7),
  19. (5, 7),
  20. (6, 8),
  21. (6, 9),
  22. (7, 8),
  23. (7, 10),
  24. (8, 11),
  25. (9, 10),
  26. (9, 11),
  27. (10, 11),
  28. ]
  29. )
  30. assert 2 == approx.local_node_connectivity(G, 1, 11)
  31. assert 2 == approx.node_connectivity(G)
  32. assert 2 == approx.node_connectivity(G, 1, 11)
  33. def test_white_harary1():
  34. # Figure 1b white and harary (2001)
  35. # A graph with high adhesion (edge connectivity) and low cohesion
  36. # (node connectivity)
  37. G = nx.disjoint_union(nx.complete_graph(4), nx.complete_graph(4))
  38. G.remove_node(7)
  39. for i in range(4, 7):
  40. G.add_edge(0, i)
  41. G = nx.disjoint_union(G, nx.complete_graph(4))
  42. G.remove_node(G.order() - 1)
  43. for i in range(7, 10):
  44. G.add_edge(0, i)
  45. assert 1 == approx.node_connectivity(G)
  46. def test_complete_graphs():
  47. for n in range(5, 25, 5):
  48. G = nx.complete_graph(n)
  49. assert n - 1 == approx.node_connectivity(G)
  50. assert n - 1 == approx.node_connectivity(G, 0, 3)
  51. def test_empty_graphs():
  52. for k in range(5, 25, 5):
  53. G = nx.empty_graph(k)
  54. assert 0 == approx.node_connectivity(G)
  55. assert 0 == approx.node_connectivity(G, 0, 3)
  56. def test_petersen():
  57. G = nx.petersen_graph()
  58. assert 3 == approx.node_connectivity(G)
  59. assert 3 == approx.node_connectivity(G, 0, 5)
  60. # Approximation fails with tutte graph
  61. # def test_tutte():
  62. # G = nx.tutte_graph()
  63. # assert_equal(3, approx.node_connectivity(G))
  64. def test_dodecahedral():
  65. G = nx.dodecahedral_graph()
  66. assert 3 == approx.node_connectivity(G)
  67. assert 3 == approx.node_connectivity(G, 0, 5)
  68. def test_octahedral():
  69. G = nx.octahedral_graph()
  70. assert 4 == approx.node_connectivity(G)
  71. assert 4 == approx.node_connectivity(G, 0, 5)
  72. # Approximation can fail with icosahedral graph depending
  73. # on iteration order.
  74. # def test_icosahedral():
  75. # G=nx.icosahedral_graph()
  76. # assert_equal(5, approx.node_connectivity(G))
  77. # assert_equal(5, approx.node_connectivity(G, 0, 5))
  78. def test_only_source():
  79. G = nx.complete_graph(5)
  80. pytest.raises(nx.NetworkXError, approx.node_connectivity, G, s=0)
  81. def test_only_target():
  82. G = nx.complete_graph(5)
  83. pytest.raises(nx.NetworkXError, approx.node_connectivity, G, t=0)
  84. def test_missing_source():
  85. G = nx.path_graph(4)
  86. pytest.raises(nx.NetworkXError, approx.node_connectivity, G, 10, 1)
  87. def test_missing_target():
  88. G = nx.path_graph(4)
  89. pytest.raises(nx.NetworkXError, approx.node_connectivity, G, 1, 10)
  90. def test_source_equals_target():
  91. G = nx.complete_graph(5)
  92. pytest.raises(nx.NetworkXError, approx.local_node_connectivity, G, 0, 0)
  93. def test_directed_node_connectivity():
  94. G = nx.cycle_graph(10, create_using=nx.DiGraph()) # only one direction
  95. D = nx.cycle_graph(10).to_directed() # 2 reciprocal edges
  96. assert 1 == approx.node_connectivity(G)
  97. assert 1 == approx.node_connectivity(G, 1, 4)
  98. assert 2 == approx.node_connectivity(D)
  99. assert 2 == approx.node_connectivity(D, 1, 4)
  100. class TestAllPairsNodeConnectivityApprox:
  101. @classmethod
  102. def setup_class(cls):
  103. cls.path = nx.path_graph(7)
  104. cls.directed_path = nx.path_graph(7, create_using=nx.DiGraph())
  105. cls.cycle = nx.cycle_graph(7)
  106. cls.directed_cycle = nx.cycle_graph(7, create_using=nx.DiGraph())
  107. cls.gnp = nx.gnp_random_graph(30, 0.1)
  108. cls.directed_gnp = nx.gnp_random_graph(30, 0.1, directed=True)
  109. cls.K20 = nx.complete_graph(20)
  110. cls.K10 = nx.complete_graph(10)
  111. cls.K5 = nx.complete_graph(5)
  112. cls.G_list = [
  113. cls.path,
  114. cls.directed_path,
  115. cls.cycle,
  116. cls.directed_cycle,
  117. cls.gnp,
  118. cls.directed_gnp,
  119. cls.K10,
  120. cls.K5,
  121. cls.K20,
  122. ]
  123. def test_cycles(self):
  124. K_undir = approx.all_pairs_node_connectivity(self.cycle)
  125. for source in K_undir:
  126. for target, k in K_undir[source].items():
  127. assert k == 2
  128. K_dir = approx.all_pairs_node_connectivity(self.directed_cycle)
  129. for source in K_dir:
  130. for target, k in K_dir[source].items():
  131. assert k == 1
  132. def test_complete(self):
  133. for G in [self.K10, self.K5, self.K20]:
  134. K = approx.all_pairs_node_connectivity(G)
  135. for source in K:
  136. for target, k in K[source].items():
  137. assert k == len(G) - 1
  138. def test_paths(self):
  139. K_undir = approx.all_pairs_node_connectivity(self.path)
  140. for source in K_undir:
  141. for target, k in K_undir[source].items():
  142. assert k == 1
  143. K_dir = approx.all_pairs_node_connectivity(self.directed_path)
  144. for source in K_dir:
  145. for target, k in K_dir[source].items():
  146. if source < target:
  147. assert k == 1
  148. else:
  149. assert k == 0
  150. def test_cutoff(self):
  151. for G in [self.K10, self.K5, self.K20]:
  152. for mp in [2, 3, 4]:
  153. paths = approx.all_pairs_node_connectivity(G, cutoff=mp)
  154. for source in paths:
  155. for target, K in paths[source].items():
  156. assert K == mp
  157. def test_all_pairs_connectivity_nbunch(self):
  158. G = nx.complete_graph(5)
  159. nbunch = [0, 2, 3]
  160. C = approx.all_pairs_node_connectivity(G, nbunch=nbunch)
  161. assert len(C) == len(nbunch)