bundles / scipy 1.17.1 / scipy / sparse / csgraph / _shortest_path / yen
cython_function_or_method
scipy.sparse.csgraph._shortest_path:yen
Signature
def yen ( csgraph , source , sink , K , * , directed = True , return_predecessors = False , unweighted = False ) Summary
Yen's K-Shortest Paths algorithm on a directed or undirected graph.
Extended Summary
Parameters
csgraph: array_like, or sparse array or matrix, 2 dimensionsThe N x N array of distances representing the input graph.
source: intThe index of the starting node for the paths.
sink: intThe index of the ending node for the paths.
K: intThe number of shortest paths to find.
directed: bool, optionalIf
True(default), then find the shortest path on a directed graph: only move from pointito pointjalong pathscsgraph[i, j]. If False, then find the shortest path on an undirected graph: the algorithm can progress from point i to j alongcsgraph[i, j]orcsgraph[j, i].return_predecessors: bool, optionalIf
True, return the size(M, N)predecessor matrix. Default:False.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. Default:False.
Returns
dist_array: ndarrayArray of size
Mof shortest distances between the source and sink nodes.dist_array[i]gives the i-th shortest distance from the source to the sink along the graph.Mis the number of shortest paths found, which is less than or equal toK.predecessors: ndarrayReturned only if
return_predecessors == True. The M x N matrix of predecessors, which can be used to reconstruct the shortest paths.Mis the number of shortest paths found, which is less than or equal toK. Rowiof the predecessor matrix contains information on thei-th shortest path from the source to the sink: each entrypredecessors[i, j]gives the index of the previous node in the path from the source to nodej. If the path does not pass via nodej, thenpredecessors[i, j] = -9999.
Raises
: NegativeCycleError:If there are negative cycles in the graph
Notes
Yen's algorithm is a graph search algorithm that finds single-source K-shortest loopless paths for a graph with nonnegative edge cost. The algorithm was published by Jin Y. Yen in 1971 and employs any shortest path algorithm to find the best path, then proceeds to find K - 1 deviations of the best path.
The algorithm is based on Dijsktra's algorithm for finding each shortest path. In case there are negative edges in the graph, Johnson's algorithm is applied.
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 yen✓
graph = [ [0, 1, 2, 0], [0, 0, 0, 1], [2, 0, 0, 3], [0, 0, 0, 0] ] graph = csr_array(graph)✓
print(graph)
✗dist_array, predecessors = yen(csgraph=graph, source=0, sink=3, K=2, directed=False, return_predecessors=True) dist_array✓
predecessors
✗Aliases
-
scipy.sparse.csgraph.yen