All paths of length L from node n using python

前端 未结 5 1388
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 07:34

Given a graph G, a node n and a length L, I\'d like to collect all (non-cyclic) paths of length L that depart from n.

Do you have any idea on how to approach this?

5条回答
  •  离开以前
    2020-12-16 08:36

    This solution might be improved in terms efficiency but it seems very short and makes use of networkx functionality:

    G = nx.complete_graph(4)
    n =  0
    L = 3
    result = []
    for paths in (nx.all_simple_paths(G, n, target, L) for target in G.nodes_iter()):
        print(paths)
        result+=paths
    

提交回复
热议问题