test_edgebfs.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import pytest
  2. import networkx as nx
  3. from networkx.algorithms.traversal.edgedfs import FORWARD, REVERSE
  4. class TestEdgeBFS:
  5. @classmethod
  6. def setup_class(cls):
  7. cls.nodes = [0, 1, 2, 3]
  8. cls.edges = [(0, 1), (1, 0), (1, 0), (2, 0), (2, 1), (3, 1)]
  9. def test_empty(self):
  10. G = nx.Graph()
  11. edges = list(nx.edge_bfs(G))
  12. assert edges == []
  13. def test_graph_single_source(self):
  14. G = nx.Graph(self.edges)
  15. G.add_edge(4, 5)
  16. x = list(nx.edge_bfs(G, [0]))
  17. x_ = [(0, 1), (0, 2), (1, 2), (1, 3)]
  18. assert x == x_
  19. def test_graph(self):
  20. G = nx.Graph(self.edges)
  21. x = list(nx.edge_bfs(G, self.nodes))
  22. x_ = [(0, 1), (0, 2), (1, 2), (1, 3)]
  23. assert x == x_
  24. def test_digraph(self):
  25. G = nx.DiGraph(self.edges)
  26. x = list(nx.edge_bfs(G, self.nodes))
  27. x_ = [(0, 1), (1, 0), (2, 0), (2, 1), (3, 1)]
  28. assert x == x_
  29. def test_digraph_orientation_invalid(self):
  30. G = nx.DiGraph(self.edges)
  31. edge_iterator = nx.edge_bfs(G, self.nodes, orientation="hello")
  32. pytest.raises(nx.NetworkXError, list, edge_iterator)
  33. def test_digraph_orientation_none(self):
  34. G = nx.DiGraph(self.edges)
  35. x = list(nx.edge_bfs(G, self.nodes, orientation=None))
  36. x_ = [(0, 1), (1, 0), (2, 0), (2, 1), (3, 1)]
  37. assert x == x_
  38. def test_digraph_orientation_original(self):
  39. G = nx.DiGraph(self.edges)
  40. x = list(nx.edge_bfs(G, self.nodes, orientation="original"))
  41. x_ = [
  42. (0, 1, FORWARD),
  43. (1, 0, FORWARD),
  44. (2, 0, FORWARD),
  45. (2, 1, FORWARD),
  46. (3, 1, FORWARD),
  47. ]
  48. assert x == x_
  49. def test_digraph2(self):
  50. G = nx.DiGraph()
  51. nx.add_path(G, range(4))
  52. x = list(nx.edge_bfs(G, [0]))
  53. x_ = [(0, 1), (1, 2), (2, 3)]
  54. assert x == x_
  55. def test_digraph_rev(self):
  56. G = nx.DiGraph(self.edges)
  57. x = list(nx.edge_bfs(G, self.nodes, orientation="reverse"))
  58. x_ = [
  59. (1, 0, REVERSE),
  60. (2, 0, REVERSE),
  61. (0, 1, REVERSE),
  62. (2, 1, REVERSE),
  63. (3, 1, REVERSE),
  64. ]
  65. assert x == x_
  66. def test_digraph_rev2(self):
  67. G = nx.DiGraph()
  68. nx.add_path(G, range(4))
  69. x = list(nx.edge_bfs(G, [3], orientation="reverse"))
  70. x_ = [(2, 3, REVERSE), (1, 2, REVERSE), (0, 1, REVERSE)]
  71. assert x == x_
  72. def test_multigraph(self):
  73. G = nx.MultiGraph(self.edges)
  74. x = list(nx.edge_bfs(G, self.nodes))
  75. x_ = [(0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0), (1, 2, 0), (1, 3, 0)]
  76. # This is an example of where hash randomization can break.
  77. # There are 3! * 2 alternative outputs, such as:
  78. # [(0, 1, 1), (1, 0, 0), (0, 1, 2), (1, 3, 0), (1, 2, 0)]
  79. # But note, the edges (1,2,0) and (1,3,0) always follow the (0,1,k)
  80. # edges. So the algorithm only guarantees a partial order. A total
  81. # order is guaranteed only if the graph data structures are ordered.
  82. assert x == x_
  83. def test_multidigraph(self):
  84. G = nx.MultiDiGraph(self.edges)
  85. x = list(nx.edge_bfs(G, self.nodes))
  86. x_ = [(0, 1, 0), (1, 0, 0), (1, 0, 1), (2, 0, 0), (2, 1, 0), (3, 1, 0)]
  87. assert x == x_
  88. def test_multidigraph_rev(self):
  89. G = nx.MultiDiGraph(self.edges)
  90. x = list(nx.edge_bfs(G, self.nodes, orientation="reverse"))
  91. x_ = [
  92. (1, 0, 0, REVERSE),
  93. (1, 0, 1, REVERSE),
  94. (2, 0, 0, REVERSE),
  95. (0, 1, 0, REVERSE),
  96. (2, 1, 0, REVERSE),
  97. (3, 1, 0, REVERSE),
  98. ]
  99. assert x == x_
  100. def test_digraph_ignore(self):
  101. G = nx.DiGraph(self.edges)
  102. x = list(nx.edge_bfs(G, self.nodes, orientation="ignore"))
  103. x_ = [
  104. (0, 1, FORWARD),
  105. (1, 0, REVERSE),
  106. (2, 0, REVERSE),
  107. (2, 1, REVERSE),
  108. (3, 1, REVERSE),
  109. ]
  110. assert x == x_
  111. def test_digraph_ignore2(self):
  112. G = nx.DiGraph()
  113. nx.add_path(G, range(4))
  114. x = list(nx.edge_bfs(G, [0], orientation="ignore"))
  115. x_ = [(0, 1, FORWARD), (1, 2, FORWARD), (2, 3, FORWARD)]
  116. assert x == x_
  117. def test_multidigraph_ignore(self):
  118. G = nx.MultiDiGraph(self.edges)
  119. x = list(nx.edge_bfs(G, self.nodes, orientation="ignore"))
  120. x_ = [
  121. (0, 1, 0, FORWARD),
  122. (1, 0, 0, REVERSE),
  123. (1, 0, 1, REVERSE),
  124. (2, 0, 0, REVERSE),
  125. (2, 1, 0, REVERSE),
  126. (3, 1, 0, REVERSE),
  127. ]
  128. assert x == x_