I have a random directed weighted graph g
. The adjacency matrix of graph is a transition matrix TM
. I need to find the second order of transition matrix. I mean, I need to calcucalete the propability of transitions from a vertex u
to a vertex v
through a vertex t
, i.e. P(u->t->v)
.
In my attempt, firstly, I have found the out neighborhood vertecies of 1-st (green) and 2-nd (red) orders for the blue vertex (its index is 3). Secondly, I try to find the subgraph subg
. The original graph g
and subgraph subg
are shown in figure.
My expected results is:
P(C->E->O)=0.33*0.05=0.165
P(C->E->M)=0.5*0.33 =0.165
P(C->E->G)=0.33*0.33=0.1089
Question. How to select the weighted pairs (CO, EO), (CE, EM), (CE, EG) of the subg
and multiplicate their weights?
My code is here:
library(igraph)
set.seed(41)
rm(list=ls(all=TRUE))
n = 20
m = 35
g <- erdos.renyi.game(n=n, p.or.m=m, type="gnm", directed = T, loops = T)
V(g)$name <- LETTERS[1:vcount(g)]
X <- as.matrix(as_adjacency_matrix(g))
TM <- X / rowSums(X)
TM[is.nan(TM)] <- 0
g <- graph_from_adjacency_matrix(TM, weighted=TRUE)
#for(i in 1:n){
f1 <- unlist(neighborhood(g, order=1, mode="out", nodes=3)); length(f1[-1]);
f2 <- unlist(neighborhood(g, order=2, mode="out", nodes=3)); length(f2[-(1:2)])
#}
V(g)$color <- "white"
V(g)[f1[1]]$color <- "blue"
V(g)[f1[-1]]$color <- "green"
V(g)[setdiff(f2,f1)]$color <- "red"
par(mfrow=c(1,2))
plot(g, layout = layout.fruchterman.reingold,
edge.arrow.size=0.2, edge.curved=T,
edge.label = round(E(g)$weight,2))
subg <- induced.subgraph(g, f2)
plot(subg, layout = layout.fruchterman.reingold,
edge.arrow.size=0.2, edge.curved=T,
edge.label = round(E(subg)$weight,2))