Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating undirected network with pre-specified degree distribution without any self loops

Tags:

r

I would like to generate an undirected network with 100 nodes, where half of the nodes have a degree of 10 and the other half has a degree of 3. Is such a network possible to construct without self loops?

Using the code specified below:

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

I can obtain such a graph, but there are self-loops included.

Is there any way to get a graph without self loops?

like image 878
upabove Avatar asked Oct 20 '22 15:10

upabove


1 Answers

It is easy to do with the graph package from Bioconductor (see here)

#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
like image 165
Jeremy Coyle Avatar answered Oct 31 '22 13:10

Jeremy Coyle