-
Notifications
You must be signed in to change notification settings - Fork 13
/
clique.community.opt.par.R
46 lines (35 loc) · 1.1 KB
/
clique.community.opt.par.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
clique.community.opt.par <- function(graph, k){
require(igraph)
if(!require(foreach)){
install.packages("foreach")
library(foreach)
}
###################################
### STEP #1: Clique discovery
###################################
clq <- cliques(graph, min=k, max=k) %>% lapply(as.vector)
###################################
### STEP #2: Clique-graph creation
###################################
#find edges between cliques
edges <- c()
edges <- foreach (i=1:(length(clq)-1), .combine=c) %dopar%
{
tmp_edg <- list()
for (j in (i+1):length(clq)) {
if ( length(unique(c(clq[[i]], clq[[j]]))) == k+1 ) {
tmp_edg[[length(tmp_edg)+1]] <- c(i,j)
#tmp_edg <- c(tmp_edg, c(i,j))
}
}
return(tmp_edg)
}
#Create an empty graph and then adding edges
clq.graph <- make_empty_graph(n = length(clq)) %>% add_edges(unlist(edges))
clq.graph <- simplify(clq.graph)
V(clq.graph)$name <- seq_len(vcount(clq.graph))
comps <- decompose.graph(clq.graph)
lapply(comps, function(x) {
unique(unlist(clq[ V(x)$name ]))
})
}