Karger-Stein: Recursive Contraction
Karger's contraction algorithm needs about independent runs to find a minimum cut with high probability, time in total. Look at where a run fails: the probability of contracting a minimum-cut edge at the step with vertices left is at most , tiny at the start and large at the end. The early contractions are nearly safe, so repeating them in every run is wasteful. Karger and Stein (1996) share the safe early work between runs: contract to about vertices, then branch into two independent recursive calls. The result runs in time per trial, succeeds with probability , and finds a minimum cut with high probability in time.
Surviving down to vertices
Running the contraction only until vertices remain keeps a fixed minimum cut with probability
For we get and , so and the survival probability is at least . Contracting a third of the vertices away is a coin flip at worst; the whole contraction down to 2 vertices is a lottery.
The algorithm
KARGER-STEIN(G) // G: multigraph on n vertices
0 if n <= 6
1 return a minimum cut of G by brute force
2 t = ceil(1 + n / sqrt(2))
3 G1 = CONTRACT-TO(G, t) // independent random contractions
4 G2 = CONTRACT-TO(G, t)
5 return the smaller of KARGER-STEIN(G1) and KARGER-STEIN(G2)CONTRACT-TO(G, t) performs random contractions until vertices remain. With an adjacency-matrix representation it costs .
Running time. . Here subproblems shrink by , and matches the work per level: case 2 of the master theorem gives . Each of the levels of the recursion tree does work in total, because the number of subproblems doubles while halves.
Success probability. A call succeeds if, in at least one branch, survives the contraction and the recursive call succeeds. Let be a lower bound on the success probability of a call at height above the leaves (leaves are exact, ). The two branches are independent, so
Claim: . By induction: the map is increasing on , so , where the last step is equivalent to . The height is about , so
Repetition. independent trials (say of them) push the failure probability below (up to constants in the exponent), for a total of time.
Implementation and measurement
The program stores a multigraph as an edge list on vertices . contract_to shuffles the edges and merges with union-find until groups remain, then relabels the groups and keeps only edges between different groups. The test graphs are doubled cycles: a cycle on vertices in which every edge is doubled except two opposite ones. The two single edges form the unique minimum cut (size 2), and there are many cuts of size 3 and 4 to fall into. Work is counted as edges processed, the dominant cost.
import random
from itertools import product
def find(p, x):
while p[x] != x:
p[x] = p[p[x]]
x = p[x]
return x
def contract_to(n, edges, t, rng, work):
order = edges[:]
rng.shuffle(order)
p = list(range(n))
groups = n
for u, v in order:
work[0] += 1
if groups == t:
break
a, b = find(p, u), find(p, v)
if a != b:
p[a] = b
groups -= 1
label = {}
for x in range(n):
label.setdefault(find(p, x), len(label))
new = [(label[find(p, u)], label[find(p, v)]) for u, v in edges]
work[0] += len(edges)
return t, [(a, b) for a, b in new if a != b]
def brute_force(n, edges, work):
best = None
for bits in product([0, 1], repeat=n - 1): # vertex n-1 is fixed on side 0
side = bits + (0,)
if any(side):
c = sum(side[u] != side[v] for u, v in edges)
work[0] += len(edges)
best = c if best is None else min(best, c)
return best
def karger_stein(n, edges, rng, work):
if n <= 6:
return brute_force(n, edges, work)
t = -(-n * 1000 // 1414) + 1 # ceil(1 + n / sqrt 2)
return min(karger_stein(*contract_to(n, edges, t, rng, work), rng, work) for _ in range(2))
def karger(n, edges, rng, work):
return len(contract_to(n, edges, 2, rng, work)[1])
def doubled_cycle(n):
e = []
for i in range(n):
e.append((i, (i + 1) % n))
if i not in (0, n // 2):
e.append((i, (i + 1) % n)) # a parallel copy: every edge doubled but two
return n, e
rng = random.Random(50)
print(" n Karger: success steps/run Karger-Stein: success steps/trial")
for size in [12, 18, 24]:
n, edges = doubled_cycle(size)
w1, w2 = [0], [0]
k_ok = sum(karger(n, edges, rng, w1) == 2 for _ in range(600))
ks_ok = sum(karger_stein(n, edges, rng, w2) == 2 for _ in range(6))
print(f"{n:2} {k_ok / 600:15.3f} {w1[0] / 600:9.0f} {ks_ok / 6:21.2f} {w2[0] / 6:11.0f}") n Karger: success steps/run Karger-Stein: success steps/trial
12 0.098 36 1.00 11424
18 0.040 58 0.83 23973
24 0.038 80 1.00 48308
One contraction succeeds less and less often as grows (on these graphs roughly like ; the bound is for the worst graphs), while a Karger-Stein trial succeeds most of the time. Per unit of work, though, plain repetition is still competitive at these small sizes: here a Karger-Stein trial costs thousands of edge steps, mostly in the many small subproblems near the leaves. The asymptotic advantage, against in the adjacency-matrix model, needs larger graphs to show; the mini project measures both on graphs of growing size.
The recursion tree itself shows where the work goes. The program counts the subproblems and their total size per level for .
from math import ceil, sqrt
n = 200
level, sizes = 0, [n]
print("level subproblems size each sum of size^2")
while sizes[0] > 6:
print(f"{level:5} {len(sizes):11} {sizes[0]:9} {sum(s * s for s in sizes):13}")
t = ceil(1 + sizes[0] / sqrt(2))
sizes = [t] * (2 * len(sizes))
level += 1
print(f"{level:5} {len(sizes):11} {sizes[0]:9} (brute-force leaves)")level subproblems size each sum of size^2
0 1 200 40000
1 2 143 40898
2 4 103 42436
3 8 74 43808
4 16 54 46656
5 32 40 51200
6 64 30 57600
7 128 23 67712
8 256 18 82944
9 512 14 100352
10 1024 11 123904
11 2048 9 165888
12 4096 8 262144
13 8192 7 401408
14 16384 6 (brute-force leaves)
On the upper levels the total size squared stays near : the number of subproblems doubles while halves, which is the work per level behind the bound. Near the leaves the "" and the ceiling in take over (11 vertices shrink only to 9, then 8, 7 and 6), so the last levels cost several times more. The analysis absorbs this into the constant, since sizes below a constant cost each and there are leaves; implementations often stop the recursion at a larger size and finish with plain repeated contraction.
In C++
Karger-Stein in C++ with a fixed-seed std::mt19937 on the same doubled cycles, including a larger one. The random stream differs from Python's, so the success rates differ a little.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <numeric>
#include <random>
#include <utility>
#include <vector>
using Edge = std::pair<int, int>;
std::mt19937 rng(50);
int find(std::vector<int>& p, int x) {
while (p[x] != x) { p[x] = p[p[x]]; x = p[x]; }
return x;
}
std::vector<Edge> contract_to(int n, std::vector<Edge> e, int t) {
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 == t) break;
int a = find(p, u), b = find(p, v);
if (a != b) { p[a] = b; --groups; }
}
std::map<int, int> label;
for (int x = 0; x < n; ++x) label.emplace(find(p, x), (int)label.size());
std::vector<Edge> out;
for (auto [u, v] : e) {
int a = label[find(p, u)], b = label[find(p, v)];
if (a != b) out.push_back({a, b});
}
return out;
}
int brute_force(int n, const std::vector<Edge>& e) {
int best = 1 << 30;
for (int mask = 1; mask < (1 << (n - 1)); ++mask) { // vertex n-1 stays on side 0
int c = 0;
for (auto [u, v] : e) c += ((mask >> u) & 1) != ((mask >> v) & 1);
best = std::min(best, c);
}
return best;
}
int karger_stein(int n, const std::vector<Edge>& e) {
if (n <= 6) return brute_force(n, e);
int t = (int)std::ceil(1 + n / std::sqrt(2.0));
int a = karger_stein(t, contract_to(n, e, t));
int b = karger_stein(t, contract_to(n, e, t));
return std::min(a, b);
}
int main() {
for (int n : {24, 48, 72}) {
std::vector<Edge> e;
for (int i = 0; i < n; ++i) {
e.push_back({i, (i + 1) % n});
if (i != 0 && i != n / 2) e.push_back({i, (i + 1) % n});
}
int trials = 20, ok = 0, one_run_ok = 0;
for (int t = 0; t < trials; ++t) ok += karger_stein(n, e) == 2;
for (int t = 0; t < 2000; ++t) one_run_ok += contract_to(n, e, 2).size() == 2;
std::printf("n = %2d: Karger one run %.4f, Karger-Stein %2d/%d trials found the min cut 2\n",
n, one_run_ok / 2000.0, ok, trials);
}
}n = 24: Karger one run 0.0425, Karger-Stein 17/20 trials found the min cut 2
n = 48: Karger one run 0.0185, Karger-Stein 17/20 trials found the min cut 2
n = 72: Karger one run 0.0215, Karger-Stein 17/20 trials found the min cut 2
Test yourself
Why does Karger-Stein contract only down to about vertices before branching?
- Because is irrational
- Because a fixed minimum cut survives contraction to vertices with probability at least
- To make the recursion tree binary
- Because smaller graphs have no minimum cuts
What is the running time of one Karger-Stein trial?
Which recurrence bounds the success probability, level by level from the leaves?
What is the success probability of one Karger-Stein trial, asymptotically?