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)]