在R中生成随机图

4
我想在R中使用任何一个包生成一个随机图。
所需的输出应该是一个两列矩阵,第一列列出代理人,第二列列出以下形式的它们的连接:
1 3
1 4
1 6
1 7
2 2
2 5
3 9
3 11
3 32
3 43
3 2
4 5

我想要能够指定平均程度和最小和最大联系人数量。
最简单的方法是什么?
1个回答

9

由于您只需一个图表而不需要其他内容,我们可以非常简单地完成此操作:

actor     <- sample(1:4, 10, replace=TRUE)
receiver  <- sample(3:43, 10, replace=TRUE)
graph     <- cbind(actor,receiver)

如果你想要更具体的内容,可以看一下igraph
library(igraph)
graph <- erdos.renyi.game(21, 0.3, type=c("gnp", "gnm"),
              directed = FALSE, loops = FALSE)

# here the 0.3 is the probability of ties and 21 is the number of nodes
# this is a one mode network

或者使用专门针对双模网络的bipartite包:

library(bipartite)
web <- genweb(N1 = 5, N2 = 10, dens = 2)
web2edges(web,return=TRUE)

# here N1 is the number of nodes in set 1 and N2 the number of nodes in set 2
# and dens the average number of ties per node

有很多要考虑的因素,例如如果您想限制度分布、代理之间联系的概率等。


是的,这基本上就是我想要的。这本质上与社交网络中的随机图形相同,对吧?顺便提一句:你所说的“需要除了图形之外的任何东西”,指的是什么呢? - user1723765
很抱歉,我刚意识到我的问题缺少必要的部分。我已经编辑过了。对不起。 - user1723765
为什么在您的“type”参数中包含了ER表单?我试图理解这对对象“graph”的影响,而不仅仅是使用其中一个。 - J.Q

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接