當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python sklearn single_source_shortest_path_length用法及代碼示例

本文簡要介紹python語言中 sklearn.utils.graph.single_source_shortest_path_length 的用法。

用法:

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

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

返回由目標鍵入的最短路徑長度的字典。

參數

graph{sparse matrix, ndarray} 形狀 (n, n)

圖的鄰接矩陣。 LIL 格式的稀疏矩陣是首選。

sourceint

路徑的起始節點。

cutoff整數,默認=無

停止搜索的深度 - 僅返回長度 <= 截止的路徑。

例子

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

相關用法


注:本文由純淨天空篩選整理自scikit-learn.org大神的英文原創作品 sklearn.utils.graph.single_source_shortest_path_length。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。