[英]Find all shortest paths between all pairs of nodes in NetworkX
I am trying to get all shortest paths between all pairs of nodes in an undirected unweighted graph.我试图在无向未加权图中获得所有节点对之间的所有最短路径。 I am currently using
nx.all_pairs_shortest_path()
, but I don't understand why it only returns one shortest path for every pair of nodes.我目前正在使用
nx.all_pairs_shortest_path()
,但我不明白为什么它只为每对节点返回一个最短路径。 There are cycles in my graph so there should exist multiple shortest paths between certain nodes.我的图中有循环,因此某些节点之间应该存在多条最短路径。 Any suggestions?
有什么建议?
Iterate over all nodes in the graph:迭代图中的所有节点:
results = []
for n1 in G.nodes():
for n2 in G.nodes():
shortest_path = nx.single_source_dijkstra(G, source=n1, target=n2, weight=f)
results.append(shortest_path)
I may be late, but I just came across the same problem and this was my solution:我可能迟到了,但我刚刚遇到了同样的问题,这是我的解决方案:
def all_shortest_paths(G):
a = list(nx.all_pairs_shortest_path(G))
all_sp_list = []
for n in range(len(G.nodes)):
a1 = a[n][1]
for k,v in a1.items():
all_sp_list.append(len(v))
return all_sp_list
Every other way I tried was getting very very slow because my graph had a bunch of nodes, so this was my fastest solution.我尝试的所有其他方法都变得非常慢,因为我的图形有一堆节点,所以这是我最快的解决方案。
I stumbled upon this problem myself and arrived her in my quest for a solution.我自己偶然发现了这个问题,并在我寻求解决方案的过程中找到了她。 unfortunately networkx doesn't have a function to calculate all the shortest pathes for every pair of node.
不幸的是,networkx 没有计算每对节点的所有最短路径的功能。 Moreover the answer from Igor Michetti wasn't giving what i wanted at all but it might have been tweekable.
此外,来自 Igor Michetti 的回答根本没有给出我想要的,但它可能是 tweekable。
The answer from math_noob was good because it was close enough for me to make up a solution but the problem was that it was way way too slow. math_noob 的答案很好,因为它足够接近我来解决问题,但问题是它太慢了。
def single_source_shortest_paths(graph,source):
shortest_paths_dict = {}
for node in graph:
shortest_paths_dict[node] = [nx.all_shortest_paths(graph,source,node)]
return shortest_paths_dict
def all_shortest_paths(graph):
for source in graph:
yield source, single_source_shortest_paths(source)
So i went back on networkx documentation and try to find one last time if there was any function i could use.所以我回到了 networkx 文档并尝试找到最后一次是否有任何我可以使用的功能。 but there was none so i decided i would implment it myself.So i first tried to implement everything by had which was a bit chaotic to only realise that it was a bit better but not that much so i decided i would try to look into the source code and found there a holy grail which is the
nx.predecessor
function.但是没有,所以我决定自己实现它。所以我首先尝试通过 had 实现所有东西,这有点混乱,只是意识到它好一点但不是那么多,所以我决定我会尝试研究源代码,并在那里找到了一个圣杯,它是
nx.predecessor
函数。
This function is called on the graph and the source node only so it doesn't depends on the target node and it is the one doing most of the hard work.此函数仅在图和源节点上调用,因此它不依赖于目标节点,并且它是完成大部分艰苦工作的节点。 So i just recreated the function
single_source_shortest_paths
but by calling only once nx.predecessor
per source node and then doing the same as all_shortest_path
which only consist of calling another function with the right arguments.所以我只是重新创建了函数
single_source_shortest_paths
但通过每个源节点只调用一次nx.predecessor
然后执行与all_shortest_path
相同的all_shortest_path
,它只包括调用具有正确参数的另一个函数。
def single_source_shortest_paths_pred(graph,source):
shortest_paths_dict = {}
pred = nx.predecessor(graph,source)
for node in graph:
shortest_paths_dict[node] = [nx.algorithms.shortest_paths.generic._build_paths_from_predecessors([source], node, pred)]
return shortest_paths_dict
In terms of time nx.predecessor
takes the majority of the time to execute so the second function is around n times faster where n is the number of nodes in the graph就时间而言,
nx.predecessor
花费了大部分时间来执行,因此第二个函数大约快 n 倍,其中 n 是图中的节点数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.