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.

Campus network

A hundred switches, servers and computers in ten buildings of a university campus, A to J. Each id is a building letter and a number, so D7 is number 7 in building D. A is the data center, where the core switches A1 and A2 link to the main switch of every other building, always number 1. The larger buildings have more switches between the main switch and the computers, and two pairs of neighboring buildings also have a direct link.

An undirected graph with 100 vertices and 114 edges.

Which groups of devices are all linked to each other, where no other device is linked to all of them?

The graph has 94 maximal cliques. The 2 largest have 4 vertices each: A1, A2, C1 and D1; A1, A2, H1 and I1.

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 A5, the first vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add A3 to R. P and X are both empty, so no other vertex is adjacent to both A5 and A3. Record them as maximal clique 1.
  3. Start from J6, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add J1 to R. P and X are both empty, so no other vertex is adjacent to both J6 and J1. Record them as maximal clique 2.
  4. Start from A8, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add A3 to R. P and X are both empty, so no other vertex is adjacent to both A8 and A3. Record them as maximal clique 3.
  5. Start from A9, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add A3 to R. P and X are both empty, so no other vertex is adjacent to both A9 and A3. Record them as maximal clique 4.
  6. Start from A16, the next vertex in degeneracy order, with its later neighbors in P and its earlier ones in X. Add A4 to R. P and X are both empty, so no other vertex is adjacent to both A16 and A4. Record them as maximal clique 5.

95 more steps lead to the last one:

  1. Done. Every vertex has been a start, and the search found 94 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

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