Minimax and Alpha-Beta
In a two-player turn-based game (tic-tac-toe, chess, connect four), minimax picks the best move assuming the opponent also plays perfectly. Alpha-beta gets exactly the same result while exploring far fewer positions.
The game tree
Each node is a position, each edge a move. Levels alternate between the two players:
- ▲ MAX (us) looks for the largest value;
- ▼ MIN (the opponent) looks for the smallest value.
Leaves get their value from an evaluation function: +1 / 0 / −1 for a finished game, or an estimate (material advantage, for instance) when the search stops at a fixed depth.
Minimax
Values are computed bottom-up: a MAX node takes the maximum of its children, a MIN node the minimum. The tree is explored depth-first, left to right.
minimax(node):
if node is a leaf: return evaluate(node)
if node is MAX: return max(minimax(c) for each child c)
else: return min(minimax(c) for each child c)
Example: the root A (MAX) has three MIN children.
A (MAX)
/ | \
B (MIN) C (MIN) D (MIN)
/ | \ / | \ / | \
4 9 6 3 7 5 8 2 10
- B = min(4, 9, 6) = 4
- C = min(3, 7, 5) = 3
- D = min(8, 2, 10) = 2
- A = max(4, 3, 2) = 4: the best move leads to B.
Minimax visits every leaf: O(bd) for a branching factor b and a depth d.
Alpha-beta pruning
Two bounds are passed down the tree:
- α: the best value MAX is already guaranteed on the path (starts at −∞);
- β: the best value MIN is already guaranteed on the path (starts at +∞).
A MAX node updates α, a MIN node updates β. As soon as α ≥ β, the remaining children can no longer change the decision: they are pruned.
The same example with alpha-beta
- B (α = −∞, β = +∞): we see 4, 9, 6 → B = 4. Back at A, α = 4.
- C (α = 4, β = +∞): first leaf 3, so β = 3. Since α = 4 ≥ β = 3, we cut: C will be worth at most 3, and A already has an option worth 4. Leaves 7 and 5 are pruned.
- D (α = 4, β = +∞): leaf 8 → β = 8, no cut. Leaf 2 → β = 2 ≤ α, cut: leaf 10 is pruned.
- A = 4, as with minimax, but with 3 fewer leaves to evaluate.
Two kinds of cutoff: at a MIN node, when its value drops to α or below (α cutoff); at a MAX node, when its value rises to β or above (β cutoff).
Move ordering matters
Alpha-beta prunes more when the best moves are explored first. With perfect ordering, the complexity drops to about O(bd/2), which allows searching twice as deep in the same time. With bad ordering, nothing is pruned and we are back to O(bd).
Common pitfalls
Alpha-beta does not change the root value. It gives exactly the same result as minimax; it only avoids useless work.
α and β go down, values go up. A node receives α and β from its parent, then returns its value. Do not mix the two.
Prune the remaining children, not the node itself. The node where the cutoff happens keeps the value already found, which is enough to prove its parent will not choose it.