# Checking the consistency of the "swap" ------------------------------------
# A graph with known structure (see Milo 2004)
n <- 5
x <- matrix(0, ncol=n, nrow=n)
x <- as(x, "dgCMatrix")
x[1,c(-1,-n)] <- 1
x[c(-1,-n),n] <- 1
x
# Simulations (increase the number for more precission)
set.seed(8612)
nsim <- 1e4
w <- sapply(seq_len(nsim), function(y) {
# Creating the new graph
g <- rewire_graph(x,p=nlinks(x)*100, algorithm = "swap")
# Categorizing (tag of the generated structure)
paste0(as.vector(g), collapse="")
})
# Counting
coded <- as.integer(as.factor(w))
plot(table(coded)/nsim*100, type="p", ylab="Frequency %", xlab="Class of graph", pch=3,
main="Distribution of classes generated by rewiring")
# Marking the original structure
baseline <- paste0(as.vector(x), collapse="")
points(x=7,y=table(as.factor(w))[baseline]/nsim*100, pch=3, col="red")
Run the code above in your browser using DataLab