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.

Office network

Switches and rooms in a four-team office, wired through a core switch.

An undirected graph with 13 vertices and 16 edges.

Which groups of switches and rooms are all wired to each other, where nothing else is wired to all of them?

The graph has 14 maximal cliques. The largest has 3 vertices: Dev, Dev 1 and Lab.

Step by step

  1. 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.
  2. Start from Sales 1, the first vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add Sales to R. P and X are both empty, so no other vertex is adjacent to both Sales 1 and Sales. Record them as maximal clique 1.
  3. Start from Dev 2, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add Dev to R. P and X are both empty, so no other vertex is adjacent to both Dev 2 and Dev. Record them as maximal clique 2.
  4. Start from Support 1, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add Support to R. P and X are both empty, so no other vertex is adjacent to both Support 1 and Support. Record them as maximal clique 3.
  5. Start from Ops 1, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add Ops to R. P and X are both empty, so no other vertex is adjacent to both Ops 1 and Ops. Record them as maximal clique 4.
  6. Start from Shared, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add Support to R. P and X are both empty, so no other vertex is adjacent to both Shared and Support. Record them as maximal clique 5.

10 more steps lead to the last one:

  1. Done. Every vertex has been a start, and the search found 14 maximal cliques. The biggest has 3 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

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.

All algorithms