python graph_tool: 得到_all_条最短路径

python graph_tool: get _all_ shortest paths

我想计算图中所有对之间的所有最短路径。为此,我对图中的每个节点对使用 graph_tool 的 all_shortest_paths 函数。根据文档,如果给定,该函数能够考虑边权重。乍一看这很好用。但是,我发现返回的最短路径列表不完整。它似乎只包括最短路径,它也使用最短路径的完整集合中最少的跳数。

这是一个小例子:

import graph_tool
import graph_tool.topology

#setup graph
g = graph_tool.Graph()
g.add_vertex(5)
edges = [(0,1),(1,2),(3,2),(0,4),(4,3)]
metrics = [3, 4, 2, 1, 3]
g.edge_properties["metric"] = g.new_edge_property("int")
for i in range(len(metrics)):
    e = g.add_edge(*(edges[i]))
    g.edge_properties["metric"][e] = metrics[i]

#compute all shortest paths from 0 to 2
paths = graph_tool.topology.all_shortest_paths(g, 0, 2, weights=g.edge_properties["metric"])

for path in paths:
    print(path)

print("-"*10)

#increase metric of edge 0-4
g.edge_properties["metric"][g.edge(0,4)] = 2

#recompute all shortest paths from 0 to 2
paths = graph_tool.topology.all_shortest_paths(g, 0, 2, weights=g.edge_properties["metric"])

for path in paths:
    print(path)

它生成一个图,其中有 5 个顶点和边,形成从顶点 0 到顶点 2 的 2 条路径,如下所示:

0 --- 1 --- 2
 \         /
  \       /
   4 --- 3

显然,就跳数而言,路径 [0, 1, 2] 比 [0, 4, 3, 2] 短。如果没有给出度量标准,则可以正确识别(此处未演示)。

在示例的开头,边缘以这样的方式加权,即具有更多跃点的第二条路径是 'shorter'。度量总和为 6,而另一条路径的总值为 7。因此,该算法正确 returns [0, 4, 3, 2].

然后,0和4之间的边的度量值增加1。现在两条路径的总值相同,应该都返回。然而,该算法只有 returns [0, 1, 2]。我只能假设跳数仍然以某种方式考虑在内,即使我指定了一个度量,这就是第二条路径被忽略的原因。据我所知,官方文档中没有提到这种行为。

我是不是忽略了什么?有没有更好的功能来做到这一点,也许即使是不同的图书馆?我已经研究过 igraph 作为替代方案,但它似乎只能计算每个节点对的一条最短路径。

这种行为确实是图形工具中的错误,在使用权重时会发生!我刚刚提交了一个解决方案:https://git.skewed.de/count0/graph-tool/commit/dc06771604dfd8f38d40e68ce16b537bc1afc272

感谢您看到这个,并提供了非常清晰的示例!