Skip to content

Instantly share code, notes, and snippets.

@tpoisot
Created November 10, 2014 22:45
Show Gist options
  • Save tpoisot/2cb1a7b3fe39c790d32b to your computer and use it in GitHub Desktop.
Save tpoisot/2cb1a7b3fe39c790d32b to your computer and use it in GitHub Desktop.
Graph permutation with some constraints.
library(igraph)
library(plyr)
nl <- function(gl) sum(aaply(gl, 1, function(x) x[1] == x[2]))
nd <- function(gl) nrow(gl) - nrow(unique(aaply(gl, 1, sort)))
permut_graph <- function(g){
gl <- get.edgelist(g)
L <- nl(gl)
D <- nd(gl)
N <- nrow(gl)
diff_L <- TRUE
diff_D <- TRUE
diff_N <- TRUE
while(diff_L | diff_D | diff_N)
{
tgl <- gl
tgl[,2] <- sample(tgl[,2])
tgl <- unique(tgl)
diff_L <- nl(tgl) != L
diff_D <- nd(tgl) != D
diff_N <- nrow(tgl) != N
}
return(graph.edgelist(tgl))
}
par(mfcol=c(2,2))
g <- graph.de.bruijn(2, 3)
plot(g)
for(i in 1:3) plot(permut_graph(g))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment