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.

Time complexity O(V·E), gograph function partition.RandomizedKCut.

gograph picks the edges to contract at random, so the cut can change each time the server restarts.

How it works

The randomized k-cut starts with each vertex in its own group. It picks an edge at random, merges the groups at its two ends, and drops the edges that now run inside one group. It repeats until k groups are left. The edges still running between groups are the cut.

This is Karger's contraction method for the minimum cut, applied to k groups. A dense cluster has many edges, so a random edge is more likely to fall inside one, and clusters tend to merge before the few edges between them are picked. One run can still miss the smallest cut, so repeat it and keep the cut with the fewest edges.

One run costs O(V·E). k must be at least 2 and no more than the number of vertices. If the graph has more than k connected components, the edges run out first and each component is its own group. Use it to split a network into parts with few links between them, such as friend groups or shards of a service map.

Karate club

The 34 members of a university karate club, numbered 1 to 34 as in the study. Each edge joins two members who also spent time together outside the club's classes and meetings. A dispute between the instructor (1) and an officer (34) split the club in two, and most members sit near the side they joined.

An undirected graph with 34 vertices and 78 edges.

Split the club in two and try to break as few ties between members as possible.

On this run, the cut splits the vertices into 2 groups, of 33 and 1 vertices, and cuts 1 edge: 1 and 12.

Step by step

  1. Randomized k-cut starts with each of the 34 vertices as a supernode of its own. gograph shuffles the edges and takes them in that order. An edge between two supernodes is contracted: the two merge into one, and the edges between them now lie inside it and are dropped. It stops when k = 2 supernodes are left, and the edges between them form the cut. These steps contract only edges inside the supernodes gograph ended with, in the graph's edge order, so they end in the same cut.
  2. Contract the edge between 1 and 2. 1 and 2 form a supernode of 2 vertices. 33 supernodes are left.
  3. Contract the edge between 1 and 3. 3 joins the supernode of 1 and 2, which now has 3 vertices. The edge between 2 and 3 joined the same two supernodes, so it now lies inside the new one and is dropped. 32 supernodes are left.
  4. Contract the edge between 1 and 4. 4 joins the supernode of 1, 2 and 3, which now has 4 vertices. The 2 other edges between the two supernodes now lie inside the new one and are dropped. 31 supernodes are left.
  5. Contract the edge between 1 and 5. 5 joins the supernode that holds 1, which now has 5 vertices. 30 supernodes are left.
  6. Contract the edge between 1 and 6. 6 joins the supernode that holds 1, which now has 6 vertices. 29 supernodes are left.

27 more steps lead to the last one:

  1. Done. After 32 contractions, k = 2 supernodes are left, so the contraction stops. The edge between 1 and 12 is the only one between two supernodes, so it is the whole cut. Every vertex now shows its supernode.

Use it in Go

cut, err := partition.RandomizedKCut(g, 2)
if err != nil {
	return err
}
for _, group := range cut.Supernodes {
	var members []string
	for _, v := range group {
		members = append(members, v.Label())
	}
	fmt.Println(members)
}
for _, e := range cut.CutEdges {
	fmt.Println(e.Source().Label(), e.Destination().Label())
}

Example graphs

More in Partitioning

  • Maximal cliques: Finds every group of vertices that are all adjacent to each other and can't take one more vertex.
  • Girvan-Newman: Splits a graph into k communities by removing, one at a time, the edge that the most shortest paths cross.

All algorithms