Memra

Alpha-beta pruning

◈ 3 cards

Track alpha (MAX best-so-far) and beta (MIN best-so-far) to skip subtrees that cannot change the minimax value — same answer, far fewer nodes.

Same answer, less work

Alpha-beta pruning computes the exact same backed-up value as full minimax but skips subtrees that cannot possibly affect the result. It carries two bounds down the tree:

  • — the best value MAX is already assured of somewhere above. It can only ever increase.
  • — the best value MIN is already assured of somewhere above. It can only ever decrease.

### The two cutoff rules

  1. -cutoff — stop exploring below a MIN node once its value of some MAX ancestor. MAX already has a line at least this good, so it will never enter this MIN node.
  2. -cutoff — stop exploring below a MAX node once its value of some MIN ancestor. MIN already has a line at least this good, so it will never allow this MAX node.

In code: at a MAX node update and break when ; at a MIN node update and break when .

Why it matters

With good move ordering (try the strongest moves first), alpha-beta prunes whole subtrees and can effectively double the search depth reachable in a fixed time budget compared with plain minimax. With the worst ordering it prunes nothing and degrades to minimax. This is why chess engines invest in move ordering (e.g. iterative deepening to order the next level).

Worked example: alpha-beta vs minimax

Run both on the same tic-tac-toe position from the previous lesson, counting the nodes each visits. Both return move (2,2), value +1 — identical, as guaranteed. But plain minimax visits 258 nodes while alpha-beta visits 176: it pruned roughly a third of the tree without changing the answer. That is the entire point — alpha-beta is a speedup, never a change of result.

O→(1,0)O→(1,1)O→(1,2)O→(2,2)MIN (after X→(2,1))enter α=0 β=2MAX+1, β←1MAX0, β←0 = αprunednot visitedprunednot visited
The identical sub-tree from the previous lesson, with bounds carried in. MAX has already secured 0 from earlier root children, so this node is entered with α = 0, β = 2. The first reply backs up +1 and lowers β to 1; the second backs up 0 and lowers β to 0, which meets α — a <strong>β-cutoff</strong>. MIN can no longer produce anything MAX would prefer to the 0 it already holds, so the last two replies are never generated. The node still returns 0, exactly as unpruned minimax did.
rulefires at abound updatedcut whenβ-cutoffMIN nodeβ = min(β, best)value ≤ α aboveα-cutoffMAX nodeα = max(α, best)value ≥ β aboveBoth reduce to α ≥ β: α only rises, β only falls.
The two rules are mirror images, and both reduce to the same test — α ≥ β. α is a floor that only ever rises and β a ceiling that only ever falls; once they cross, whatever is left below the node is provably unreachable in optimal play, so it is skipped. The value returned is unchanged; only the node count moves.
NORMAL ~/memra/learn/comp-456/alpha-beta-pruning utf-8 LF