生成r中具有指定边数的随机网络模型 [英] Generate Random network models with specified number of edges in r

查看:368
本文介绍了生成r中具有指定边数的随机网络模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成随机网络,并想将该网络与我的原始网络进行比较,该原始网络具有16809个节点和173393个边缘.因此,为了将其与不同的netwok模型进行比较,我将不得不生成具有相同边数的网络模型.在erdos.renyi模型中,我可以生成具有指定边数的随机图.如何使用r中的igraph库生成具有相同数量边的无标度和小世界网络.

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.

我的示例脚本如下.

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)

如何生成边缘数为173393的随机网络g_scale和g_small.

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

推荐答案

简短的答案是,这有点儿怪异,您需要决定使用哪种策略来舍入或舍弃边的数量.

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.

对于小世界,由于它是一个高度有序的结构,因此很难精确地指定每个节点以相同的度数开始并随机重新布线所需的边数.我能想到的最好的办法是制作下一个最大的网络并随机删除边缘:

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

特惠附件

对于BA网络,同样,它希望有一定数量的边缘进入.您可以使用out.seq参数指定每步添加多少个边缘:

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.

这篇关于生成r中具有指定边数的随机网络模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆