简体   繁体   中英

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). 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.

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 :

在此处输入图片说明

I am really stuck with doing this, for example, if we look at the output of 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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