SciPy

scipy.sparse.csgraph.dijkstra

scipy.sparse.csgraph.dijkstra(csgraph, directed=True, indices=None, return_predecessors=False, unweighted=False, limit=np.inf)

Dijkstra algorithm using Fibonacci Heaps

New in version 0.11.0.

Parameters:
csgraph : array, matrix, or sparse matrix, 2 dimensions

The N x N array of non-negative distances representing the input graph.

directed : bool, optional

If True (default), then find the shortest path on a directed graph: only move from point i to point j along paths csgraph[i, j] and from point j to i along paths csgraph[j, i]. If False, then find the shortest path on an undirected graph: the algorithm can progress from point i to j or j to i along either csgraph[i, j] or csgraph[j, i].

indices : array_like or int, optional

if specified, only compute the paths for the points at the given indices.

return_predecessors : bool, optional

If True, return the size (N, N) predecesor matrix

unweighted : bool, optional

If True, then find unweighted distances. That is, rather than finding the path between each point such that the sum of weights is minimized, find the path such that the number of edges is minimized.

limit : float, optional

The maximum distance to calculate, must be >= 0. Using a smaller limit will decrease computation time by aborting calculations between pairs that are separated by a distance > limit. For such pairs, the distance will be equal to np.inf (i.e., not connected).

New in version 0.14.0.

Returns:
dist_matrix : ndarray

The matrix of distances between graph nodes. dist_matrix[i,j] gives the shortest distance from point i to point j along the graph.

predecessors : ndarray

Returned only if return_predecessors == True. The matrix of predecessors, which can be used to reconstruct the shortest paths. Row i of the predecessor matrix contains information on the shortest paths from point i: each entry predecessors[i, j] gives the index of the previous node in the path from point i to point j. If no path exists between point i and j, then predecessors[i, j] = -9999

Notes

As currently implemented, Dijkstra’s algorithm does not work for graphs with direction-dependent distances when directed == False. i.e., if csgraph[i,j] and csgraph[j,i] are not equal and both are nonzero, setting directed=False will not yield the correct result.

Also, this routine does not work for graphs with negative distances. Negative distances can lead to infinite cycles that must be handled by specialized algorithms such as Bellman-Ford’s algorithm or Johnson’s algorithm.

Examples

>>> from scipy.sparse import csr_matrix
>>> from scipy.sparse.csgraph import dijkstra
>>> graph = [
... [0, 1 , 2, 0],
... [0, 0, 0, 1],
... [0, 0, 0, 3],
... [0, 0, 0, 0]
... ]
>>> graph = csr_matrix(graph)
>>> print(graph)
  (0, 1)    1
  (0, 2)    2
  (1, 3)    1
  (2, 3)    3
>>> dist_matrix, predecessors = dijkstra(csgraph=graph, directed=False, indices=0, return_predecessors=True)
>>> dist_matrix
array([ 0.,  1.,  2.,  2.])
>>> predecessors
array([-9999,     0,     0,     1], dtype=int32)

Previous topic

scipy.sparse.csgraph.shortest_path

Next topic

scipy.sparse.csgraph.floyd_warshall