【发布时间】:2013-04-04 08:31:22
【问题描述】:
自从我开始在我的编码中成功实现 Igraph 以来,我一直在想这个问题:你能用 get_all_shortest_paths 检索尽可能多的最短路径吗?假设是前 10 个。
到目前为止,我已经明白,在无向图中检索所有最短路径是没有意义的,因为在大多数情况下,它们的数量是无限的。
但是我可以只检索前 10 条最短路径吗?
我尝试使用无向的 g = Graph() 来实现这一点:
list = []
list.append(g.get_all_shortest_paths(index1,index2, "distance")[0]) # shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[1]) # second shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[2]) # third shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[3]) # forth shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[4]) # fifth shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[5]) # sixth shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[6]) # seventh shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[7]) # eigth shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[8]) # ninth shortest path
list.append(g.get_all_shortest_paths(index1,index2, "distance")[9]) # tenth shortest path
#"distance" is from g.es["distance"]
列表索引超出范围总是会给我错误。
实际上我什至无法获得list.append(g.get_all_shortest_paths(index1,index2, "distance")[1]),尽管我知道那里有超过 10 条路径。
图表没有什么特别之处。数以千计的顶点,都以某种方式连接,我的意思是各种顶点度数。
最后我想有一个 wx.spin 或 wx.ComboBox 在路径之间进行选择(即现实生活中最短的国道在冬天结冰,所以我想选择第二短的City1 和 City2 之间的道路......然后哦不......有袋鼠在上面跳,所以我选择第三条,或者更好的第四条路,因为我知道麦当劳,我真的很喜欢吃垃圾食品 bla bla)
我知道最短!= 短,但我需要类似的东西:如果最短不好,那么忽略它,转到第二个等等。 我已经在 Google 上搜索过,但我不清楚我该怎么做。
提前感谢您!
编辑:我可能会提到list.append(g.get_all_shortest_paths(index1,index2, "distance")[1]) 当然在有 2 条最短路径自然完全相同的距离时工作。
重要更新:
由于我不光彩地误解了g.get_all_shortest_paths,这部分将在我的代码中更改为g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath"),我还提到该图是加权的,但无论如何这一点很明显:
list = []
g.es["weight"] = distance
list.append(g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath")[0]) # shortest path
list.append(g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath")[1]) # second shortest path
list.append(g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath")[2]) # third shortest path
list.append(g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath")[3]) # forth shortest path
list.append(g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath")[4]) # fifth shortest path
list.append(g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath")[5]) # sixth shortest path
list.append(g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath")[6]) # seventh shortest path
list.append(g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath")[7]) # eigth shortest path
list.append(g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath")[8]) # ninth shortest path
list.append(g.get_shortest_paths(index1, index2, weights=distance, mode=ALL, output="vpath")[9]) # tenth shortest path
#"distance" is a list containing the weights
# graph is TRUE-ly weighted now
如何获取两个顶点之间的前 10 条或所有短路径?
这个问题仍在寻找被接受的答案。谢谢。
(P.S. 我在问题的第一部分中保留了错误的方法,因为可能有像我这样的其他巨大的 idiobooms 尝试同样的事情)
【问题讨论】:
-
您可能会遗漏两件事: 1. 即使循环图中的 路径 的数量通常可能是无限的,但 shortest 的数量i> 任何两个节点之间的路径都是有限的,除非您有零权重的边、具有负总权重的循环或任何其他类似的邪恶。 2.
get_all_shortest_paths不支持边权重,所以第三个参数不是保存距离的属性名。 -
3.您计划做的事情将非常低效,因为您将计算同一节点对之间的所有最短路径 10 次。只需计算一次所有最短路径并将列表切片:
paths = g.get_all_shortest_paths(index1, index2)[:10]。 -
啊,我明白了。我会试一试的。第三个论点似乎工作正常。这是一个错误的信念吗?严肃的问题,因为我有一系列属性,例如“道路代码”、“难度”等。我使用了
g.get_all_shortest_paths(index1, index2,"difficulty"),它似乎是一个具有“难度”给出的值的权重。我这么说是因为它到目前为止有效。我不明白。 -
第三个参数不能正常工作。该函数在那里接受任何字符串,但唯一有意义的值是
in、out和all;它们确定允许遍历有向边的方向。 -
+1 为您所有非常有用的 cmets Tamás。再次感谢你。不过我很震惊,因为我花了很多时间相信第三个论点正在按照我的想法工作。我现在需要改变那里的一切。已经在查找 is_weighted() 。您能否提出一些进一步的文档来从第三个属性中制作加权图并完成整个问题的相关内容?如果您想到它们,只需链接就足够了。我不想阻止你开发伟大的图书馆:)
标签: python graph path shortest-path igraph