networkx-ksp

all_simple_paths

all_simple_paths(G, source, target, cutoff=None)[source]

Generate all simple paths in the graph G from source to target.

A simple path is a path with no repeated nodes.

Parameters:

G : NetworkX graph

source : node

Starting node for path

target : node

Ending node for path

cutoff : integer, optional

Depth to stop the search. Only paths of length <= cutoff are returned.

Returns:

path_generator: generator

A generator that produces lists of simple paths. If there are no paths between the source and target within the given cutoff the generator produces no output.

See also

all_shortest_paths, shortest_path

Notes

This algorithm uses a modified depth-first search to generate the paths [R277]. A single path can be found in \(O(V+E)\) time but the number of simple paths in a graph can be very large, e.g. \(O(n!)\) in the complete graph of order n.

References

[R277](1, 2) R. Sedgewick, “Algorithms in C, Part 5: Graph Algorithms”, Addison Wesley Professional, 3rd ed., 2001.

Examples

>>> G = nx.complete_graph(4)
>>> for path in nx.all_simple_paths(G, source=0, target=3):
...     print(path)
...
[0, 1, 2, 3]
[0, 1, 3]
[0, 2, 1, 3]
[0, 2, 3]
[0, 3]
>>> paths = nx.all_simple_paths(G, source=0, target=3, cutoff=2)
>>> print(list(paths))
[[0, 1, 3], [0, 2, 3], [0, 3]]

参考链接:https://networkx.org/documentation/networkx-1.9.1/reference/generated/networkx.algorithms.simple_paths.all_simple_paths.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值