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 / MinimaxAlpha–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
- Start at the root with alpha = −∞ and beta = +∞.
- At a MAX node take the largest child value and raise alpha; at a MIN node take the smallest and lower beta.
- After each child, test beta ≤ alpha — if true, stop exploring this node’s remaining children.
- Propagate the surviving value to the parent and continue until the root is evaluated.
- 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.
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 caseBehind 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
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
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.