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,默认=无
停止搜索的深度-仅返回长度<= 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)]