The Issue
Given the following network of nodes and edges, I would like to derive all possible groupings of nodes where all nodes within a group are
Convert your data frame representation of the network to an igraph
object. Use max_cliques
to find "all the maximal cliques in an undirected graph".
library(igraph)
g <- graph_from_data_frame(df, directed = FALSE)
mc <- max_cliques(g, min = 1)
mc
# [[1]]
# + 1/6 vertex, named, from eb2aa45:
# [1] A
#
# [[2]]
# + 2/6 vertices, named, from eb2aa45:
# [1] D E
#
# [[3]]
# + 2/6 vertices, named, from eb2aa45:
# [1] D B
#
# [[4]]
# + 3/6 vertices, named, from eb2aa45:
# [1] B F C
Grab the names of the vertices of the maximal cliques. Create corresponding group numbers and convert to data frame:
nm <- lapply(mc, attr, "names")
d <- data.frame(g = rep(seq_len(length(nm)), lengths(nm)), vert = unlist(nm))
d
# g vert
# 1 1 A
# 2 2 D
# 3 2 E
# 4 3 D
# 5 3 B
# 6 4 B
# 7 4 F
# 8 4 C
simplify
graph, plot it, highlight vertex groups using the list above in mark.groups
. Prettify according to taste (see ?plot.igraph
).
plot(simplify(g), mark.groups = nm, mark.border = "red", mark.col = NA)