Maximal cliques
Finds every group of vertices that are all adjacent to each other and can't take one more vertex.
Time complexity O(3^(V/3)), gograph function partition.MaximalCliques.
How it works
A clique is a set of vertices where every two are joined by an edge. It is maximal when no other vertex is joined to all of them, so it can't grow. partition.MaximalCliques lists every maximal clique with the Bron-Kerbosch algorithm. It grows a clique one vertex at a time from a set of candidates, and keeps the vertices it has already tried so that no clique is listed twice.
gograph adds three things that make it faster. It starts from the vertices in degeneracy order, which keeps the candidate sets small in sparse graphs. At each step it picks a pivot and only branches on candidates that aren't the pivot's neighbors, since every maximal clique holds the pivot or one of those. And it stores the sets as bitsets, so comparing them is quick.
A graph can have up to 3^(V/3) maximal cliques, so no method is fast on every graph, but real networks are usually sparse enough. Edge directions are ignored, and so are self-loops. Use it to find tight groups of friends or devices that are all linked, where one vertex can belong to several groups.
Friend groups
Twelve friends in three circles: a climbing group, a book club and a band. Each edge joins two friends. Eli climbs and reads, and Hana reads and plays in the band, so each of them links two circles.
An undirected graph with 12 vertices and 21 edges.
Which groups of friends all know each other, where no one else knows everyone in the group?
The graph has 6 maximal cliques. The 2 largest have 4 vertices each: Ana, Ben, Cara and Dev; Ivo, Jun, Kai and Lea.
Step by step
- Bron-Kerbosch grows a clique R. P holds the vertices that can still join R, and X holds the ones that could join but whose cliques were listed already. The search starts once from each vertex, in degeneracy order: repeatedly remove the vertex with the fewest neighbors left. That keeps P small. Each call picks a pivot in P or X with the most neighbors in P and tries only the vertices of P that are not its neighbors.
- Start from Ana, the first vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add Ben, Cara and Dev to R. P and X are both empty, so no other vertex is adjacent to all of Ana, Ben, Cara and Dev. Record them as maximal clique 1.
- Start from Ben, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. The pivot, Ana, is in X and adjacent to every vertex in R and P, so every clique grown from here could also take Ana. Those cliques were listed already. Back up.
- Start from Cara, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add Eli and Dev to R. P and X are both empty, so no other vertex is adjacent to all of Cara, Eli and Dev. Record them as maximal clique 2.
- Start from Dev, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. The pivot, Cara, is in X and adjacent to every vertex in R and P, so every clique grown from here could also take Cara. Those cliques were listed already. Back up.
- Start from Eli, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add Fay and Gus to R. P and X are both empty, so no other vertex is adjacent to all of Eli, Fay and Gus. Record them as maximal clique 3.
7 more steps lead to the last one:
- Done. Every vertex has been a start, and the search found 6 maximal cliques. The biggest has 4 vertices.
Use it in Go
for _, clique := range partition.MaximalCliques(g) {
var names []string
for _, v := range clique {
names = append(names, v.Label())
}
fmt.Println(strings.Join(names, ", "))
}
Example graphs
- Friend groups, 12 vertices and 21 edges
- Office network, 13 vertices and 16 edges
- Karate club, 34 vertices and 78 edges
- Campus network, 100 vertices and 114 edges
More in Partitioning
- Girvan-Newman: Splits a graph into k communities by removing, one at a time, the edge that the most shortest paths cross.
- Randomized k-cut: Splits an undirected graph into k groups by merging the ends of random edges until k groups are left, and cuts the edges between them.