我想生成一个有100个节点的无向网络,其中一半节点的度数为10,另一半节点的度数为3.这样的网络是否可以在没有自循环的情况下构建?
使用下面指定的代码:
library(graph)
degrees=c(rep(3,50),rep(10,50))
names(degrees)=paste("node",seq_along(degrees)) #nodes must be names
x=randomNodeGraph(degrees)
Run Code Online (Sandbox Code Playgroud)
我可以获得这样的图表,但包括自循环.
有没有办法获得没有自循环的图形?
小智 2
使用 Bioconductor 的图形包很容易做到(参见此处)
#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
Run Code Online (Sandbox Code Playgroud)