test_flow.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import numpy as np
  2. from numpy.testing import assert_array_equal
  3. import pytest
  4. from scipy.sparse import csr_matrix, csc_matrix
  5. from scipy.sparse.csgraph import maximum_flow
  6. from scipy.sparse.csgraph._flow import (
  7. _add_reverse_edges, _make_edge_pointers, _make_tails
  8. )
  9. methods = ['edmonds_karp', 'dinic']
  10. def test_raises_on_dense_input():
  11. with pytest.raises(TypeError):
  12. graph = np.array([[0, 1], [0, 0]])
  13. maximum_flow(graph, 0, 1)
  14. maximum_flow(graph, 0, 1, method='edmonds_karp')
  15. def test_raises_on_csc_input():
  16. with pytest.raises(TypeError):
  17. graph = csc_matrix([[0, 1], [0, 0]])
  18. maximum_flow(graph, 0, 1)
  19. maximum_flow(graph, 0, 1, method='edmonds_karp')
  20. def test_raises_on_floating_point_input():
  21. with pytest.raises(ValueError):
  22. graph = csr_matrix([[0, 1.5], [0, 0]], dtype=np.float64)
  23. maximum_flow(graph, 0, 1)
  24. maximum_flow(graph, 0, 1, method='edmonds_karp')
  25. def test_raises_on_non_square_input():
  26. with pytest.raises(ValueError):
  27. graph = csr_matrix([[0, 1, 2], [2, 1, 0]])
  28. maximum_flow(graph, 0, 1)
  29. def test_raises_when_source_is_sink():
  30. with pytest.raises(ValueError):
  31. graph = csr_matrix([[0, 1], [0, 0]])
  32. maximum_flow(graph, 0, 0)
  33. maximum_flow(graph, 0, 0, method='edmonds_karp')
  34. @pytest.mark.parametrize('method', methods)
  35. @pytest.mark.parametrize('source', [-1, 2, 3])
  36. def test_raises_when_source_is_out_of_bounds(source, method):
  37. with pytest.raises(ValueError):
  38. graph = csr_matrix([[0, 1], [0, 0]])
  39. maximum_flow(graph, source, 1, method=method)
  40. @pytest.mark.parametrize('method', methods)
  41. @pytest.mark.parametrize('sink', [-1, 2, 3])
  42. def test_raises_when_sink_is_out_of_bounds(sink, method):
  43. with pytest.raises(ValueError):
  44. graph = csr_matrix([[0, 1], [0, 0]])
  45. maximum_flow(graph, 0, sink, method=method)
  46. @pytest.mark.parametrize('method', methods)
  47. def test_simple_graph(method):
  48. # This graph looks as follows:
  49. # (0) --5--> (1)
  50. graph = csr_matrix([[0, 5], [0, 0]])
  51. res = maximum_flow(graph, 0, 1, method=method)
  52. assert res.flow_value == 5
  53. expected_flow = np.array([[0, 5], [-5, 0]])
  54. assert_array_equal(res.flow.toarray(), expected_flow)
  55. @pytest.mark.parametrize('method', methods)
  56. def test_bottle_neck_graph(method):
  57. # This graph cannot use the full capacity between 0 and 1:
  58. # (0) --5--> (1) --3--> (2)
  59. graph = csr_matrix([[0, 5, 0], [0, 0, 3], [0, 0, 0]])
  60. res = maximum_flow(graph, 0, 2, method=method)
  61. assert res.flow_value == 3
  62. expected_flow = np.array([[0, 3, 0], [-3, 0, 3], [0, -3, 0]])
  63. assert_array_equal(res.flow.toarray(), expected_flow)
  64. @pytest.mark.parametrize('method', methods)
  65. def test_backwards_flow(method):
  66. # This example causes backwards flow between vertices 3 and 4,
  67. # and so this test ensures that we handle that accordingly. See
  68. # https://stackoverflow.com/q/38843963/5085211
  69. # for more information.
  70. graph = csr_matrix([[0, 10, 0, 0, 10, 0, 0, 0],
  71. [0, 0, 10, 0, 0, 0, 0, 0],
  72. [0, 0, 0, 10, 0, 0, 0, 0],
  73. [0, 0, 0, 0, 0, 0, 0, 10],
  74. [0, 0, 0, 10, 0, 10, 0, 0],
  75. [0, 0, 0, 0, 0, 0, 10, 0],
  76. [0, 0, 0, 0, 0, 0, 0, 10],
  77. [0, 0, 0, 0, 0, 0, 0, 0]])
  78. res = maximum_flow(graph, 0, 7, method=method)
  79. assert res.flow_value == 20
  80. expected_flow = np.array([[0, 10, 0, 0, 10, 0, 0, 0],
  81. [-10, 0, 10, 0, 0, 0, 0, 0],
  82. [0, -10, 0, 10, 0, 0, 0, 0],
  83. [0, 0, -10, 0, 0, 0, 0, 10],
  84. [-10, 0, 0, 0, 0, 10, 0, 0],
  85. [0, 0, 0, 0, -10, 0, 10, 0],
  86. [0, 0, 0, 0, 0, -10, 0, 10],
  87. [0, 0, 0, -10, 0, 0, -10, 0]])
  88. assert_array_equal(res.flow.toarray(), expected_flow)
  89. @pytest.mark.parametrize('method', methods)
  90. def test_example_from_clrs_chapter_26_1(method):
  91. # See page 659 in CLRS second edition, but note that the maximum flow
  92. # we find is slightly different than the one in CLRS; we push a flow of
  93. # 12 to v_1 instead of v_2.
  94. graph = csr_matrix([[0, 16, 13, 0, 0, 0],
  95. [0, 0, 10, 12, 0, 0],
  96. [0, 4, 0, 0, 14, 0],
  97. [0, 0, 9, 0, 0, 20],
  98. [0, 0, 0, 7, 0, 4],
  99. [0, 0, 0, 0, 0, 0]])
  100. res = maximum_flow(graph, 0, 5, method=method)
  101. assert res.flow_value == 23
  102. expected_flow = np.array([[0, 12, 11, 0, 0, 0],
  103. [-12, 0, 0, 12, 0, 0],
  104. [-11, 0, 0, 0, 11, 0],
  105. [0, -12, 0, 0, -7, 19],
  106. [0, 0, -11, 7, 0, 4],
  107. [0, 0, 0, -19, -4, 0]])
  108. assert_array_equal(res.flow.toarray(), expected_flow)
  109. @pytest.mark.parametrize('method', methods)
  110. def test_disconnected_graph(method):
  111. # This tests the following disconnected graph:
  112. # (0) --5--> (1) (2) --3--> (3)
  113. graph = csr_matrix([[0, 5, 0, 0],
  114. [0, 0, 0, 0],
  115. [0, 0, 9, 3],
  116. [0, 0, 0, 0]])
  117. res = maximum_flow(graph, 0, 3, method=method)
  118. assert res.flow_value == 0
  119. expected_flow = np.zeros((4, 4), dtype=np.int32)
  120. assert_array_equal(res.flow.toarray(), expected_flow)
  121. @pytest.mark.parametrize('method', methods)
  122. def test_add_reverse_edges_large_graph(method):
  123. # Regression test for https://github.com/scipy/scipy/issues/14385
  124. n = 100_000
  125. indices = np.arange(1, n)
  126. indptr = np.array(list(range(n)) + [n - 1])
  127. data = np.ones(n - 1, dtype=np.int32)
  128. graph = csr_matrix((data, indices, indptr), shape=(n, n))
  129. res = maximum_flow(graph, 0, n - 1, method=method)
  130. assert res.flow_value == 1
  131. expected_flow = graph - graph.transpose()
  132. assert_array_equal(res.flow.data, expected_flow.data)
  133. assert_array_equal(res.flow.indices, expected_flow.indices)
  134. assert_array_equal(res.flow.indptr, expected_flow.indptr)
  135. def test_residual_raises_deprecation_warning():
  136. graph = csr_matrix([[0, 5, 0], [0, 0, 3], [0, 0, 0]])
  137. res = maximum_flow(graph, 0, 2)
  138. with pytest.deprecated_call():
  139. res.residual
  140. @pytest.mark.parametrize("a,b_data_expected", [
  141. ([[]], []),
  142. ([[0], [0]], []),
  143. ([[1, 0, 2], [0, 0, 0], [0, 3, 0]], [1, 2, 0, 0, 3]),
  144. ([[9, 8, 7], [4, 5, 6], [0, 0, 0]], [9, 8, 7, 4, 5, 6, 0, 0])])
  145. def test_add_reverse_edges(a, b_data_expected):
  146. """Test that the reversal of the edges of the input graph works
  147. as expected.
  148. """
  149. a = csr_matrix(a, dtype=np.int32, shape=(len(a), len(a)))
  150. b = _add_reverse_edges(a)
  151. assert_array_equal(b.data, b_data_expected)
  152. @pytest.mark.parametrize("a,expected", [
  153. ([[]], []),
  154. ([[0]], []),
  155. ([[1]], [0]),
  156. ([[0, 1], [10, 0]], [1, 0]),
  157. ([[1, 0, 2], [0, 0, 3], [4, 5, 0]], [0, 3, 4, 1, 2])
  158. ])
  159. def test_make_edge_pointers(a, expected):
  160. a = csr_matrix(a, dtype=np.int32)
  161. rev_edge_ptr = _make_edge_pointers(a)
  162. assert_array_equal(rev_edge_ptr, expected)
  163. @pytest.mark.parametrize("a,expected", [
  164. ([[]], []),
  165. ([[0]], []),
  166. ([[1]], [0]),
  167. ([[0, 1], [10, 0]], [0, 1]),
  168. ([[1, 0, 2], [0, 0, 3], [4, 5, 0]], [0, 0, 1, 2, 2])
  169. ])
  170. def test_make_tails(a, expected):
  171. a = csr_matrix(a, dtype=np.int32)
  172. tails = _make_tails(a)
  173. assert_array_equal(tails, expected)