简体   繁体   English

R igraph:仅为所有顶点对的子集计算最短路径

[英]R igraph: calculate shortest path only for a subset of all pairs of vertices

I have a very large igraph object so that calculation of shortest paths takes a long time.我有一个非常大的 igraph object,因此最短路径的计算需要很长时间。 I am only interested in the length of the shortest path between a very small set of pairs of vertices.我只对一组非常小的顶点对之间的最短路径的长度感兴趣。 Let's say the (undirected) graph consists of 10,000 vertices and has 500,000 edges, there are shortest paths for 10,000 * 10,000 / 2 pairs of vertices, but I only need the paths between 10,000 pairs of vertices.假设(无向)图由 10,000 个顶点和 500,000 条边组成,有10,000 * 10,000 / 2对顶点的最短路径,但我只需要 10,000 对顶点之间的路径。

Is there any possibility to define not only vertices, but pairs of vertices (meaning: start and end point of the path to be calculated)?是否有可能不仅定义顶点,还定义顶点对(意思是:要计算的路径的起点和终点)?

Since you have even number of vertices to make pairs, you can divide all vertices into two groups, ie, even or odd, like below由于您有偶数个顶点来配对,您可以将所有顶点分成两组,即偶数或奇数,如下所示

v_even <- subset(V(g), !V(g) %% 2)
v_odd <- subset(V(g), !!V(g) %% 2)

and then you run shortest_paht with mapply to produce the shortest paths然后你用 mapply 运行mapply以产生shortest_paht路径

> mapply(function(x, y) shortest_paths(g, x, y)$vpath, v_even, v_odd)
[[1]]
+ 3/10 vertices, from 7125d30:
[1] 2 6 1

[[2]]
+ 2/10 vertices, from 7125d30:
[1] 4 3

[[3]]
+ 2/10 vertices, from 7125d30:
[1] 6 5

[[4]]
+ 5/10 vertices, from 7125d30:
[1] 8 5 6 2 7

[[5]]
+ 3/10 vertice

Data数据

set.seed(1)
g <- sample_gnm(10, 15)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM