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 Algorithms

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

  1. Copy the graph so the original stays intact.
  2. While more than two super-nodes remain, choose a remaining edge uniformly at random.
  3. Contract that edge: merge the endpoints and remove self-loops.
  4. The surviving edges between the last two super-nodes are the cut.
  5. 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.

Random: Karger's Min CutO(n²·m) per trial
Contract random edges until 2 supernodes remain — survivors define a cut.
ABCDEF
supernodes6
Step 1 / 6
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Karger's Min Cut: repeatedly contract a RANDOM edge until 2 supernodes remain — survivors define a cut.
Python · Randomized Algorithms in Code
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

BestO(E)
AverageO(V² log V) runs × O(E)
WorstO(V²·E log V)
SpaceO(V + E) per run

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.