本文简要介绍
networkx.algorithms.simple_paths.shortest_simple_paths
的用法。用法:
shortest_simple_paths(G, source, target, weight=None)
- 生成图 G 中从源到目标的所有简单路径,
从最短的开始。
简单路径是没有重复节点的路径。
如果要使用加权最短路径搜索,则不允许使用负权重。
- G:NetworkX 图
- source:节点
路径的起始节点
- target:节点
路径的结束节点
- weight:字符串或函数
如果是字符串,则为要用作权重的边属性的名称。
如果是函数,边的权重就是函数返回的值。该函数必须准确地接受三个位置参数:一条边的两个端点和该边的边属性字典。该函数必须返回一个数字。
如果 None 所有边都被认为具有单位权重。默认值无。
- path_generator:生成器
生成简单路径列表的生成器,按从最短到最长的顺序排列。
- NetworkXNoPath
如果源和目标之间不存在路径。
- NetworkXError
如果源或目标节点不在输入图中。
- NetworkXNotImplemented
如果输入图是 Multi[Di]Graph。
参数:
返回:
抛出:
注意:
该过程基于 Jin Y. Yen [1] 的算法。查找第一个 路径需要 操作。
参考:
- 1
Jin Y. Yen, “Finding the K Shortest Loopless Paths in a Network”, Management Science, Vol. 17, No. 11, Theory Series (Jul., 1971), pp. 712-716.
例子:
>>> G = nx.cycle_graph(7) >>> paths = list(nx.shortest_simple_paths(G, 0, 3)) >>> print(paths) [[0, 1, 2, 3], [0, 6, 5, 4, 3]]
您可以使用此函数有效地计算两个节点之间的 k 个最短/最佳路径。
>>> from itertools import islice >>> def k_shortest_paths(G, source, target, k, weight=None): ... return list( ... islice(nx.shortest_simple_paths(G, source, target, weight=weight), k) ... ) >>> for path in k_shortest_paths(G, 0, 3, 2): ... print(path) [0, 1, 2, 3] [0, 6, 5, 4, 3]
相关用法
- Python NetworkX shortest_path用法及代码示例
- Python NetworkX shortest_augmenting_path用法及代码示例
- Python NetworkX shortest_path_length用法及代码示例
- Python NetworkX shell_layout用法及代码示例
- Python NetworkX single_source_dijkstra_path_length用法及代码示例
- Python NetworkX single_source_bellman_ford用法及代码示例
- Python NetworkX subgraph_view用法及代码示例
- Python NetworkX square_clustering用法及代码示例
- Python NetworkX soft_random_geometric_graph用法及代码示例
- Python NetworkX sets用法及代码示例
- Python NetworkX simrank_similarity用法及代码示例
- Python NetworkX single_source_bellman_ford_path用法及代码示例
- Python NetworkX sudoku_graph用法及代码示例
- Python NetworkX single_source_bellman_ford_path_length用法及代码示例
- Python NetworkX single_source_shortest_path_length用法及代码示例
- Python NetworkX snap_aggregation用法及代码示例
- Python NetworkX set_edge_attributes用法及代码示例
- Python NetworkX stochastic_block_model用法及代码示例
- Python NetworkX symmetric_difference用法及代码示例
- Python NetworkX selfloop_edges用法及代码示例
- Python NetworkX second_order_centrality用法及代码示例
- Python NetworkX simulated_annealing_tsp用法及代码示例
- Python NetworkX spring_layout用法及代码示例
- Python NetworkX simple_cycles用法及代码示例
- Python NetworkX single_source_dijkstra用法及代码示例
注:本文由纯净天空筛选整理自networkx.org大神的英文原创作品 networkx.algorithms.simple_paths.shortest_simple_paths。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。