bundles / scipy latest / scipy / sparse / csgraph / _shortest_path / floyd_warshall
cython_function_or_method
scipy.sparse.csgraph._shortest_path:floyd_warshall
Signature
def floyd_warshall ( csgraph , directed = True , return_predecessors = False , unweighted = False , overwrite = False ) Summary
Compute the shortest path lengths using the Floyd-Warshall algorithm
Extended Summary
Parameters
csgraph: array_like, or sparse array or matrix, 2 dimensionsThe N x N array of 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]. If False, then find the shortest path on an undirected graph: the algorithm can progress from point i to j along csgraph[i, j] or csgraph[j, i]
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.
overwrite: bool, optionalIf True, overwrite csgraph with the result. This applies only if csgraph is a dense, c-ordered array with dtype=float64.
Returns
dist_matrix: ndarrayThe N x N matrix of distances between graph nodes. dist_matrix[i,j] gives the shortest distance from point i to point j along the graph.
predecessors: ndarrayReturned only if return_predecessors == True. The N x N 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
Raises
: NegativeCycleError:if there are negative cycles in the graph
Notes
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 floyd_warshall✓
graph = [ [0, 1, 2, 0], [0, 0, 0, 1], [2, 0, 0, 3], [0, 0, 0, 0] ] graph = csr_array(graph)✓
print(graph)
✗dist_matrix, predecessors = floyd_warshall(csgraph=graph, directed=False, return_predecessors=True) dist_matrix predecessors✓
Aliases
-
scipy.sparse.csgraph.floyd_warshall