test_connected.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import pytest
  2. import networkx as nx
  3. from networkx import NetworkXNotImplemented
  4. from networkx import convert_node_labels_to_integers as cnlti
  5. from networkx.classes.tests import dispatch_interface
  6. class TestConnected:
  7. @classmethod
  8. def setup_class(cls):
  9. G1 = cnlti(nx.grid_2d_graph(2, 2), first_label=0, ordering="sorted")
  10. G2 = cnlti(nx.lollipop_graph(3, 3), first_label=4, ordering="sorted")
  11. G3 = cnlti(nx.house_graph(), first_label=10, ordering="sorted")
  12. cls.G = nx.union(G1, G2)
  13. cls.G = nx.union(cls.G, G3)
  14. cls.DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)])
  15. cls.grid = cnlti(nx.grid_2d_graph(4, 4), first_label=1)
  16. cls.gc = []
  17. G = nx.DiGraph()
  18. G.add_edges_from(
  19. [
  20. (1, 2),
  21. (2, 3),
  22. (2, 8),
  23. (3, 4),
  24. (3, 7),
  25. (4, 5),
  26. (5, 3),
  27. (5, 6),
  28. (7, 4),
  29. (7, 6),
  30. (8, 1),
  31. (8, 7),
  32. ]
  33. )
  34. C = [[3, 4, 5, 7], [1, 2, 8], [6]]
  35. cls.gc.append((G, C))
  36. G = nx.DiGraph()
  37. G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
  38. C = [[2, 3, 4], [1]]
  39. cls.gc.append((G, C))
  40. G = nx.DiGraph()
  41. G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
  42. C = [[1, 2, 3]]
  43. cls.gc.append((G, C))
  44. # Eppstein's tests
  45. G = nx.DiGraph({0: [1], 1: [2, 3], 2: [4, 5], 3: [4, 5], 4: [6], 5: [], 6: []})
  46. C = [[0], [1], [2], [3], [4], [5], [6]]
  47. cls.gc.append((G, C))
  48. G = nx.DiGraph({0: [1], 1: [2, 3, 4], 2: [0, 3], 3: [4], 4: [3]})
  49. C = [[0, 1, 2], [3, 4]]
  50. cls.gc.append((G, C))
  51. G = nx.DiGraph()
  52. C = []
  53. cls.gc.append((G, C))
  54. # This additionally tests the @nx._dispatch mechanism, treating
  55. # nx.connected_components as if it were a re-implementation from another package
  56. @pytest.mark.parametrize("wrapper", [lambda x: x, dispatch_interface.convert])
  57. def test_connected_components(self, wrapper):
  58. cc = nx.connected_components
  59. G = wrapper(self.G)
  60. C = {
  61. frozenset([0, 1, 2, 3]),
  62. frozenset([4, 5, 6, 7, 8, 9]),
  63. frozenset([10, 11, 12, 13, 14]),
  64. }
  65. assert {frozenset(g) for g in cc(G)} == C
  66. def test_number_connected_components(self):
  67. ncc = nx.number_connected_components
  68. assert ncc(self.G) == 3
  69. def test_number_connected_components2(self):
  70. ncc = nx.number_connected_components
  71. assert ncc(self.grid) == 1
  72. def test_connected_components2(self):
  73. cc = nx.connected_components
  74. G = self.grid
  75. C = {frozenset([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])}
  76. assert {frozenset(g) for g in cc(G)} == C
  77. def test_node_connected_components(self):
  78. ncc = nx.node_connected_component
  79. G = self.grid
  80. C = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
  81. assert ncc(G, 1) == C
  82. def test_is_connected(self):
  83. assert nx.is_connected(self.grid)
  84. G = nx.Graph()
  85. G.add_nodes_from([1, 2])
  86. assert not nx.is_connected(G)
  87. def test_connected_raise(self):
  88. with pytest.raises(NetworkXNotImplemented):
  89. next(nx.connected_components(self.DG))
  90. pytest.raises(NetworkXNotImplemented, nx.number_connected_components, self.DG)
  91. pytest.raises(NetworkXNotImplemented, nx.node_connected_component, self.DG, 1)
  92. pytest.raises(NetworkXNotImplemented, nx.is_connected, self.DG)
  93. pytest.raises(nx.NetworkXPointlessConcept, nx.is_connected, nx.Graph())
  94. def test_connected_mutability(self):
  95. G = self.grid
  96. seen = set()
  97. for component in nx.connected_components(G):
  98. assert len(seen & component) == 0
  99. seen.update(component)
  100. component.clear()