图中的前10条最短路径-Igraph 0.6-Python 2.7

图中的前10条最短路径-Igraph 0.6-Python 2.7,python,graph,path,shortest-path,igraph,Python,Graph,Path,Shortest Path,Igraph,自从我开始成功地将Igraph实现到我的编码中以来,我一直在想:你能用获取所有最短路径吗。让我们先说10 到目前为止,我知道在无向图中检索所有最短路径是没有意义的,因为在大多数情况下,它们的数量是无限的 但是我能简单地检索前10条最短路径吗? 我尝试用无向g=Graph()实现这一点: 列表索引超出范围总是会给我错误。 事实上,我甚至无法获取list.append(g.get\u all\u shortest\u path(index1,index2,“distance”)[1]),尽管我知道那

自从我开始成功地将Igraph实现到我的编码中以来,我一直在想:你能用获取所有最短路径吗。让我们先说10

到目前为止,我知道在无向图中检索所有最短路径是没有意义的,因为在大多数情况下,它们的数量是无限的

但是我能简单地检索前10条最短路径吗?

我尝试用无向g=Graph()实现这一点:

列表索引超出范围总是会给我错误。 事实上,我甚至无法获取
list.append(g.get\u all\u shortest\u path(index1,index2,“distance”)[1])
,尽管我知道那里有10多条路径

这张图没有什么特别之处。数千个顶点,都以某种方式连接,我指的是不同的顶点度

最后,我希望有一个wx.spin或wx.ComboBox在路径之间进行选择(也就是说,现实生活中最短的一条国道在冬天结冰,所以我想在1号城市和2号城市之间走第二条最短的路……然后,哦,不……这条路上到处都是坎戈罗斯人,所以我走第三条路,或者更好的第四条路,因为我知道那里有麦当劳,我真的很喜欢吃垃圾食品等等)

我知道shortest!=short,不过我需要这样的东西:如果shortest不好,那么忽略它,转到second,依此类推。 我搜索过谷歌,但我不清楚我该怎么做

提前谢谢你

编辑:我可能会提到
list.append(g.get\u all\u shortest\u path(index1,index2,“distance”)[1])
当然,当有两条最短路径具有完全相同的距离时,它就起作用了

重要更新:

由于我不光彩地误解了
g.get\u all\u shortest\u path
这一部分将在我的代码中更改为
g.get\u shortest\u path(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条或所有短路径?

这个问题仍在等待答案被接受。谢谢


(顺便说一句,我在问题的第一部分保留了错误的方法,因为可能有其他像我这样的巨大白痴也在尝试同样的事情)

好吧,我仍然不确定“前10条最短路径”是什么意思,但我认为你可能想要实现。你有一个图,图中的边用实际路径标记(比如说,欧几里得)两个端点的距离。给您两个点,您希望在它们之间找到一些备用路径,同时尽量缩短这些路径。请注意,由于图形是加权的,所以两点之间不太可能有许多最短路径-为了使所有路径都是最短的,它们必须总重量完全相同。如果你的重量测量的是“乌鸦飞”时的实际距离,这种情况不太可能同时发生-您要查找的十条路径的长度略有不同。因此,
get\u all\u shortest\u path
对您没有用处,不仅因为它不使用权重,而且因为即使使用了权重,您也不太可能在具有精确权重的两点之间找到多条路径长度相同

一个备选方案是“代码> GETS80StReTysPrase<代码>,它可以考虑权重,但它只返回一对顶点之间的一条路径。(为什么它被称为代码> GETS80StReTyPrase是因为如果指定多个目标顶点,它会返回多条路径——更确切地说,它为每个目标顶点提供了一条路径)因此,您不能使用

get_shortest_path
来获取您正在查找的十条路径


通过谷歌搜索,我找到了一个可能对您有用的工具。它不是
igraph
的一部分,但您可以从
igraph
保存图形,使用
os.system
或Python中的
子流程
模块调用k-最短路径算法的编译可执行文件,然后以某种方式解析结果

好的,我仍然不确定你所说的“前10条最短路径”是什么意思,但我认为你可能想要实现这一点。你有一个图,图中的边用实际的(比如欧几里得)标记两个端点的距离。给您两个点,您希望在它们之间找到一些备用路径,同时尽量缩短这些路径。请注意,由于图形是加权的,所以两点之间不太可能有许多最短路径-为了使所有路径都是最短的,它们必须总重量完全相同。如果你的重量测量的是“乌鸦飞”时的实际距离,这种情况不太可能同时发生-您要查找的十条路径的长度略有不同。因此,
get\u all\u shortest\u path
对您没有用处,不仅因为它不使用权重,而且因为即使使用了权重,您也不太可能在具有精确权重的两点之间找到多条路径长度相同

一个备选方案是“代码> GETS80StReTysPrase<代码>,它可以考虑权重,但它只返回一对顶点之间的一条路径。(为什么它被称为代码> GETS80StReTyPrase是因为如果指定多个目标顶点,它会返回多条路径——更确切地说,它为每个目标顶点提供了一条路径)因此,您不能使用

get_shortest_path
来获取您正在查找的十条路径

通过谷歌搜索,我找到了一个可能对您有用的。它不是igraph的一部分,但您可以从igraph保存图形,调用k-最短路径算法的编译可执行文件
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