当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。