I have a directed networkx weighted graph. How do I find the path with the biggest sum of weights?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With