{ } Raw JSON

bundles / scipy latest / 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 dimensions

The N x N array of distances representing the input graph.

source : int

The index of the starting node for the paths.

sink : int

The index of the ending node for the paths.

K : int

The number of shortest paths to find.

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]. 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, optional

If True, return the size (M, N) predecessor matrix. Default: False.

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. Default: False.

Returns

dist_array : ndarray

Array of size M of 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. M is the number of shortest paths found, which is less than or equal to K.

predecessors : ndarray

Returned only if return_predecessors == True. The M x N matrix of predecessors, which can be used to reconstruct the shortest paths. M is the number of shortest paths found, which is less than or equal to K. Row i of the predecessor matrix contains information on the i-th shortest path from the source to the sink: each entry predecessors[i, j] gives the index of the previous node in the path from the source to node j. If the path does not pass via node j, then predecessors[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

Referenced by