找到从所有顶点到多个顶点igraph的最短路径

时间:2016-10-13 03:00:34

标签: r network-programming igraph

我试图导出一个接近度量,它使用我的图形中的所有顶点和一组顶点之间的最短距离。

我已经能够使用shortest.paths()获得最短距离。 我想获得相应的路径,以便在更改边缘权重时可以看到路径如何变化。但我无法这样做。我找到了get.shortest.path,它只能获得从一个顶点到一组顶点的最短路径。我喜欢从所有顶点到一组顶点,而不必依赖for循环。

你有什么建议吗? 您可以运行的示例代码如下:

set.seed(2222)
graph <- erdos.renyi.game(50, 0.3)
# Group of vertices that I want to know the shortest paths to.
some_function_that_can_get_the_shortest_path(graph, from=V(graph), to=V(graph)[1:10])
#Currently get.shortest.paths(graph, 1, to=V(graph)[1:10]) gets me the following 
$vpath
$vpath[[1]]
+ 1/50 vertex:
[1] 1

$vpath[[2]]
+ 3/50 vertices:
[1]  1 12  2

$vpath[[3]]
+ 2/50 vertices:
[1] 1 3

$vpath[[4]]
+ 3/50 vertices:
[1] 1 3 4

$vpath[[5]]
+ 2/50 vertices:
[1] 1 5

$vpath[[6]]
+ 2/50 vertices:
[1] 1 6

$vpath[[7]]
+ 3/50 vertices:
[1] 1 3 7

$vpath[[8]]
+ 3/50 vertices:
[1]  1 35  8

$vpath[[9]]
+ 3/50 vertices:
[1]  1 14  9

$vpath[[10]]
+ 3/50 vertices:
[1]  1 22 10

1 个答案:

答案 0 :(得分:0)

您可以尝试使用induced.subgraph对图表进行子集化,以拆分要分析的顶点集,并应用distancesshortest_paths,例如:

set.seed(2222)
graph <- erdos.renyi.game(50, 0.3)
subgraph<-induced.subgraph(graph,c(1,5,19))
subgraph2<-induced.subgraph(graph,c(20,38,50))
v1<-V(subgraph)
v1<-as.vector(v1)
v2<-V(subgraph2)
v2<-as.vector(v2)

distances(graph,v1,to=v2)

      [,1] [,2] [,3]
[1,]    0    2    1
[2,]    2    0    2
[3,]    1    2    0

或只使用get.shortest.paths

> get.shortest.paths(graph, v1, to=v2)
$vpath
$vpath[[1]]
+ 1/50 vertex:
[1] 1

$vpath[[2]]
+ 3/50 vertices:
[1]  1 12  2

$vpath[[3]]
+ 2/50 vertices:
[1] 1 3



$epath
NULL

$predecessors
NULL

$inbound_edges
NULL