[英]Shortest path of a list of Source and Destination networkx
I have a graph with relative nodes and edges like this:我有一个带有相对节点和边的图,如下所示:
what I would like is to find a way to do the shortest_path of networkx only of the nodes included in my list, that is:我想要的是找到一种方法来仅对我列表中包含的节点执行 networkx 的最短路径,即:
source = ["10.0.11.100","10.0.12.100","10.0.13.100","10.0.14.100"]
destination = ["10.0.11.100","10.0.12.100","10.0.13.100","10.0.14.100"]
I would like not to do this by hand, that is:我不想手动执行此操作,即:
nx.shortest_path(G, source="10.0.11.100", target="10.0.12.100")
nx.shortest_path(G, source="10.0.11.100", target="10.0.13.100")
nx.shortest_path(G, source="10.0.11.100", target="10.0.14.100")
nx.shortest_path(G, source="10.0.12.100", target="10.0.11.100")
nx.shortest_path(G, source="10.0.12.100", target="10.0.13.100")
nx.shortest_path(G, source="10.0.12.100", target="10.0.14.100")
...
The result I would like looks like this:我想要的结果如下所示:
[('10.0.12.100','10.0.14.100',['10.0.1.11', '10.0.1.23']), ('10.0.14.100', '10.0.12.100', ['10.0.1.22', '10.0.1.9']), ('10.0.11.100', '10.0.14.100', ['10.0.1.6', '10.0.1.18', '10.0.1.26']), ('10.0.14.100', '10.0.11.100' ,['10.0.1.25', '10.0.1.17', '10.0.1.5'])...]
That is [(Source, Destination, [Path from Source to Destination])]即[(Source, Destination, [从Source到Destination的路径])]
Is there any way to do this?有没有办法做到这一点? Thank you very much
非常感谢
This is my code:这是我的代码:
import networkx
G = nx.DiGraph()
z = [('10.0.12.100', '10.0.1.1'),('10.0.1.1', '10.0.11.100'),('10.0.11.100', '10.0.1.2'),('10.0.1.2', '10.0.12.100'),('10.0.13.100', '10.0.1.17'),('10.0.1.17', '10.0.1.5'),('10.0.1.5', '10.0.11.100'),('10.0.11.100', '10.0.1.6'),('10.0.1.6', '10.0.1.18'),('10.0.1.18', '10.0.13.100'),('10.0.11.100', '10.0.1.6'),('10.0.1.6', '10.0.1.18'),('10.0.1.18', '10.0.1.26'),('10.0.1.26', '10.0.14.100'),('10.0.14.100', '10.0.1.22'),('10.0.1.22', '10.0.1.9'),('10.0.1.9', '10.0.12.100'),('10.0.12.100', '10.0.1.1'),('10.0.1.1', '10.0.1.6'),('10.0.1.6', '10.0.1.18'),('10.0.1.18', '10.0.13.100'),('10.0.13.100', '10.0.1.26'),('10.0.1.26', '10.0.14.100'),('10.0.13.100', '10.0.1.17'),('10.0.1.17', '10.0.1.5'),('10.0.1.5', '10.0.1.2'),('10.0.1.2', '10.0.12.100'),('10.0.14.100', '10.0.1.25'),('10.0.1.25', '10.0.1.17'),('10.0.1.17', '10.0.1.5'),('10.0.1.5', '10.0.11.100'),('10.0.12.100', '10.0.1.11'),('10.0.1.11', '10.0.1.23'),('10.0.1.23', '10.0.14.100'),('10.0.14.100', '10.0.1.25'),('10.0.1.25', '10.0.13.100')]
G.add_edges_from(z)
random_pos = nx.random_layout(G, seed=42)
pos=nx.spring_layout(G, pos=random_pos)
nx.draw(
G,
pos=pos,
node_color='#FF0000',
with_labels=True,
arrows=False
)
source = ["10.0.11.100","10.0.12.100","10.0.13.100","10.0.14.100"]
destination = ["10.0.11.100","10.0.12.100","10.0.13.100","10.0.14.100"]
list_shortest_path = []
for i in G.nodes(data=True):
DD = list_shortest_path.append(nx.shortest_path(G, source=i[0], target=i[1]))
Isn't that the combinations of source nodes?不就是源节点的组合吗?
from itertools import combinations
for n1, n2 in combinations(source, 2):
list_shortest_path.append((n1, n2, nx.shortest_path(G, source=n1, target=n2))
Simply put you can do a double for
loop:简单地说,你可以
for
一个双循环:
sources = ["10.0.11.100","10.0.12.100","10.0.13.100","10.0.14.100"]
destinations = ["10.0.11.100","10.0.12.100","10.0.13.100","10.0.14.100"]
res = []
for s in sources:
for d in destinations:
res.append((s, d, nx.shortest_path(G, source=s, target=d)))
But this may have some problems for some kind of graphs:但这对于某种图表可能会有一些问题:
In case of a DiGraph (your case), this should be the solution to go for, since it considers the shortest path from both ends ( source to destination is a different path than destination to source ).如果是有向图(您的情况),这应该是go的解决方案,因为它考虑了从两端的最短路径(源到目标的路径与目标到源的路径不同)。
If you were to use a Graph (without directed edges) the most efficient solution would look similar to @Vanojx1's answer.如果您要使用图形(没有有向边),最有效的解决方案看起来类似于@Vanojx1 的答案。
You can use itertools.product and list comprehension.您可以使用itertools.product和列表理解。
from itertools import product
list_shortest_path = [(src, dst, nx.shortest_path(G, source=src, target=dst)
for src, dst in product(sources, destinations) if src != dst]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.