Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
Merge random edges, keep the smallest cut
Contracts random edges until two vertices are left: the edges between them are a cut. Repeated often enough, it finds a minimum cut with high probability
Select an algorithm and generate steps to begin visualization
Karger's algorithm finds a global minimum cut of an undirected graph: the fewest edges whose removal splits the vertices into two non-empty groups. David Karger published it in 1993 as a strikingly simple randomized method. Pick an edge at random and contract it, merging its two ends into one vertex, until only two vertices are left. The edges between those two are a cut. A minimum cut has few edges, so a random edge rarely crosses it, and one trial keeps it with probability at least 2 / (n(n − 1)). That sounds small, but repeating the trial about n² ln n / 2 times makes a miss unlikely. It is the textbook example of a Monte Carlo algorithm: fast, simple, and right with high probability rather than for sure.
Each contraction picks one of the remaining edges at random, merges its ends and drops the edges that became loops. Parallel edges stay, so a pair of vertices joined by many edges is more likely to merge next, which is why tightly linked groups melt together first. The visualizer colours each merged group, names the contracted edge and its chance, and when two vertices are left draws the edges between them as the cut. Then it runs all the trials at once and reports how often the smallest cut appeared. In the example, two squares of four vertices joined by two edges, the first trial ends with a cut of 4 because the edge D–F was contracted early. Over 59 trials the minimum cut of 2, between {A, B, C, D} and {E, F, G, H}, comes up 12 times, and trying all 127 splits confirms that nothing smaller exists.
Minimum cuts measure how robust a network is: the fewest links whose failure disconnects it. Karger's contraction is used in network reliability estimates, in clustering and image segmentation, where a small cut separates two dense regions, and as a building block of faster randomized cut algorithms. The Karger–Stein refinement contracts down to about n / √2 vertices, then recurses twice, and reaches O(n² log³ n). Beyond its uses, the algorithm is the classic first example in courses on randomized algorithms, because its analysis fits in a few lines and shows how repeating a cheap random trial turns a small success chance into near certainty.
One trial contracts random edges until two vertices are left. The trial is repeated and the smallest cut kept.
Karger(G):
best = infinity
repeat T = ceil(C(n, 2) * ln n) times:
H = a copy of G
while H has more than 2 vertices:
pick an edge (u, v) of H uniformly at random
merge v into u and drop the loops // parallel edges stay
best = min(best, number of edges left in H)
return bestA trial keeps a fixed minimum cut of k edges when none of them is contracted. With i vertices left, every vertex still has degree at least k, so there are at least ik / 2 edges and the next pick hits the cut with probability at most 2 / i. Multiplying (1 − 2 / i) from i = n down to 3 leaves at least 2 / (n(n − 1)). T trials all miss with probability at most e^(−2T / (n(n − 1))), which is 1 / n for the T above.
The eight-vertex example loaded on this page.
Example graph: Two squares with both diagonals, A, B, C, D and E, F, G, H, each a complete graph of 6 edges, joined by C–E and D–F. 8 vertices, 14 edges, unweighted.
Minimum cut 2, the edges C–E and D–F, between {A, B, C, D} and {E, F, G, H}.
Time: O(n²) per trial, O(n⁴ log n) for all trials · Space: O(n + m)
A trial performs n − 2 contractions. With a matrix of edge multiplicities, drawing a random edge and merging two rows costs O(n), so a trial costs O(n²), and the C(n, 2) ln n trials give O(n⁴ log n). A trial can also run as Kruskal's algorithm on a random edge order with union-find, in O(m α(n)). Karger–Stein shares work between trials: it contracts down to about n / √2 vertices, where the minimum cut survives with probability about 1 / 2, then recurses twice, for O(n² log³ n) in total with high probability.
Other ways to find a minimum cut on the site.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Stoer–Wagner | A certain answer is needed, deterministically, also with weights. It runs n − 1 maximum adjacency phases. | O(nm + n² log n) |
| Max flow and min cut | The cut must separate two given vertices s and t. Fixing s and trying every t also gives the global minimum. | one max flow per pair |
| Karger–Stein | The graph is large and dense and a randomized answer is fine. Recursive contraction reuses the early, safe merges. | O(n² log³ n) |
Related algorithms: Stoer-Wagner Min Cut, Minimum Cut, Maximum Flow, Kruskal's MST Algorithm, Union-Find (Disjoint Set Union)
Built by Mohammed Islam Hadjoudj · Work with me