[英]Extracting information from the `all_shortest_paths` function from `igraph` package in R
For this example graph: 对于此示例图:
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)
We have all shortest paths starting any given node in the graph and ending with node 7 (the reference node). 我们拥有所有最短路径,从图中的任何给定节点开始,到节点7(参考节点)结束。 What I want is to count the number of times a node Y is present in the shortest path from Node X to node 7. 我要计算的是从节点X到节点7的最短路径中节点Y出现的次数。
If i denote the number of shortest paths from node 1 to node 7 that pass through node 2 by n(1,2,7), and the total number of shortest paths from node 1 to node 7 by n(1,7) I want a way to generate a table that looks something like : 如果我表示从节点1到节点7的最短路径经过n(1,2,7)的数量,而从节点1到节点7的最短路径总数由n(1,7)表示,想要一种生成如下表的方法:
I am really stuck with doing this, for example, if we look at the output of k: 例如,如果我们看一下k的输出,我真的会坚持这样做:
> k[[1]][1]
$res
$res[[1]]
+ 3/15 vertices:
[1] 1 4 7
I don't know how to isolate the path 1,4,7 and count this towards n(1,4,7) 我不知道如何隔离路径1,4,7并将其计入n(1,4,7)
I would go for a simple loop : 我会去一个简单的循环:
# 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
}
}
Result : 结果:
> 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
If vertices have name attributes, you can set them as row and col names of the matrix : 如果顶点具有名称属性,则可以将它们设置为矩阵的行名和列名:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.