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([[ 0100],
...                   [ 1010],
...                   [ 0101],
...                   [ 0010]])
>>> list(sorted(single_source_shortest_path_length(graph, 0).items()))
[(00), (11), (22), (33)]
>>> graph = np.ones((66))
>>> list(sorted(single_source_shortest_path_length(graph, 2).items()))
[(01), (11), (20), (31), (41), (51)]