如何在加权networkx图中找到具有最高总和的路径? [英] How to find path with highest sum in a weighted networkx graph?

查看:330
本文介绍了如何在加权networkx图中找到具有最高总和的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有向网络x加权图.如何找到权重总和最大的路径?

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.

这篇关于如何在加权networkx图中找到具有最高总和的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆