learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Algorithm Selection

Select an algorithm and generate steps to begin visualization

Karger's Algorithm Visualizer

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

Time: O(n⁴ log n) as shown, O(n² log³ n) Karger–Stein
Space: O(n + m)
Use Case: Network reliability, clustering and image segmentation, the classic first example of a randomized algorithm
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Karger's Minimum Cut (Random Contraction)

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.

How it works

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.

Applications

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.

Pseudocode

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 best

A 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.

Worked example, step by step

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.

  1. Trial 1, inside the squares. A–B is picked with chance 1 in 14, then F–H, E–G and F–G merge the right square into one vertex, and B–C joins C to {A, B}. Loops are dropped, so the edge count falls faster than the vertex count.
  2. The unlucky pick. D–F is contracted with chance 20%. It is one of the two edges of the minimum cut, so this trial can no longer find it. Two vertices are left, {A, B, C} and {D, E, F, G, H}, with 4 edges between them.
  3. All trials. T = ceil(28 · ln 8) = 59 trials. The cut of 2 comes up 12 times, 20.3%, far above the guaranteed 3.6% per trial.
  4. Exact check. For a graph this small the visualizer tries all 2⁷ − 1 = 127 splits into two non-empty groups. None has fewer than 2 crossing edges.

Minimum cut 2, the edges C–E and D–F, between {A, B, C, D} and {E, F, G, H}.

Complexity, and where it comes from

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.

When to use Karger's Minimum Cut (Random Contraction), and when not to

Other ways to find a minimum cut on the site.

AlternativePrefer it whenCost
Stoer–WagnerA certain answer is needed, deterministically, also with weights. It runs n − 1 maximum adjacency phases.O(nm + n² log n)
Max flow and min cutThe 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–SteinThe graph is large and dense and a randomized answer is fine. Recursive contraction reuses the early, safe merges.O(n² log³ n)

Common pitfalls

  • Running a single trial. One trial finds the minimum cut with probability as low as 2 / (n(n − 1)), 3.6% on 8 vertices, and the first trial here returns 4. Repeat about C(n, 2) ln n times, or use Karger–Stein.
  • Dropping parallel edges. After a merge, two edges from the merged vertex to the same neighbour must both stay. Removing duplicates changes the chances and the answer, because the cut is the number of edges left between the last two vertices.
  • Picking a random vertex pair. The bound needs a uniformly random edge, which favours heavily linked pairs. Picking a random vertex and then a random neighbour is not the same. On a weighted graph, pick each edge with probability proportional to its weight.

Frequently asked questions

Why is a random edge unlikely to cross the minimum cut?
If the minimum cut has k edges, every vertex has degree at least k, otherwise its own edges would form a smaller cut. So the graph has at least nk / 2 edges, and a random edge crosses the cut with probability at most k / (nk / 2) = 2 / n.
How many minimum cuts can a graph have?
At most C(n, 2), and a cycle of n vertices has exactly that many. The bound follows from the analysis: each minimum cut survives a trial with probability at least 1 / C(n, 2), and two different cuts cannot both be the result of the same trial.
Does it work on weighted graphs?
Yes. Pick each edge with probability proportional to its weight and the same analysis holds. The answer is then the total weight of the edges left between the two groups.

Related algorithms: Stoer-Wagner Min Cut, Minimum Cut, Maximum Flow, Kruskal's MST Algorithm, Union-Find (Disjoint Set Union)

Built by Mohammed Islam HadjoudjWork with me

An example graph: you can change it, or start blank.

Zoom Controls

30%
Nodes: 8
Edges: 14