我正在寻找所有可能的最短路径
这是我的代码:
import networkx as nx
g=nx.Graph()
e=[('a', 'b', 2), ('a', 'c', 6), ('b', 'c', 4), ('c', 'e', 5), ('c', 'f', 1)]
paths=nx.shortest_paths(g,'a','c',weight=True)
print('%s' %list(paths))
输出如下:
^{pr2}$
根据权重,a->;b->;c也是最短路径。在
为什么它没有投入产出?在
Tags:
我不能在我的笔记本上运行你的代码。在
所以我尝试使用
^{pr2}$all_shortest_paths
方法,也许在某种程度上他们类似的。这里我的代码是:我得到了相同的输出,我阅读了networkx文档关于all_shortest_paths:
所以我假设
weight=True
是无效的,所以任何不存在的edge属性都默认为1,这就是为什么你不能得到你想要的结果。在如果修改代码并将
weight=True
更改为weight='weight'
。在您将获得:
希望这有帮助。在
使用
all_shortest_paths
函数代替shortest_paths
。在请尝试以下代码:
输出:
^{pr2}$相关问题 更多 >
编程相关推荐