skip to content

How does alpha-beta pruning skip branches without changing the minimax result, and why does move ordering decide how much it saves?

level: middleimportance: must knowfreq 52%

answer

  1. two bounds carried down the search
  2. a better option already exists elsewhere
  3. cutoff when alpha reaches beta
  4. best move first, square-root branching

basics

~20 s

Alpha-beta carries alpha, the score the maximiser is already guaranteed, and beta, the score the minimiser is already guaranteed. Once a node's value must fall outside that window, its remaining moves are skipped. Trying the best move first maximises cutoffs.

solid answer

~50 s

Alpha-beta passes two bounds down the search: `alpha`, the best score the maximising side can already force elsewhere, and `beta`, the best (lowest) score the minimising side can already force. A maximising node raises `alpha` as it finds good moves; when `alpha >= beta`, the opponent would never let play reach this node, so its remaining moves cannot change the root and are skipped. The root is searched with an unbounded window, so it returns exactly the minimax value. How much is saved depends on order: with the best move searched first at every node, Knuth and Moore showed the leaf count falls to `b^ceil(d/2) + b^floor(d/2) - 1`, about `b^(d/2)`, so you search roughly twice as deep for the same work; with the worst order it saves nothing. Hence the move-ordering heuristics: the stored best move, winning captures, killer moves and the history heuristic.

code

pseudocode · 18 lines
pseudocode
function negamax(position, depth, alpha, beta):
    if depth == 0 or position.isGameOver():
        return evaluate(position)          // score for the side to move
    best = -INFINITY
    for move in orderedMoves(position):    // stored move, captures, killers, history
        position.make(move)
        score = -negamax(position, depth - 1, -beta, -alpha)
        position.unmake(move)
        if score > best:
            best = score
        if best > alpha:
            alpha = best
        if alpha >= beta:
            recordKiller(move, depth)       // a quiet move that refuted this line
            break                           // cutoff: the opponent avoids this node
    return best                             // fail-soft: may lie outside the window

// root call: negamax(root, maxDepth, -INFINITY, +INFINITY)

go deeper

for a junior

Recall what alpha and beta stand for and the cutoff condition: once alpha reaches beta, the rest of the node's moves are skipped.

for a middle

Walk a small tree by hand, show where cutoffs fire and why the root value is unchanged, and state the best-case leaf count with its perfect-ordering assumption.

for a senior

Explain which ordering heuristics buy the most cutoffs in practice and how you would measure them, for example the share of cutoffs on the first move tried.

for a principal

Weigh ordering and pruning work against evaluation cost under a fixed time per move, deciding where effort per node buys more depth than it costs.

## The idea: bounds that make branches irrelevant **Alpha-beta pruning** is minimax that skips work it can prove does not matter. It carries two numbers down the tree: - **alpha**: the best score the maximising player (MAX) is already guaranteed by some move searched earlier on the current path; - **beta**: the best score the minimising player (MIN) is already guaranteed, that is, the lowest value MIN can already force. The search starts at the root with `alpha = -infinity` and `beta = +infinity`. A MAX node raises `alpha` whenever a move beats it; a MIN node lowers `beta`. As soon as `alpha >= beta` at a node, that node is refuted: the player choosing at its parent already has an alternative at least as good, so play will never reach it. Its remaining moves are skipped. This is a **cutoff**. ## A worked example MAX has moves X, Y and Z, each answered by three MIN replies: | MAX move | Leaves in search order | What happens | |---|---|---| | X | 4, 5, 6 | all three searched, value 4, so `alpha = 4` | | Y | 1, (15), (14) | first leaf 1: MIN can hold Y to at most 1, below 4, cutoff | | Z | 3, (9), (12) | first leaf 3: at most 3, below 4, cutoff | Five of nine leaves are evaluated and the root value is 4, the same as plain minimax. Searched in the order Z, X, Y instead, Z sets `alpha = 3`, X needs all three leaves (none is at or below 3) and raises `alpha` to 4, and only Y is cut: seven leaves. The **order** decided the saving. ## Why the result is unchanged A node's exact value only matters to its parent if it falls strictly inside the window `(alpha, beta)`. Once a node is known to be at least `beta` (or at most `alpha`), the parent will choose something else whatever the exact number is. Every skipped subtree is one whose value could only push its node further outside the window. The root starts with an infinite window, so the value it returns is exactly the minimax value; the move chosen can differ only among moves of equal value. ## How much ordering saves For a uniform tree with branching factor `b` and depth `d`: | Move order at every node | Leaves examined | `b = 30`, `d = 4` | |---|---|---| | Worst (best move last) | `b^d`, no saving | 810,000 | | Random | roughly `b^(3d/4)` for moderate `b` | about 27,000 | | Perfect (best move first) | `b^ceil(d/2) + b^floor(d/2) - 1` | 1,799 | The perfect-ordering figure is Knuth and Moore's result: the **effective branching factor** drops to about `sqrt(b)`, so the same budget reaches roughly twice the depth. Real engines cannot order perfectly, because the best move is what they are searching for, so they guess. ## Move-ordering heuristics Common sources of a good guess, roughly in the order they are tried: 1. The **stored best move** for this position from a transposition table or from the previous, shallower iteration of the search. 2. **Winning captures**, ordered most valuable victim first and least valuable attacker first (often called MVV-LVA). 3. **Killer moves**: one or two quiet moves per ply that caused a cutoff in a sibling position at the same ply. A refutation often works again in a nearly identical position. 4. The **history heuristic**: a table indexed by move (for example origin and destination square) that is credited, often by depth squared, whenever that move causes a cutoff anywhere; remaining quiet moves are sorted by it. 5. Everything else. Ordering costs time too. Sorting a full move list at every node is wasteful when the first move usually cuts, so many engines generate and pick moves in stages, trying the stored move before generating anything else. ## Variants worth naming - **Fail-hard** searches clamp returned scores into `[alpha, beta]`; **fail-soft** searches return the true bound found, which gives tighter information to store and reuse. - **Principal variation search** searches the first move with the full window and the rest with a zero-width window, re-searching only if one surprises it. It relies on good ordering to pay off.

  • Why does alpha-beta reach about twice the depth of minimax only in the best case?
    The `b^(d/2)` bound assumes the best move is searched first at every node, so each refutation is found immediately and each cut node examines one child. With poor ordering the refutation arrives late or never, and the count drifts towards `b^d`. Real searches land in between, which is why measured effective branching factors depend so heavily on the ordering heuristics.
  • What is a killer move, and why is it kept per ply rather than globally?
    A killer is a quiet move that caused a cutoff at a given ply. Positions at the same ply in sibling subtrees often differ by one earlier move, so the same refutation tends to work again. Keeping killers per ply limits them to that similar context; credit for moves that cut anywhere in the tree goes into the history table instead.

  • Alpha-beta trades some accuracy for speed, like a beam search.
  • Alpha-beta always examines about b^(d/2) leaves, whatever the move order.
  • Pruned branches are losing moves that are discarded permanently.
  • Alpha is the opponent's best score and beta is our own.
  • Killer moves are the captures of the most valuable pieces.