test_recognition.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import pytest
  2. import networkx as nx
  3. class TestTreeRecognition:
  4. graph = nx.Graph
  5. multigraph = nx.MultiGraph
  6. @classmethod
  7. def setup_class(cls):
  8. cls.T1 = cls.graph()
  9. cls.T2 = cls.graph()
  10. cls.T2.add_node(1)
  11. cls.T3 = cls.graph()
  12. cls.T3.add_nodes_from(range(5))
  13. edges = [(i, i + 1) for i in range(4)]
  14. cls.T3.add_edges_from(edges)
  15. cls.T5 = cls.multigraph()
  16. cls.T5.add_nodes_from(range(5))
  17. edges = [(i, i + 1) for i in range(4)]
  18. cls.T5.add_edges_from(edges)
  19. cls.T6 = cls.graph()
  20. cls.T6.add_nodes_from([6, 7])
  21. cls.T6.add_edge(6, 7)
  22. cls.F1 = nx.compose(cls.T6, cls.T3)
  23. cls.N4 = cls.graph()
  24. cls.N4.add_node(1)
  25. cls.N4.add_edge(1, 1)
  26. cls.N5 = cls.graph()
  27. cls.N5.add_nodes_from(range(5))
  28. cls.N6 = cls.graph()
  29. cls.N6.add_nodes_from(range(3))
  30. cls.N6.add_edges_from([(0, 1), (1, 2), (2, 0)])
  31. cls.NF1 = nx.compose(cls.T6, cls.N6)
  32. def test_null_tree(self):
  33. with pytest.raises(nx.NetworkXPointlessConcept):
  34. nx.is_tree(self.graph())
  35. def test_null_tree2(self):
  36. with pytest.raises(nx.NetworkXPointlessConcept):
  37. nx.is_tree(self.multigraph())
  38. def test_null_forest(self):
  39. with pytest.raises(nx.NetworkXPointlessConcept):
  40. nx.is_forest(self.graph())
  41. def test_null_forest2(self):
  42. with pytest.raises(nx.NetworkXPointlessConcept):
  43. nx.is_forest(self.multigraph())
  44. def test_is_tree(self):
  45. assert nx.is_tree(self.T2)
  46. assert nx.is_tree(self.T3)
  47. assert nx.is_tree(self.T5)
  48. def test_is_not_tree(self):
  49. assert not nx.is_tree(self.N4)
  50. assert not nx.is_tree(self.N5)
  51. assert not nx.is_tree(self.N6)
  52. def test_is_forest(self):
  53. assert nx.is_forest(self.T2)
  54. assert nx.is_forest(self.T3)
  55. assert nx.is_forest(self.T5)
  56. assert nx.is_forest(self.F1)
  57. assert nx.is_forest(self.N5)
  58. def test_is_not_forest(self):
  59. assert not nx.is_forest(self.N4)
  60. assert not nx.is_forest(self.N6)
  61. assert not nx.is_forest(self.NF1)
  62. class TestDirectedTreeRecognition(TestTreeRecognition):
  63. graph = nx.DiGraph
  64. multigraph = nx.MultiDiGraph
  65. def test_disconnected_graph():
  66. # https://github.com/networkx/networkx/issues/1144
  67. G = nx.Graph()
  68. G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4)])
  69. assert not nx.is_tree(G)
  70. G = nx.DiGraph()
  71. G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4)])
  72. assert not nx.is_tree(G)
  73. def test_dag_nontree():
  74. G = nx.DiGraph()
  75. G.add_edges_from([(0, 1), (0, 2), (1, 2)])
  76. assert not nx.is_tree(G)
  77. assert nx.is_directed_acyclic_graph(G)
  78. def test_multicycle():
  79. G = nx.MultiDiGraph()
  80. G.add_edges_from([(0, 1), (0, 1)])
  81. assert not nx.is_tree(G)
  82. assert nx.is_directed_acyclic_graph(G)
  83. def test_emptybranch():
  84. G = nx.DiGraph()
  85. G.add_nodes_from(range(10))
  86. assert nx.is_branching(G)
  87. assert not nx.is_arborescence(G)
  88. def test_path():
  89. G = nx.DiGraph()
  90. nx.add_path(G, range(5))
  91. assert nx.is_branching(G)
  92. assert nx.is_arborescence(G)
  93. def test_notbranching1():
  94. # Acyclic violation.
  95. G = nx.MultiDiGraph()
  96. G.add_nodes_from(range(10))
  97. G.add_edges_from([(0, 1), (1, 0)])
  98. assert not nx.is_branching(G)
  99. assert not nx.is_arborescence(G)
  100. def test_notbranching2():
  101. # In-degree violation.
  102. G = nx.MultiDiGraph()
  103. G.add_nodes_from(range(10))
  104. G.add_edges_from([(0, 1), (0, 2), (3, 2)])
  105. assert not nx.is_branching(G)
  106. assert not nx.is_arborescence(G)
  107. def test_notarborescence1():
  108. # Not an arborescence due to not spanning.
  109. G = nx.MultiDiGraph()
  110. G.add_nodes_from(range(10))
  111. G.add_edges_from([(0, 1), (0, 2), (1, 3), (5, 6)])
  112. assert nx.is_branching(G)
  113. assert not nx.is_arborescence(G)
  114. def test_notarborescence2():
  115. # Not an arborescence due to in-degree violation.
  116. G = nx.MultiDiGraph()
  117. nx.add_path(G, range(5))
  118. G.add_edge(6, 4)
  119. assert not nx.is_branching(G)
  120. assert not nx.is_arborescence(G)