首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生成具有预先指定的度分布而没有任何自环的无向网络

生成具有预先指定的度分布而没有任何自环的无向网络
EN

Stack Overflow用户
提问于 2014-01-30 22:43:25
回答 2查看 381关注 0票数 5

我想生成一个有100个节点的无向网络,其中一半节点的度为10,另一半节点的度为3。这样的网络可以在没有自环的情况下构建吗?

使用下面指定的代码:

代码语言:javascript
复制
library(graph)
degrees=c(rep(3,50),rep(10,50))
names(degrees)=paste("node",seq_along(degrees)) #nodes must be names
x=randomNodeGraph(degrees)

我可以得到这样的图,但其中包含了自循环。

有没有办法得到一个没有自循环的图?

EN

回答 2

Stack Overflow用户

发布于 2014-01-30 23:22:47

使用Bioconductor (see here)的图形包很容易做到这一点

代码语言:javascript
复制
#install graph from Bioconductor
source("http://bioconductor.org/biocLite.R")
biocLite("graph")

#load graph and make the specified graph
library(graph)
degrees=c(rep(3,50),rep(10,50))
names(degrees)=paste("node",seq_along(degrees)) #nodes must be names
x=randomNodeGraph(degrees)

#verify graph
edges=edgeMatrix(x)
edgecount=table(as.vector(edges))
table(edgecount)
#edgecount
# 3 10 
#50 50
票数 2
EN

Stack Overflow用户

发布于 2014-01-30 23:53:35

Erdős–Gallai theorem回答了这样一个图是否可以构造的问题。

它基于图的非递减度序列,在您的情况下

代码语言:javascript
复制
ds <- c( rep( 10, 50 ), rep(3,50) )

您可以通过以下方法计算不等式的右侧

代码语言:javascript
复制
rhs <- (1:100 * 0:99) + c( rev(cumsum(rev(apply( data.frame(ds, 1:100) , 1, min ))))[-1], 0 )

左手边

代码语言:javascript
复制
lhs <- cumsum( ds )

最后:

代码语言:javascript
复制
all( lhs <= rhs )
[1] TRUE
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21459516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档