single_source_shortest_path_length#
- sklearn.utils.graph.single_source_shortest_path_length(graph, source, *, cutoff=None)[source]#
返回从源到所有可达节点的shortest path length。
- 参数:
- graph{array-like, sparse matrix} of shape (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)]