Generate Random network models with specified number of edges in r

旧巷老猫 提交于 2019-12-11 01:21:32

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/37178286/generate-random-network-models-with-specified-number-of-edges-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!