Karger's Contraction Algorithm for Minimum Cuts
A cut of a connected undirected graph is a partition of into two nonempty sets and ; its size is the number of edges crossing it. A minimum cut (global min cut) has the fewest crossing edges: it measures how robust a network is against link failures. Deterministically one can compute it with maximum-flow computations (The Max-Flow Min-Cut Theorem) or with the Stoer-Wagner algorithm. David Karger's contraction algorithm (1993) is much simpler: merge the endpoints of random edges until two vertices are left. It is a Monte Carlo algorithm, and its analysis is a gem of probabilistic reasoning.
Contraction
Contracting an edge merges and into one vertex: edges from or to other vertices now leave the merged vertex, edges between and disappear (they would be loops), and parallel edges are kept, so the graph becomes a multigraph. Every cut of the contracted graph is a cut of the original graph (with and on the same side), with the same size.
CONTRACT(G) // G: connected multigraph on n vertices
0 while G has more than 2 vertices
1 choose an edge {u, v} of G uniformly at random
2 contract it (merge u and v, delete the edges between them)
3 return the edges between the two remaining verticesAn efficient way to run it: shuffle the edge list once and process the edges in that order with a union-find structure, joining the endpoints of every edge whose endpoints are still in different sets, until two sets remain. Skipping edges inside a set is exactly "deleting loops", and the first remaining edge in a random order is a uniform random remaining edge, so this is the same random process. It is Kruskal's algorithm with random weights, stopped two steps early.
The success probability
Theorem (Karger, 1993). Let be any minimum cut of , with . CONTRACT outputs exactly with probability at least
Proof. The output is if and only if no edge of is ever contracted. Every vertex has degree at least (its edges form a cut), so a multigraph on vertices whose minimum cut is still has at least edges. Contractions never create smaller cuts, so as long as survives, the current graph on vertices has minimum cut exactly , and a random edge lies in with probability at most . Multiplying the conditional survival probabilities for :
Corollary. A graph has at most minimum cuts. (The events "the output is " are disjoint for distinct minimum cuts, and each has probability at least .) The cycle shows this is tight: any 2 of its edges form a minimum cut.
Repeating
One run succeeds with small probability, so run it times independently and keep the smallest cut found. All runs miss a fixed minimum cut with probability at most
so runs give failure probability at most . With an implementation per run (an adjacency matrix with merged rows), the total is , which is slow; the Karger-Stein algorithm reduces it to .
The program implements CONTRACT with union-find and measures how often it finds a minimum cut on two graphs: two cliques joined by two edges (one minimum cut, of size 2), and a cycle, where every run finds some minimum cut but each particular one with probability exactly .
import random
from math import comb
def find(parent, x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
def contract(n, edges, rng):
order = edges[:]
rng.shuffle(order)
parent = list(range(n))
groups = n
for u, v in order:
if groups == 2:
break
ru, rv = find(parent, u), find(parent, v)
if ru != rv:
parent[ru] = rv
groups -= 1
side = [find(parent, x) == find(parent, 0) for x in range(n)]
cut = [(u, v) for u, v in edges if side[u] != side[v]]
return cut, frozenset(x for x in range(n) if side[x])
def two_cliques(h):
edges = [(u, v) for u in range(h) for v in range(u + 1, h)]
edges += [(u + h, v + h) for u, v in edges]
return 2 * h, edges + [(0, h), (1, h + 1)]
rng = random.Random(40)
n, edges = two_cliques(6)
runs = 2000
sizes = [len(contract(n, edges, rng)[0]) for _ in range(runs)]
print(f"two 6-cliques + 2 bridge edges (n = {n}, m = {len(edges)}): min cut 2")
print(f" success rate {sizes.count(2) / runs:.3f} vs bound 2/(n(n-1)) = {2 / (n * (n - 1)):.4f}")
n = 8
cycle = [(i, (i + 1) % n) for i in range(n)]
runs = 6000
outcomes = [contract(n, cycle, rng) for _ in range(runs)]
target = frozenset(range(1, 4)) # the side {1, 2, 3}: cut edges (0,1) and (3,4)
hits = sum(frozenset(range(n)) - s == target for _, s in outcomes) # s is the side of vertex 0
print(f"cycle C_{n}: all {runs} runs gave a cut of size 2: {all(len(c) == 2 for c, _ in outcomes)}")
print(f" one particular min cut found {hits / runs:.4f} of the time, 1/C(n,2) = {1 / comb(n, 2):.4f}")two 6-cliques + 2 bridge edges (n = 12, m = 32): min cut 2
success rate 0.306 vs bound 2/(n(n-1)) = 0.0152
cycle C_8: all 6000 runs gave a cut of size 2: True
one particular min cut found 0.0357 of the time, 1/C(n,2) = 0.0357
On the two cliques a random edge is far more likely to lie inside a clique than to be one of the two bridges, so the bridges usually survive; the success rate of about 0.3 is twenty times the worst-case bound. (A bridge is most at risk near the end, when few clique edges remain.) The cycle is the worst case, where the bound is exact.
In C++
The same algorithm on the two-clique graph, with a fixed-seed std::mt19937, a Fisher-Yates shuffle written out (so that the result does not depend on the library's std::shuffle), and the repetition count from the analysis. The random stream differs from Python's, so the measured rate differs slightly.
#include <cmath>
#include <cstdio>
#include <numeric>
#include <random>
#include <utility>
#include <vector>
std::mt19937 rng(40);
using Edge = std::pair<int, int>;
int find(std::vector<int>& p, int x) {
while (p[x] != x) { p[x] = p[p[x]]; x = p[x]; }
return x;
}
int contract(int n, std::vector<Edge> e) { // returns the size of the cut found
for (int i = (int)e.size() - 1; i > 0; --i) std::swap(e[i], e[rng() % (i + 1)]);
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 0);
int groups = n;
for (auto [u, v] : e) {
if (groups == 2) break;
int a = find(p, u), b = find(p, v);
if (a != b) { p[a] = b; --groups; }
}
int cut = 0;
for (auto [u, v] : e) cut += find(p, u) != find(p, v);
return cut;
}
int main() {
const int h = 6, n = 2 * h;
std::vector<Edge> e;
for (int u = 0; u < h; ++u)
for (int v = u + 1; v < h; ++v) { e.push_back({u, v}); e.push_back({u + h, v + h}); }
e.push_back({0, h});
e.push_back({1, h + 1});
int runs = 2000, ok = 0;
for (int r = 0; r < runs; ++r) ok += contract(n, e) == 2;
std::printf("n = %d, m = %zu: success rate of one run %.3f, bound %.4f\n", n, e.size(), double(ok) / runs, 2.0 / (n * (n - 1)));
int T = (int)std::ceil(n * (n - 1) / 2.0 * std::log(n)), best = 1 << 30;
for (int r = 0; r < T; ++r) best = std::min(best, contract(n, e));
std::printf("best of T = %d runs: cut of size %d\n", T, best);
}n = 12, m = 32: success rate of one run 0.302, bound 0.0152
best of T = 165 runs: cut of size 2
Finding all minimum cuts
The corollary has an algorithmic side: since each minimum cut is output with probability at least , runs find every minimum cut with high probability. On the cycle there are exactly minimum cuts, each found with probability exactly per run. The program collects the distinct cuts found as the number of runs grows; the coupon-collector bound says about runs are needed on average to see all 15.
import random
def contract_cycle(n, rng):
edges = [(i, (i + 1) % n) for i in range(n)]
rng.shuffle(edges)
parent = list(range(n))
def find(x):
while parent[x] != x:
x = parent[x]
return x
groups = n
for u, v in edges:
if groups == 2:
break
if find(u) != find(v):
parent[find(u)] = find(v)
groups -= 1
return frozenset((u, v) for u, v in edges if find(u) != find(v)) # the two cut edges
rng = random.Random(41)
n, seen = 6, set()
for runs in range(1, 201):
seen.add(contract_cycle(n, rng))
if runs in (5, 10, 20, 50, 100, 200):
print(f"after {runs:3} runs: {len(seen):2} of 15 distinct minimum cuts found")after 5 runs: 5 of 15 distinct minimum cuts found
after 10 runs: 10 of 15 distinct minimum cuts found
after 20 runs: 12 of 15 distinct minimum cuts found
after 50 runs: 15 of 15 distinct minimum cuts found
after 100 runs: 15 of 15 distinct minimum cuts found
after 200 runs: 15 of 15 distinct minimum cuts found
Where it stands
- Monte Carlo, one-sided. The algorithm never reports a cut smaller than the minimum (every output is a real cut), so its only error is reporting a cut that is too large.
- All minimum cuts at once. With runs, every one of the at most minimum cuts is found with high probability, which deterministic flow-based methods do not give for free.
- Faster versions. Karger-Stein (1996) runs in . Karger (2000) gave a near-linear algorithm using tree packings, and recent work has found deterministic near-linear algorithms for simple graphs (Kawarabayashi and Thorup, 2015) and weighted graphs (Henzinger, Li, Rao and Wang, 2024).
Test yourself
Why does a random edge of a multigraph on vertices with minimum cut lie in a fixed minimum cut with probability at most ?
- Because there are cuts
- Every vertex has degree at least , so there are at least edges, of which are in the cut
- Because the graph is a tree
- Because contraction removes half of the edges
What is the lower bound on the probability that one run of the contraction algorithm returns a fixed minimum cut?
What does the analysis imply about the number of minimum cuts in a graph on vertices?
- At most
- At most
- At most , and this is attained
- Exactly one
How many independent runs make the failure probability at most ?