single_source_shortest_path_length#

sklearn.utils.graph.single_source_shortest_path_length(graph, source, *, cutoff=None)[源代码]#

返回从源到所有可达节点的shortest path length。

参数:
graph{类数组, 稀疏矩阵},形状为 (n_nodes, n_nodes)

图的邻接矩阵。推荐使用 LIL 格式的稀疏矩阵。

sourceint

路径的起始节点。

cutoffint,默认=None

停止搜索的深度——仅返回长度 <= cutoff 的路径。

返回:
pathsdict

可到达的结束节点映射到从源节点出发的路径长度,例如 {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)]