python - 贝尔曼福特负重量 - Networkx

标签 python graph networkx

我有这个节点列表,我想获得最小路径,因为我有负权重的节点必须使用 Bellman Ford,我正在使用 networkx 库,但是我没有得到打印路径的表格,这是具有权重的节点列表和我正在使用的命令

1 10 {'weight': 96}
1 13 {'weight': 97}
2 11 {'weight': -70}
2 13 {'weight': 77}
3 12 {'weight': 30}
3 13 {'weight': -30}
4 10 {'weight': 17}
4 14 {'weight': -75}
5 11 {'weight': -4}
5 14 {'weight': 45}
6 12 {'weight': -67}
6 14 {'weight': 63}
7 10 {'weight': 38}
7 15 {'weight': -40}
8 11 {'weight': -30}
8 15 {'weight': -46}
9 12 {'weight': 37}
9 15 {'weight': -97}




assert_raises(nx.NetworkXUnbounded,nx.bellman_ford,G_Bellman_Ford,1)

G_Bellman_Ford 是图

最佳答案

In [1]: import networkx as nx

In [2]: edges ="""1 10 {'weight': 96}
1 13 {'weight': 97}
2 11 {'weight': -70}
2 13 {'weight': 77}
3 12 {'weight': 30}
3 13 {'weight': -30}
4 10 {'weight': 17}
4 14 {'weight': -75}
5 11 {'weight': -4}
5 14 {'weight': 45}
6 12 {'weight': -67}
6 14 {'weight': 63}
7 10 {'weight': 38}
7 15 {'weight': -40}
8 11 {'weight': -30}
8 15 {'weight': -46}
9 12 {'weight': 37}
9 15 {'weight': -97}"""

In [3]: lines = edges.split('\n')

In [4]: G = nx.parse_edgelist(lines, nodetype = int, create_using=nx.DiGraph())

In [5]: nx.bellman_ford(G,1)
Out[5]: ({1: None, 10: 1, 13: 1}, {1: 0, 10: 96, 13: 97})

关于python - 贝尔曼福特负重量 - Networkx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25416232/

相关文章:

python - 如何检查列表项是否存在于另一个列表中

Python 冒险游戏 -> 在 while 循环中选择 A 或 B 不起作用!

algorithm - 计算二进制矩阵中的所有路径

python - 即使在清除之后,图形仍然不断增长

python 3,递归,或在return语句中

Python 等效于 Perl 在 if block 中匹配和捕获

c++ - 是否可以在创建图形后将节点添加到图形中?

javascript - 在D3中绘制分层圆弧

python - 如何比较两个图中的节点和边?

python - 属性错误: 'module' object has no attribute in NetworkX