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.
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.
Split the twelve friends into 3 groups and try to cut as few friendships as possible.
On this run, the cut splits the vertices into 3 groups, of 7, 1 and 4 vertices, and cuts 6 edges: Ana and Cara; Ben and Cara; Cara and Dev; Cara and Eli; Hana and Ivo; Hana and Jun.
Step by step
- Randomized k-cut starts with each of the 12 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 = 3 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.
- Contract the edge between Ana and Ben. Ana and Ben form a supernode of 2 vertices. 11 supernodes are left.
- Contract the edge between Ana and Dev. Dev joins the supernode of Ana and Ben, which now has 3 vertices. The edge between Ben and Dev joined the same two supernodes, so it now lies inside the new one and is dropped. 10 supernodes are left.
- Contract the edge between Dev and Eli. Eli joins the supernode of Ana, Ben and Dev, which now has 4 vertices. 9 supernodes are left.
- Contract the edge between Eli and Fay. Fay joins the supernode that holds Eli, which now has 5 vertices. 8 supernodes are left.
- Contract the edge between Eli and Gus. Gus joins the supernode that holds Eli, which now has 6 vertices. The edge between Fay and Gus joined the same two supernodes, so it now lies inside the new one and is dropped. 7 supernodes are left.
4 more steps lead to the last one:
- Done. After 9 contractions, k = 3 supernodes are left, so the contraction stops. The 6 edges between different supernodes form the cut. Every vertex now shows its supernode.
Use it in Go
cut, err := partition.RandomizedKCut(g, 3)
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
- Friend groups, 12 vertices and 21 edges
- Office network, 13 vertices and 16 edges
- Maze, 30 vertices and 31 edges
- Karate club, 34 vertices and 78 edges
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.