test_attracting.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import pytest
  2. import networkx as nx
  3. from networkx import NetworkXNotImplemented
  4. class TestAttractingComponents:
  5. @classmethod
  6. def setup_class(cls):
  7. cls.G1 = nx.DiGraph()
  8. cls.G1.add_edges_from(
  9. [
  10. (5, 11),
  11. (11, 2),
  12. (11, 9),
  13. (11, 10),
  14. (7, 11),
  15. (7, 8),
  16. (8, 9),
  17. (3, 8),
  18. (3, 10),
  19. ]
  20. )
  21. cls.G2 = nx.DiGraph()
  22. cls.G2.add_edges_from([(0, 1), (0, 2), (1, 1), (1, 2), (2, 1)])
  23. cls.G3 = nx.DiGraph()
  24. cls.G3.add_edges_from([(0, 1), (1, 2), (2, 1), (0, 3), (3, 4), (4, 3)])
  25. cls.G4 = nx.DiGraph()
  26. def test_attracting_components(self):
  27. ac = list(nx.attracting_components(self.G1))
  28. assert {2} in ac
  29. assert {9} in ac
  30. assert {10} in ac
  31. ac = list(nx.attracting_components(self.G2))
  32. ac = [tuple(sorted(x)) for x in ac]
  33. assert ac == [(1, 2)]
  34. ac = list(nx.attracting_components(self.G3))
  35. ac = [tuple(sorted(x)) for x in ac]
  36. assert (1, 2) in ac
  37. assert (3, 4) in ac
  38. assert len(ac) == 2
  39. ac = list(nx.attracting_components(self.G4))
  40. assert ac == []
  41. def test_number_attacting_components(self):
  42. assert nx.number_attracting_components(self.G1) == 3
  43. assert nx.number_attracting_components(self.G2) == 1
  44. assert nx.number_attracting_components(self.G3) == 2
  45. assert nx.number_attracting_components(self.G4) == 0
  46. def test_is_attracting_component(self):
  47. assert not nx.is_attracting_component(self.G1)
  48. assert not nx.is_attracting_component(self.G2)
  49. assert not nx.is_attracting_component(self.G3)
  50. g2 = self.G3.subgraph([1, 2])
  51. assert nx.is_attracting_component(g2)
  52. assert not nx.is_attracting_component(self.G4)
  53. def test_connected_raise(self):
  54. G = nx.Graph()
  55. with pytest.raises(NetworkXNotImplemented):
  56. next(nx.attracting_components(G))
  57. pytest.raises(NetworkXNotImplemented, nx.number_attracting_components, G)
  58. pytest.raises(NetworkXNotImplemented, nx.is_attracting_component, G)