single_source_shortest_path_length#
- sklearn.utils.graph.single_source_shortest_path_length(graph, source, *, cutoff=None)[source]#
返回从源节点到所有可达节点的最短路径长度。
- 参数:
- graph{array-like, sparse matrix} 形状为 (n_nodes, n_nodes)
图的邻接矩阵。稀疏矩阵,LIL格式优先。
- sourceint
路径的起始节点。
- cutoffint, default=None
停止搜索的深度 — 仅返回长度小于等于 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)]