Algorithms · Visualized

Game: Alpha–Beta Pruning

Game Theory & Minimax

Two-player games — who wins with perfect play, and by how much.

Alpha–beta pruning is minimax with bookkeeping: alpha is the best score the maximising player can already guarantee, beta the best the minimising player can guarantee. Time: O(b^(3d/4)) average, O(b^d) — nothing pruned worst; space: O(b·d) for the recursion stack; the tree itself is O(b^d).

Big-O: Winning positions, DP over states, and minimax evaluation.

Game: Alpha–Beta Pruning

Game Theory / Minimax

Alpha–beta pruning is minimax with bookkeeping: alpha is the best score the maximising player can already guarantee, beta the best the minimising player can guarantee. Once beta falls to or below alpha the current branch cannot change the value of any ancestor, so its remaining children are skipped entirely. The visualizer builds a three-level complete binary tree over the leaves [3, 12, 8, 2, 4, 6, 14, 5]: the root is MAX, its two children are MIN, their four children are MAX, and the eight leaves are the terminal scores. Pruning never changes the root value — it only removes work that provably cannot matter.

How Alpha–Beta Pruning Works

  1. Start at the root with alpha = −∞ and beta = +∞.
  2. At a MAX node take the largest child value and raise alpha; at a MIN node take the smallest and lower beta.
  3. After each child, test beta ≤ alpha — if true, stop exploring this node’s remaining children.
  4. Propagate the surviving value to the parent and continue until the root is evaluated.
  5. Return the root value and the best child: the move to play.

Alpha–Beta Pruning Worked Example

Input: {values: [3, 12, 8, 2, 4, 6, 14, 5]}

With leaves 3 12 8 2 4 6 14 5 the four MAX nodes evaluate to 12, 8, 6 and 14. The first MIN node therefore takes min(12, 8) = 8 and the second takes min(6, 14) = 6, so the root MAX returns max(8, 6) = 8 and the left branch is the better move. The pruning happens at the last MAX node: it sees 14, raising alpha to 14, while its parent MIN already holds beta = 6 from the sibling MAX node. Since beta ≤ alpha, the leaf 5 is never examined — the animation marks it pruned — and the root value is unchanged at 8.

Game: Alpha–Beta PruningO(b^(d/2)) best
Minimax that skips branches provably irrelevant (β ≤ α).
3
12
8
2
4
6
14
5
alpha-∞
beta+∞
Step 1 / 14
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Alpha–Beta: like minimax, but stop exploring a node once it provably cannot change the answer (α ≥ β).
Python · Game Theory Algorithms in Code
def alphabeta(node, alpha, beta, is_max):
    if leaf(node): return value(node)
    if is_max:
        best = float('-inf')
        for c in children(node):
            best = max(best, alphabeta(c, alpha, beta, False))
            alpha = max(alpha, best)
            if beta <= alpha: break   # prune siblings
        return best
    best = float('inf')
    for c in children(node):
        best = min(best, alphabeta(c, alpha, beta, True))
        beta = min(beta, best)
        if beta <= alpha: break       # prune
    return best  # O(b^(d/2)) best case

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.

Alpha–Beta Pruning Time & Space Complexity

BestO(b^(d/2)) with perfect move ordering
AverageO(b^(3d/4))
WorstO(b^d) — nothing pruned
SpaceO(b·d) for the recursion stack; the tree itself is O(b^d)

When to Use Alpha–Beta Pruning

  • Two-player board games where full minimax is too slow but the game tree is finite.
  • Chess, checkers and Othello engines, which pair it with move ordering and evaluation functions.
  • Teaching why search order matters: the same tree with the same leaves can need half the nodes.

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.