Skip to content

Commit

Permalink
BUG: [sparse.csgraph] Fix a bug in dijkstra and johnson algorithm (sc…
Browse files Browse the repository at this point in the history
…ipy#17800)

* MAINT: Fix wrong 'directed' being passed in the tests for shortest path algorithms
* BUG: scipy.sparse.csgraph: Fix a bug in FibonacciHeap used for the dijkstra function
* TST: PR 17800 revisions to pin down exact test cases

---------

Co-authored-by: Tyler Reddy <tyler.je.reddy@gmail.com>
Co-authored-by: CJ Carey <perimosocordiae@gmail.com>
Co-authored-by: Kai Striega <kaistriega@gmail.com>
  • Loading branch information
4 people committed Feb 18, 2023
1 parent 2eb9b96 commit 6931375
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion scipy/sparse/csgraph/_shortest_path.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ cdef void decrease_val(FibonacciHeap* heap,
# at the leftmost end of the roots' linked-list.
remove(node)
node.right_sibling = heap.min_node
heap.min_node.left_sibling = node.right_sibling
heap.min_node.left_sibling = node
heap.min_node = node


Expand Down
48 changes: 46 additions & 2 deletions scipy/sparse/csgraph/tests/test_shortest_path.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from io import StringIO
import warnings
import numpy as np
from numpy.testing import assert_array_almost_equal, assert_array_equal
Expand All @@ -6,6 +7,7 @@
bellman_ford, construct_dist_matrix,
NegativeCycleError)
import scipy.sparse
from scipy.io import mmread
import pytest

directed_G = np.array([[0, 3, 3, 0, 0],
Expand Down Expand Up @@ -176,7 +178,7 @@ def test_dijkstra_indices_min_only(directed, SP_ans, indices):


@pytest.mark.parametrize('n', (10, 100, 1000))
def test_shortest_path_min_only_random(n):
def test_dijkstra_min_only_random(n):
np.random.seed(1234)
data = scipy.sparse.rand(n, n, density=0.5, format='lil',
random_state=42, dtype=np.float64)
Expand All @@ -186,7 +188,7 @@ def test_shortest_path_min_only_random(n):
np.random.shuffle(v)
indices = v[:int(n*.1)]
ds, pred, sources = dijkstra(data,
directed=False,
directed=True,
indices=indices,
min_only=True,
return_predecessors=True)
Expand All @@ -198,6 +200,48 @@ def test_shortest_path_min_only_random(n):
p = pred[p]


def test_dijkstra_random():
# reproduces the hang observed in gh-17782
n = 10
indices = [0, 4, 4, 5, 7, 9, 0, 6, 2, 3, 7, 9, 1, 2, 9, 2, 5, 6]
indptr = [0, 0, 2, 5, 6, 7, 8, 12, 15, 18, 18]
data = [0.33629, 0.40458, 0.47493, 0.42757, 0.11497, 0.91653, 0.69084,
0.64979, 0.62555, 0.743, 0.01724, 0.99945, 0.31095, 0.15557,
0.02439, 0.65814, 0.23478, 0.24072]
graph = scipy.sparse.csr_matrix((data, indices, indptr), shape=(n, n))
dijkstra(graph, directed=True, return_predecessors=True)


def test_gh_17782_segfault():
text = """%%MatrixMarket matrix coordinate real general
84 84 22
2 1 4.699999809265137e+00
6 14 1.199999973177910e-01
9 6 1.199999973177910e-01
10 16 2.012000083923340e+01
11 10 1.422000026702881e+01
12 1 9.645999908447266e+01
13 18 2.012000083923340e+01
14 13 4.679999828338623e+00
15 11 1.199999973177910e-01
16 12 1.199999973177910e-01
18 15 1.199999973177910e-01
32 2 2.299999952316284e+00
33 20 6.000000000000000e+00
33 32 5.000000000000000e+00
36 9 3.720000028610229e+00
36 37 3.720000028610229e+00
36 38 3.720000028610229e+00
37 44 8.159999847412109e+00
38 32 7.903999328613281e+01
43 20 2.400000000000000e+01
43 33 4.000000000000000e+00
44 43 6.028000259399414e+01
"""
data = mmread(StringIO(text))
dijkstra(data, directed=True, return_predecessors=True)


def test_shortest_path_indices():
indices = np.arange(4)

Expand Down

0 comments on commit 6931375

Please sign in to comment.