sklearn.utils.graph.single_source_shortest_path_length

sklearn.utils.graph.single_source_shortest_path_length(graph, source, *, cutoff=None)

源码

返回从源到所有可达节点的最短路径长度。

返回目标确定的最短路径长度的字典。

参数 说明
graph sparse matrix or 2D array (preferably LIL matrix)
图的邻接矩阵
source integer
路径的起始节点
cutoff integer, optional
停止搜索的深度-仅返回长度<=截止的路径。

示例:

>>> 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, 1],
...                   [ 0, 0, 1, 0]])
>>> list(sorted(single_source_shortest_path_length(graph, 0).items()))
[(0, 0), (1, 1), (2, 2), (3, 3)]
>>> graph = np.ones((6, 6))
>>> list(sorted(single_source_shortest_path_length(graph, 2).items()))
[(0, 1), (1, 1), (2, 0), (3, 1), (4, 1), (5, 1)]