单源最短路径长度#
- sklearn.utils.graph.single_source_shortest_path_length(graph, source, *, cutoff=None)[source]#
- 返回从源节点到所有可达节点的最短路径长度。 - 参数:
- graph形状为 (n_nodes, n_nodes) 的 {数组, 稀疏矩阵}
- 图的邻接矩阵。建议使用 LIL 格式的稀疏矩阵。 
- sourceint
- 路径的起始节点。 
- cutoffint, 默认值=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)] 
