-
Notifications
You must be signed in to change notification settings - Fork 13
/
clique.community.opt.R
36 lines (27 loc) · 921 Bytes
/
clique.community.opt.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
clique.community.opt <- function(graph, k){
require(igraph)
###################################
### 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()
for (i in 1:(length(clq)-1)) {
for (j in (i+1):length(clq)) {
if ( length(unique(c(clq[[i]], clq[[j]]))) == k+1 ) {
edges[[length(edges)+1]] <- c(i,j)
}
}
}
#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 ]))
})
}