Return the length of the shortest path from source to all reachable nodes.
Adjacency matrix of the graph. Sparse matrix of format LIL is preferred.
Start node for path.
Depth to stop the search - only paths of length <= cutoff are returned.
Reachable end nodes mapped to length of path from source, i.e. {end: path_length}.
>>> from sklearn.utils.graph import single_source_shortest_path_length
>>> import numpy as np
>>> graph = np.array([[ 0, 1, 0, 0],
... [ 1, 0, 1, 0],
... [ 0, 1, 0, 0],
... [ 0, 0, 0, 0]])
>>> single_source_shortest_path_length(graph, 0)
{0: 0, 1: 1, 2: 2}
>>> graph = np.ones((6, 6))
>>> sorted(single_source_shortest_path_length(graph, 2).items())
[(0, 1), (1, 1), (2, 0), (3, 1), (4, 1), (5, 1)]
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.utils.graph.single_source_shortest_path_length.html