How to find path with highest sum in a weighted networkx graph?

后端 未结 1 542
北荒
北荒 2021-01-06 12:44

I have a directed networkx weighted graph. How do I find the path with the biggest sum of weights?

1条回答
  •  孤街浪徒
    2021-01-06 13:05

    You can use all_simple_paths and check the maximum. Assuming you have a function that takes a path and gives you the sum of the weights:

    heaviest_path = max((path for path in nx.all_simple_paths(G, source, dest)),
                        key=lambda path: get_weight(path))
    

    In case two of them have the same weight, this will give you the first one found.

    0 讨论(0)
提交回复
热议问题