priyanka nimavat
priyanka nimavat

Reputation: 97

Generate Random network models with specified number of edges in r

I want to generate random networks and want to compare the network with my original network that has 16809 nodes and 173393 edges. So for comparing it with different netwok models i will have to generate network model with same number of edges. In the erdos.renyi model i can generate random graph with specifying number of edges. How to generate scale-free and small world networks with same number of edges using igraph library in r.

My example script is as follows.

library(igraph)
g_erdos_renyi <- erdos.renyi.game(16809, 173393 , type = "gnm" , directed = F , loops = F)
g_scale <- barabasi.game(16809 , m = 10)
g_small <- watts.strogatz.game(1, 16809, 10, 0.05)

How to generate random networks g_scale and g_small with 173393 number of edges..??

Upvotes: 2

Views: 2096

Answers (1)

dougmet
dougmet

Reputation: 685

The short answer is that it's a bit fiddly and you need to decide what strategy to use to round up or down the number of edges.

Small World

For the small world, because it's a highly ordered structure, it's hard to specify exactly how many edges you want as each node starts with the same degree and you randomly rewire. Best I could think of was to make the next biggest network and randomly deleted edges:

n <- 16809
m <- 173393

# Work out how theye divide into each other
rem <- m %% n
div <- m %/% n

set.seed(123)
if(rem != 0) {
  g <- sample_smallworld(1, n, div+1, p = 0.001)
# Randomly delete the unwanted edges. Should be quite homegenous
  g <- delete_edges(g, sample(1:gsize(g), size = gsize(g) - m))
} else {
  g <- sample_smallworld(1, n, div, p = 0.001)
}

Preferential Attachement

For the BA network, again, it expects an ordered number of edges coming in. You can specify how many edges are added each step with the out.seq argument:

# Barabasi - Albert --------------------------------------------------------

genOutSeq <- function(n, m) {
  n <- n-1 # Shift it along
  rem <- m %% n
  c(0, rep(m%/%n + 1, rem), rep(m%/%n, n - rem))
}

n <- 16809
m <- 173393

# This creates the right number of edges but some are multiple
set.seed(11)
g <- sample_pa(n, power = 0.5, out.seq = genOutSeq(n, m),
               algorithm = "psumtree-multiple", directed = FALSE)
gsize(g)

set.seed(11)
g <- sample_pa(n, power = 0.5, out.seq = genOutSeq(n, m),
               algorithm = "psumtree", directed = FALSE)

# Randomly add the remainder

nMulti <- m - gsize(g) # Number of multiple edges that were removed

for (i in 1:nMulti) {
  vPair <- sample(1:n, size = 2)
  while (get.edge.ids(g, vPair) > 0) {
    add_edges(g, vPair)
    vPair <- sample(1:n, size = 2)
  }
}

g

As you can see the first run uses an algorithm that makes multi-edges. I worked around this by then adding them back in randomly, but that's up to you what strategy you use.

Upvotes: 1

Related Questions