bundles / scipy latest / scipy / sparse / csgraph / _shortest_path / dijkstra
cython_function_or_method
scipy.sparse.csgraph._shortest_path:dijkstra
Summary
Dijkstra algorithm using priority queue
Extended Summary
Parameters
csgraph: array_like, or sparse array or matrix, 2 dimensionsThe N x N array of non-negative distances representing the input graph.
directed: bool, optionalIf 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, optionalif specified, only compute the paths from the points at the given indices.
return_predecessors: bool, optionalIf True, return the size (N, N) predecessor matrix.
unweighted: bool, optionalIf 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, optionalThe 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).
min_only: bool, optionalIf False (default), for every node in the graph, find the shortest path from every node in indices. If True, for every node in the graph, find the shortest path from any of the nodes in indices (which can be substantially faster).
Returns
dist_matrix: ndarray, shape ([n_indices, ]n_nodes,)The matrix of distances between graph nodes. If min_only=False, dist_matrix has shape (n_indices, n_nodes) and dist_matrix[i, j] gives the shortest distance from point i to point j along the graph. If min_only=True, dist_matrix has shape (n_nodes,) and contains for a given node the shortest path to that node from any of the nodes in indices.
predecessors: ndarray, shape ([n_indices, ]n_nodes,)If
min_only=False, this has shape(n_indices, n_nodes), otherwise it has shape(n_nodes,). If indices is None andmin_only=Falsethenn_indices = n_nodesand the shape of the matrix becomes(n_nodes, n_nodes). 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] = -9999sources: ndarray, shape (n_nodes,)Returned only if min_only=True and return_predecessors=True. Contains the index of the source which had the shortest path to each target. If no path exists within the limit, this will contain -9999. The value at the indices passed will be equal to that index (i.e. the fastest way to reach node i, is to start on node i).
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.
If multiple valid solutions are possible, output may vary with SciPy and Python version.
Examples
from scipy.sparse import csr_array 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_array(graph)✓
print(graph)
✗dist_matrix, predecessors = dijkstra(csgraph=graph, directed=False, indices=0, return_predecessors=True) dist_matrix predecessors✓
Aliases
-
scipy.sparse.csgraph.dijkstra