Algorithms · Visualized
Random: Karger's Min Cut
Randomized Algorithms
Randomness as a tool — shuffles, sampling, and probabilistic selection.
Estimates a graph’s minimum cut by repeatedly contracting random edges: pick an edge, merge its two endpoints into one super-node, and delete self-loops. Time: O(V² log V) runs × O(E) average, O(V²·E log V) worst; space: O(V + E) per run.
Big-O: Expected-case guarantees with simple implementations.
Karger’s Minimum Cut
Randomized AlgorithmsEstimates a graph’s minimum cut by repeatedly contracting random edges: pick an edge, merge its two endpoints into one super-node, and delete self-loops. After V−1 contractions two super-nodes remain, and the edges between them form a cut. A single run succeeds with probability at least 2/V², so running the contraction many times and keeping the smallest cut found gives a high-probability answer.
How Karger's Min Cut Works
- Copy the graph so the original stays intact.
- While more than two super-nodes remain, choose a remaining edge uniformly at random.
- Contract that edge: merge the endpoints and remove self-loops.
- The surviving edges between the last two super-nodes are the cut.
- Repeat O(V² log V) times and keep the smallest cut seen.
Karger's Min Cut Worked Example
Input:
A square with a diagonal: contracting three random edges usually leaves the two-edge side cut, but a bad sequence can merge across it — which is why the run is repeated and the minimum kept.
import random
def karger(edges, n):
parent = list(range(n))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
verts = n
while verts > 2: # contract random edge
u, v = random.choice(edges)
ru, rv = find(u), find(v)
if ru == rv: continue # self-loop: skip
parent[rv] = ru
verts -= 1
return sum(1 for u, v in edges if find(u) != find(v))Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Karger's Min Cut Time & Space Complexity
When to Use Karger's Min Cut
- Approximate minimum cuts in large graphs where exact algorithms are too slow.
- A clean demonstration of randomized algorithms and probability amplification by repetition.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.