I am trying to find an efficient way of simulating random meetings in a graph in R using igraph.
I managed to do it using the code below in which I assume edges appear with some probability (prob.meet) and add them to an (pre-existent) empty graph of same size.
However, for large graphs this is not efficient. Plus, I repeat this process over an over.
- The first layer of inefficiency is the random selection of edges
using the
rbin()
function (amounts to 25% of the time) - The second layer of inefficiency is adding edges to an existent empty
graph using the
add_edges()
function (amounts to 75% of the time)
Any suggestion on how to improve efficiency?
Here is what I tried:
- First, I create a random graph:
library(igraph)
nodes = 5
g1 <- barabasi.game(nodes)
EL1 <- get.edgelist(g1, names=FALSE)
- Second, I assume the edges pop up with probability "prob.meet", and add them to an empty graph. The reason I do that is to force
g_meet
to conform in size withg1
.
prob.meet = 0.5
EL_meet <- matrix(EL1[(as.logical(rbinom(nrow(EL1), 1, prob.meet))),],
nrow=2,byrow = TRUE
)
g_meet <- make_empty_graph(n = nodes) %>%
add_edges(EL_meet)