single_source_shortest_path_length#

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

返回從源節點到所有可到達節點的最短路徑長度。

參數:
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)]