shortest_path(csgraph, method='auto', directed=True, return_predecessors=False, unweighted=False, overwrite=False, indices=None)
As currently implemented, Dijkstra's algorithm and Johnson's algorithm do not work for graphs with direction-dependent distances when directed == False. i.e., if csgraph[i,j] and csgraph[j,i] are non-equal edges, method='D' may yield an incorrect result.
The N x N array of distances representing the input graph.
Algorithm to use for shortest paths. Options are:
'auto' -- (default) select the best among 'FW', 'D', 'BF', or 'J'
based on the input data.
'FW' -- Floyd-Warshall algorithm. Computational cost is
approximately
O[N^3]
. The input csgraph will be converted to a dense representation.'D' -- Dijkstra's algorithm with Fibonacci heaps. Computational
cost is approximately
O[N(N*k + N*log(N))]
, wherek
is the average number of connected edges per node. The input csgraph will be converted to a csr representation.'BF' -- Bellman-Ford algorithm. This algorithm can be used when
weights are negative. If a negative cycle is encountered, an error will be raised. Computational cost is approximately
O[N(N^2 k)]
, wherek
is the average number of connected edges per node. The input csgraph will be converted to a csr representation.'J' -- Johnson's algorithm. Like the Bellman-Ford algorithm,
Johnson's algorithm is designed for use when the weights are negative. It combines the Bellman-Ford algorithm with Dijkstra's algorithm for faster computation.
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]. 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]
If True, return the size (N, N) predecesor matrix
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.
If True, overwrite csgraph with the result. This applies only if method == 'FW' and csgraph is a dense, c-ordered array with dtype=float64.
If specified, only compute the paths from the points at the given indices. Incompatible with method == 'FW'.
if there are negative cycles in the graph
The 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.
Returned 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
Perform a shortest-path graph search on a positive directed or undirected graph.
>>> from scipy.sparse import csr_matrix
... from scipy.sparse.csgraph import shortest_path
>>> graph = [
... [0, 1, 2, 0],
... [0, 0, 0, 1],
... [2, 0, 0, 3],
... [0, 0, 0, 0]
... ]
... graph = csr_matrix(graph)
... print(graph) (0, 1) 1 (0, 2) 2 (1, 3) 1 (2, 0) 2 (2, 3) 3
>>> dist_matrix, predecessors = shortest_path(csgraph=graph, directed=False, indices=0, return_predecessors=True)
... dist_matrix array([0., 1., 2., 2.])
>>> predecessors array([-9999, 0, 0, 1], dtype=int32)See :
The following pages refer to to this document either explicitly or contain code examples using this.
scipy.sparse.csgraph._shortest_path.shortest_path
scipy.sparse.csgraph._tools.construct_dist_matrix
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them