library(igraph) # for Network graph
library(visNetwork) # for interaction Network graphdata <- read.table("D:/XPER/data.txt", header = F,sep = "-")
colnames(data) <- c("from", "to")igraphg1 <- graph.data.frame(data, directed = F)
plot(g1)visNetworkWe can quick plot with function visIgraph for g1 above. Which this interaction graph, we can move Nodes by click and drag; zoom-in, zoom-out; highligh Nodes nearest when we select one Node; Select by id…
visIgraph(g1) %>%
visNodes(size = 25, shape = "circle") %>%
visOptions(highlightNearest = TRUE,
nodesIdSelection = TRUE) %>%
visInteraction(keyboard = TRUE)Function largest.clique will help us find cliques have maximum nodes possible in our Network graph
#'Find largest cliques
largest <- largest.cliques(g1)
largest## [[1]]
## + 11/15 vertices, named:
## [1] 2 5 8 9 10 11 12 13 14 15 4
##
## [[2]]
## + 11/15 vertices, named:
## [1] 2 5 8 9 10 11 12 13 14 15 1
As we can see in largest, we have 2 maximum cliques with 11 Nodes : “2 5 8 9 10 11 12 13 14 15 4” and “2 5 8 9 10 11 12 13 14 15 1”
To plot this clique in our Network, we need to convert g1 to format of package visNetwork. We have g2
g2 <- toVisNetworkData(g1)And I don’t want to retype many time to plot a clique. So I create a function b.visNetwork to make it easy
b.visNetwork <- function(g2,largest,number){
g2$nodes$group<-"single"
g2$nodes$group[largest[[number]]] <- "clique"
#Plot
visNetwork(g2$nodes, g2$edges) %>%
visIgraphLayout() %>%
visNodes(size = 25, shape = "circle") %>%
visOptions(selectedBy="group",
highlightNearest = TRUE,
nodesIdSelection = TRUE) %>%
visGroups(groupname = "clique", color = "orange")%>%
visInteraction(keyboard = TRUE)%>%
visLegend()
}Now, we have 2 maximum cliques.
Plot clique 1. We can choose Select by group to seclect only clique.
b.visNetwork(g2,largest,1)Plot clique 2
b.visNetwork(g2,largest,2)If we don’t want find largest clique. We can use function cliques() to set min and max of Node. Ex: find all clique have min = 7 and max = 8
cliques <- cliques(g1, min = 7, max = 8)
head(cliques)## [[1]]
## + 7/15 vertices, named:
## [1] 1 2 5 8 9 10 11
##
## [[2]]
## + 7/15 vertices, named:
## [1] 1 2 5 8 9 10 12
##
## [[3]]
## + 7/15 vertices, named:
## [1] 1 2 5 8 9 10 13
##
## [[4]]
## + 7/15 vertices, named:
## [1] 1 2 5 8 9 10 14
##
## [[5]]
## + 7/15 vertices, named:
## [1] 1 2 5 8 9 10 15
##
## [[6]]
## + 7/15 vertices, named:
## [1] 1 2 5 8 9 11 12