test_shortest_path.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. from io import StringIO
  2. import warnings
  3. import numpy as np
  4. from numpy.testing import assert_array_almost_equal, assert_array_equal, assert_allclose
  5. from pytest import raises as assert_raises
  6. from scipy.sparse.csgraph import (shortest_path, dijkstra, johnson,
  7. bellman_ford, construct_dist_matrix,
  8. NegativeCycleError)
  9. import scipy.sparse
  10. from scipy.io import mmread
  11. import pytest
  12. directed_G = np.array([[0, 3, 3, 0, 0],
  13. [0, 0, 0, 2, 4],
  14. [0, 0, 0, 0, 0],
  15. [1, 0, 0, 0, 0],
  16. [2, 0, 0, 2, 0]], dtype=float)
  17. undirected_G = np.array([[0, 3, 3, 1, 2],
  18. [3, 0, 0, 2, 4],
  19. [3, 0, 0, 0, 0],
  20. [1, 2, 0, 0, 2],
  21. [2, 4, 0, 2, 0]], dtype=float)
  22. unweighted_G = (directed_G > 0).astype(float)
  23. directed_SP = [[0, 3, 3, 5, 7],
  24. [3, 0, 6, 2, 4],
  25. [np.inf, np.inf, 0, np.inf, np.inf],
  26. [1, 4, 4, 0, 8],
  27. [2, 5, 5, 2, 0]]
  28. directed_sparse_zero_G = scipy.sparse.csr_matrix(([0, 1, 2, 3, 1],
  29. ([0, 1, 2, 3, 4],
  30. [1, 2, 0, 4, 3])),
  31. shape = (5, 5))
  32. directed_sparse_zero_SP = [[0, 0, 1, np.inf, np.inf],
  33. [3, 0, 1, np.inf, np.inf],
  34. [2, 2, 0, np.inf, np.inf],
  35. [np.inf, np.inf, np.inf, 0, 3],
  36. [np.inf, np.inf, np.inf, 1, 0]]
  37. undirected_sparse_zero_G = scipy.sparse.csr_matrix(([0, 0, 1, 1, 2, 2, 1, 1],
  38. ([0, 1, 1, 2, 2, 0, 3, 4],
  39. [1, 0, 2, 1, 0, 2, 4, 3])),
  40. shape = (5, 5))
  41. undirected_sparse_zero_SP = [[0, 0, 1, np.inf, np.inf],
  42. [0, 0, 1, np.inf, np.inf],
  43. [1, 1, 0, np.inf, np.inf],
  44. [np.inf, np.inf, np.inf, 0, 1],
  45. [np.inf, np.inf, np.inf, 1, 0]]
  46. directed_pred = np.array([[-9999, 0, 0, 1, 1],
  47. [3, -9999, 0, 1, 1],
  48. [-9999, -9999, -9999, -9999, -9999],
  49. [3, 0, 0, -9999, 1],
  50. [4, 0, 0, 4, -9999]], dtype=float)
  51. undirected_SP = np.array([[0, 3, 3, 1, 2],
  52. [3, 0, 6, 2, 4],
  53. [3, 6, 0, 4, 5],
  54. [1, 2, 4, 0, 2],
  55. [2, 4, 5, 2, 0]], dtype=float)
  56. undirected_SP_limit_2 = np.array([[0, np.inf, np.inf, 1, 2],
  57. [np.inf, 0, np.inf, 2, np.inf],
  58. [np.inf, np.inf, 0, np.inf, np.inf],
  59. [1, 2, np.inf, 0, 2],
  60. [2, np.inf, np.inf, 2, 0]], dtype=float)
  61. undirected_SP_limit_0 = np.ones((5, 5), dtype=float) - np.eye(5)
  62. undirected_SP_limit_0[undirected_SP_limit_0 > 0] = np.inf
  63. undirected_pred = np.array([[-9999, 0, 0, 0, 0],
  64. [1, -9999, 0, 1, 1],
  65. [2, 0, -9999, 0, 0],
  66. [3, 3, 0, -9999, 3],
  67. [4, 4, 0, 4, -9999]], dtype=float)
  68. directed_negative_weighted_G = np.array([[0, 0, 0],
  69. [-1, 0, 0],
  70. [0, -1, 0]], dtype=float)
  71. directed_negative_weighted_SP = np.array([[0, np.inf, np.inf],
  72. [-1, 0, np.inf],
  73. [-2, -1, 0]], dtype=float)
  74. methods = ['auto', 'FW', 'D', 'BF', 'J']
  75. def test_dijkstra_limit():
  76. limits = [0, 2, np.inf]
  77. results = [undirected_SP_limit_0,
  78. undirected_SP_limit_2,
  79. undirected_SP]
  80. def check(limit, result):
  81. SP = dijkstra(undirected_G, directed=False, limit=limit)
  82. assert_array_almost_equal(SP, result)
  83. for limit, result in zip(limits, results):
  84. check(limit, result)
  85. def test_directed():
  86. def check(method):
  87. SP = shortest_path(directed_G, method=method, directed=True,
  88. overwrite=False)
  89. assert_array_almost_equal(SP, directed_SP)
  90. for method in methods:
  91. check(method)
  92. def test_undirected():
  93. def check(method, directed_in):
  94. if directed_in:
  95. SP1 = shortest_path(directed_G, method=method, directed=False,
  96. overwrite=False)
  97. assert_array_almost_equal(SP1, undirected_SP)
  98. else:
  99. SP2 = shortest_path(undirected_G, method=method, directed=True,
  100. overwrite=False)
  101. assert_array_almost_equal(SP2, undirected_SP)
  102. for method in methods:
  103. for directed_in in (True, False):
  104. check(method, directed_in)
  105. def test_directed_sparse_zero():
  106. # test directed sparse graph with zero-weight edge and two connected components
  107. def check(method):
  108. SP = shortest_path(directed_sparse_zero_G, method=method, directed=True,
  109. overwrite=False)
  110. assert_array_almost_equal(SP, directed_sparse_zero_SP)
  111. for method in methods:
  112. check(method)
  113. def test_undirected_sparse_zero():
  114. def check(method, directed_in):
  115. if directed_in:
  116. SP1 = shortest_path(directed_sparse_zero_G, method=method, directed=False,
  117. overwrite=False)
  118. assert_array_almost_equal(SP1, undirected_sparse_zero_SP)
  119. else:
  120. SP2 = shortest_path(undirected_sparse_zero_G, method=method, directed=True,
  121. overwrite=False)
  122. assert_array_almost_equal(SP2, undirected_sparse_zero_SP)
  123. for method in methods:
  124. for directed_in in (True, False):
  125. check(method, directed_in)
  126. @pytest.mark.parametrize('directed, SP_ans',
  127. ((True, directed_SP),
  128. (False, undirected_SP)))
  129. @pytest.mark.parametrize('indices', ([0, 2, 4], [0, 4], [3, 4], [0, 0]))
  130. def test_dijkstra_indices_min_only(directed, SP_ans, indices):
  131. SP_ans = np.array(SP_ans)
  132. indices = np.array(indices, dtype=np.int64)
  133. min_ind_ans = indices[np.argmin(SP_ans[indices, :], axis=0)]
  134. min_d_ans = np.zeros(SP_ans.shape[0], SP_ans.dtype)
  135. for k in range(SP_ans.shape[0]):
  136. min_d_ans[k] = SP_ans[min_ind_ans[k], k]
  137. min_ind_ans[np.isinf(min_d_ans)] = -9999
  138. SP, pred, sources = dijkstra(directed_G,
  139. directed=directed,
  140. indices=indices,
  141. min_only=True,
  142. return_predecessors=True)
  143. assert_array_almost_equal(SP, min_d_ans)
  144. assert_array_equal(min_ind_ans, sources)
  145. SP = dijkstra(directed_G,
  146. directed=directed,
  147. indices=indices,
  148. min_only=True,
  149. return_predecessors=False)
  150. assert_array_almost_equal(SP, min_d_ans)
  151. @pytest.mark.parametrize('n', (10, 100, 1000))
  152. def test_dijkstra_min_only_random(n):
  153. np.random.seed(1234)
  154. data = scipy.sparse.rand(n, n, density=0.5, format='lil',
  155. random_state=42, dtype=np.float64)
  156. data.setdiag(np.zeros(n, dtype=np.bool_))
  157. # choose some random vertices
  158. v = np.arange(n)
  159. np.random.shuffle(v)
  160. indices = v[:int(n*.1)]
  161. ds, pred, sources = dijkstra(data,
  162. directed=True,
  163. indices=indices,
  164. min_only=True,
  165. return_predecessors=True)
  166. for k in range(n):
  167. p = pred[k]
  168. s = sources[k]
  169. while p != -9999:
  170. assert sources[p] == s
  171. p = pred[p]
  172. def test_dijkstra_random():
  173. # reproduces the hang observed in gh-17782
  174. n = 10
  175. indices = [0, 4, 4, 5, 7, 9, 0, 6, 2, 3, 7, 9, 1, 2, 9, 2, 5, 6]
  176. indptr = [0, 0, 2, 5, 6, 7, 8, 12, 15, 18, 18]
  177. data = [0.33629, 0.40458, 0.47493, 0.42757, 0.11497, 0.91653, 0.69084,
  178. 0.64979, 0.62555, 0.743, 0.01724, 0.99945, 0.31095, 0.15557,
  179. 0.02439, 0.65814, 0.23478, 0.24072]
  180. graph = scipy.sparse.csr_matrix((data, indices, indptr), shape=(n, n))
  181. dijkstra(graph, directed=True, return_predecessors=True)
  182. def test_gh_17782_segfault():
  183. text = """%%MatrixMarket matrix coordinate real general
  184. 84 84 22
  185. 2 1 4.699999809265137e+00
  186. 6 14 1.199999973177910e-01
  187. 9 6 1.199999973177910e-01
  188. 10 16 2.012000083923340e+01
  189. 11 10 1.422000026702881e+01
  190. 12 1 9.645999908447266e+01
  191. 13 18 2.012000083923340e+01
  192. 14 13 4.679999828338623e+00
  193. 15 11 1.199999973177910e-01
  194. 16 12 1.199999973177910e-01
  195. 18 15 1.199999973177910e-01
  196. 32 2 2.299999952316284e+00
  197. 33 20 6.000000000000000e+00
  198. 33 32 5.000000000000000e+00
  199. 36 9 3.720000028610229e+00
  200. 36 37 3.720000028610229e+00
  201. 36 38 3.720000028610229e+00
  202. 37 44 8.159999847412109e+00
  203. 38 32 7.903999328613281e+01
  204. 43 20 2.400000000000000e+01
  205. 43 33 4.000000000000000e+00
  206. 44 43 6.028000259399414e+01
  207. """
  208. data = mmread(StringIO(text))
  209. dijkstra(data, directed=True, return_predecessors=True)
  210. def test_shortest_path_indices():
  211. indices = np.arange(4)
  212. def check(func, indshape):
  213. outshape = indshape + (5,)
  214. SP = func(directed_G, directed=False,
  215. indices=indices.reshape(indshape))
  216. assert_array_almost_equal(SP, undirected_SP[indices].reshape(outshape))
  217. for indshape in [(4,), (4, 1), (2, 2)]:
  218. for func in (dijkstra, bellman_ford, johnson, shortest_path):
  219. check(func, indshape)
  220. assert_raises(ValueError, shortest_path, directed_G, method='FW',
  221. indices=indices)
  222. def test_predecessors():
  223. SP_res = {True: directed_SP,
  224. False: undirected_SP}
  225. pred_res = {True: directed_pred,
  226. False: undirected_pred}
  227. def check(method, directed):
  228. SP, pred = shortest_path(directed_G, method, directed=directed,
  229. overwrite=False,
  230. return_predecessors=True)
  231. assert_array_almost_equal(SP, SP_res[directed])
  232. assert_array_almost_equal(pred, pred_res[directed])
  233. for method in methods:
  234. for directed in (True, False):
  235. check(method, directed)
  236. def test_construct_shortest_path():
  237. def check(method, directed):
  238. SP1, pred = shortest_path(directed_G,
  239. directed=directed,
  240. overwrite=False,
  241. return_predecessors=True)
  242. SP2 = construct_dist_matrix(directed_G, pred, directed=directed)
  243. assert_array_almost_equal(SP1, SP2)
  244. for method in methods:
  245. for directed in (True, False):
  246. check(method, directed)
  247. def test_unweighted_path():
  248. def check(method, directed):
  249. SP1 = shortest_path(directed_G,
  250. directed=directed,
  251. overwrite=False,
  252. unweighted=True)
  253. SP2 = shortest_path(unweighted_G,
  254. directed=directed,
  255. overwrite=False,
  256. unweighted=False)
  257. assert_array_almost_equal(SP1, SP2)
  258. for method in methods:
  259. for directed in (True, False):
  260. check(method, directed)
  261. def test_negative_cycles():
  262. # create a small graph with a negative cycle
  263. graph = np.ones([5, 5])
  264. graph.flat[::6] = 0
  265. graph[1, 2] = -2
  266. def check(method, directed):
  267. assert_raises(NegativeCycleError, shortest_path, graph, method,
  268. directed)
  269. for method in ['FW', 'J', 'BF']:
  270. for directed in (True, False):
  271. check(method, directed)
  272. @pytest.mark.parametrize("method", ['FW', 'J', 'BF'])
  273. def test_negative_weights(method):
  274. SP = shortest_path(directed_negative_weighted_G, method, directed=True)
  275. assert_allclose(SP, directed_negative_weighted_SP, atol=1e-10)
  276. def test_masked_input():
  277. np.ma.masked_equal(directed_G, 0)
  278. def check(method):
  279. SP = shortest_path(directed_G, method=method, directed=True,
  280. overwrite=False)
  281. assert_array_almost_equal(SP, directed_SP)
  282. for method in methods:
  283. check(method)
  284. def test_overwrite():
  285. G = np.array([[0, 3, 3, 1, 2],
  286. [3, 0, 0, 2, 4],
  287. [3, 0, 0, 0, 0],
  288. [1, 2, 0, 0, 2],
  289. [2, 4, 0, 2, 0]], dtype=float)
  290. foo = G.copy()
  291. shortest_path(foo, overwrite=False)
  292. assert_array_equal(foo, G)
  293. @pytest.mark.parametrize('method', methods)
  294. def test_buffer(method):
  295. # Smoke test that sparse matrices with read-only buffers (e.g., those from
  296. # joblib workers) do not cause::
  297. #
  298. # ValueError: buffer source array is read-only
  299. #
  300. G = scipy.sparse.csr_matrix([[1.]])
  301. G.data.flags['WRITEABLE'] = False
  302. shortest_path(G, method=method)
  303. def test_NaN_warnings():
  304. with warnings.catch_warnings(record=True) as record:
  305. shortest_path(np.array([[0, 1], [np.nan, 0]]))
  306. for r in record:
  307. assert r.category is not RuntimeWarning
  308. def test_sparse_matrices():
  309. # Test that using lil,csr and csc sparse matrix do not cause error
  310. G_dense = np.array([[0, 3, 0, 0, 0],
  311. [0, 0, -1, 0, 0],
  312. [0, 0, 0, 2, 0],
  313. [0, 0, 0, 0, 4],
  314. [0, 0, 0, 0, 0]], dtype=float)
  315. SP = shortest_path(G_dense)
  316. G_csr = scipy.sparse.csr_matrix(G_dense)
  317. G_csc = scipy.sparse.csc_matrix(G_dense)
  318. G_lil = scipy.sparse.lil_matrix(G_dense)
  319. assert_array_almost_equal(SP, shortest_path(G_csr))
  320. assert_array_almost_equal(SP, shortest_path(G_csc))
  321. assert_array_almost_equal(SP, shortest_path(G_lil))