从 R 中的 `igraph` 包的 `all_shortest_paths` 函数中提取信息

Extracting information from the `all_shortest_paths` function from `igraph` package in R

对于此示例图:

set.seed(1)
g <- make_chordal_ring(15, matrix(c(3, 12, 4, 7, 8, 11), nr = 2))
k <- Vectorize(all_shortest_paths, "from", F)(g, V(g), 7)

我们拥有从图中任何给定节点开始到节点 7(参考节点)结束的所有最短路径。我想要的是计算节点 Y 在从节点 X 到节点 7 的最短路径中出现的次数。

如果i用n(1,2,7)表示从节点1到节点7经过节点2的最短路径数,节点1到节点7的最短路径总数用n( 1,7) 我想要一种方法来生成一个看起来像 :

的 table

我真的坚持这样做,例如,如果我们看一下 k:

的输出
> k[[1]][1]
$res
$res[[1]]
+ 3/15 vertices:
[1] 1 4 7

我不知道如何隔离路径 1,4,7 并将其计入 n(1,4,7)

我会选择一个简单的循环:

# initialize your matrix with all zeros
m <- matrix(0,nrow=vcount(g),ncol=vcount(g)+1)

# iterate over elements of k
for(fromVertex in 1:length(k)){

  # iterate over res entry of each element of k
  for(path in k[[fromVertex]]$res){

    # path is a vertex sequence, same type as V(g), 
    # calling as.integer we get the vertices indexes inside the sequence
    verticesOfPath <- as.integer(path)

    # we exclude the first and the last vertex (from,to)
    innerVertices <- verticesOfPath[c(-1,-length(verticesOfPath))]

    if(length(innerVertices) > 0){
      # this is not a direct path
      m[verticesOfPath[1],innerVertices] <- m[verticesOfPath[1],innerVertices] + 1
    }
    # add 1 to the last column
    m[verticesOfPath[1],ncol(m)] <- m[verticesOfPath[1],ncol(m)] + 1
  }
}

结果:

> m
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
 [1,]    0    0    0    1    0    0    0    0    0     0     0     0     0     0     0     1
 [2,]    0    0    0    0    0    1    0    0    0     0     0     0     0     0     0     1
 [3,]    0    0    0    1    0    0    0    0    0     0     0     0     0     0     0     1
 [4,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     1
 [5,]    0    0    0    1    0    1    0    0    0     0     0     0     0     0     0     2
 [6,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     1
 [7,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     1
 [8,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     1
 [9,]    0    0    0    0    0    0    0    1    0     1     0     0     0     0     0     2
[10,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     1
[11,]    0    0    0    0    0    0    0    0    0     1     0     0     0     0     0     1
[12,]    0    0    0    0    0    0    0    1    0     0     0     0     0     0     0     1
[13,]    0    0    0    0    0    0    0    0    0     1     0     0     0     0     0     1
[14,]    0    0    0    0    0    1    0    0    0     0     0     0     0     0     0     1
[15,]    0    0    0    0    0    0    0    1    0     0     0     0     0     0     0     1

如果顶点有名称属性,你可以将它们设置为矩阵的行和列名称:

rownames(m) <- V(g)$name
colnames(m) <- c(V(g)$name,'TOT')

> m
  A B C D E F G H I J K L M N O TOT
A 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0   1
B 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0   1
C 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0   1
D 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   1
E 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0   2
F 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   1
G 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   1
H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   1
I 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0   2
J 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   1
K 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0   1
L 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0   1
M 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0   1
N 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0   1
O 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0   1