♟️ Minimax & Alpha-Beta Pruning
915 words
5 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# ♟️ Minimax & Alpha-Beta Pruning ## 1. 🎯 Learning Objectives - Trace Minimax on a game tree: propagate values, determine best move - Identify α-cutoff and β-cutoff in Alpha-Beta pruning - Explain the difference between Minimax and Alpha-Beta (both find same value) - Compute the number of nodes inspected with and w...

♟️ Minimax & Alpha-Beta Pruning
1. 🎯 Learning Objectives
- Trace Minimax on a game tree: propagate values, determine best move
- Identify α-cutoff and β-cutoff in Alpha-Beta pruning
- Explain the difference between Minimax and Alpha-Beta (both find same value)
- Compute the number of nodes inspected with and without pruning
- Understand evaluation functions for non-terminal states
2. 📖 Core Content
3.1 Intuition: Playing Perfectly
In two-player zero-sum games (chess, checkers, tic-tac-toe), Minimax assumes both players play optimally:
- Max player (us) wants to maximize the outcome
- Min player (opponent) wants to minimize the outcome The algorithm computes the game-theoretic value of each position: the best outcome assuming optimal play from both sides.
3.2 Minimax Algorithm
textMinimax(state, depth, is_maximizing): if goal_test(state) or depth == 0: return evaluate(state) // Terminal or leaf if is_maximizing: best_value = -infinity for each child in move_gen(state): value = Minimax(child, depth-1, False) best_value = max(best_value, value) return best_value else: // Minimizing best_value = +infinity for each child in move_gen(state): value = Minimax(child, depth-1, True) best_value = min(best_value, value) return best_value
3.3 Worked Example: Minimax Trace
(Diagram)
Propagation:
- Max node A has children B (Min) and C (Min)
- B: Min of [3, 5] = 3
- C: Min of [2, 9] = 2
- A: Max of [3, 2] = 3 Best move for Max: go to B (value 3).
3.4 Alpha-Beta Pruning
Alpha-Beta pruning computes the same value as Minimax but prunes branches that cannot affect the outcome.
Key definitions:
- α: Best value Max can achieve (lower bound). Initially −∞.
- β: Best value Min can achieve (upper bound). Initially +∞. Cutoff conditions:
- α-cutoff (at Min node): If α≥β, prune remaining children. Min will not choose a value ≤ current α when Max can guarantee α.
- β-cutoff (at Max node): If β≤α, prune remaining children. Max will not choose a value ≥ current β when Min can guarantee β.
3.5 Alpha-Beta Algorithm
textAlphaBeta(state, depth, alpha, beta, is_maximizing): if goal_test(state) or depth == 0: return evaluate(state) if is_maximizing: for each child in move_gen(state): value = AlphaBeta(child, depth-1, alpha, beta, False) alpha = max(alpha, value) if alpha >= beta: // β-cutoff break return alpha else: for each child in move_gen(state): value = AlphaBeta(child, depth-1, alpha, beta, True) beta = min(beta, value) if beta <= alpha: // α-cutoff break return beta
3.6 Worked Example: Alpha-Beta Trace
Consider this game tree (Max to move, leaf values shown):
(Diagram)
Step 1: Call AlphaBeta(A, α=-∞, β=+∞). A is Max. Step 2: Recurse to B (Min, α=-∞, β=+∞). Step 3: B evaluates children: D=3. β=min(∞,3)=3. α=-∞. Check: β ≤ α? 3 ≤ -∞? No. Step 4: Next child E=5. β=min(3,5)=3. No change. β ≤ α? No. Step 5: B returns β=3 to A. A updates α=max(-∞,3)=3. Step 6: A checks next child C (Min, α=3, β=+∞). Step 7: C evaluates children: F=2. β=min(∞,2)=2. α=3. Check: β ≤ α? 2 ≤ 3? YES! α-cutoff! Step 8: Prune remaining children (G). C returns 2 to A. Step 9: A has α=3 (from B). A returns 3.
Nodes inspected: D, E, F. G was pruned — 4 nodes vs 5 without pruning.
3.7 Alpha-Beta Performance
| Ordering | Nodes Inspected | Improvement |
|---|---|---|
| Worst (no pruning) | O(bd) | None |
| Random ordering | O(b3d/4) | Moderate |
| Perfect ordering (best first) | O(bd/2) | Double effective depth |
Key insight: With perfect move ordering (best moves evaluated first), Alpha-Beta doubles the searchable depth compared to Minimax.
3.8 Minimax vs Alpha-Beta vs SSS*
| Algorithm | Type | Nodes Inspected | Space | Uses Heuristic? |
|---|---|---|---|---|
| Minimax | Depth-first, blind | O(bd) | O(bd) | No (leaf eval) |
| Alpha-Beta | Depth-first with pruning | O(b3d/4) avg | O(bd) | No (leaf eval) |
| SSS* | Best-first | Often fewer than AB | O(bd) | Yes (heuristic direction) |
3.9 Evaluation Functions
Evaluation functions estimate the value of a non-terminal position. Common components:
- Material: Piece values (chess: P=1, N/B=3, R=5, Q=9)
- Positional: Control of center, king safety, pawn structure
- Terminal values: Win = +∞, Loss = -∞, Draw = 0 Range: Usually normalized to [−∞,+∞] or [−1,+1].
4. 📝 Practice Questions
Q1: On a game tree with Max root, Min children at depth 1, leaves [2, 7, 1, 8, 3, 5, 6, 4] in order. Trace Minimax first 4 leaves, then identify which are pruned by Alpha-Beta.Answer: First pass: each pair goes to a Min node. [2,7] → Min=2. [1,8] → Min=1. [3,5] → Min=3. [6,4] → Min=4. Max = max(2,1,3,4) = 4. Alpha-Beta: evaluate 2,7 → β=2 at first Min. α=-∞, no cutoff. β=2 returned to Max, α=2. Next Min: evaluate 1 → β=1. α=2 ≥ β=1 → α-cutoff! Prune 8. α remains 2. Next Min: evaluate 3 → β=3. α=2 < 3, no cutoff. Evaluate 5 → β=min(3,5)=3. Return β=3. α=max(2,3)=3. Next Min: evaluate 6 → β=6. α=3 < 6, no cutoff. Evaluate 4 → β=min(6,4)=4. Return 4. α=max(3,4)=4. Final: 4. Pruned: 8. Q2: What is an α-cutoff and when does it occur?Answer: An α-cutoff occurs at a Min node when α ≥ β. At this point, Max can already guarantee a value ≥ α, and Min will not choose a value ≤ α (which would be worse for Min). The remaining children of the Min node cannot affect the outcome. Q3: With perfect move ordering, how much does Alpha-Beta improve over Minimax?Answer: Alpha-Beta inspects O(b^{d/2}) nodes vs Minimax's O(b^d) — the square root improvement doubles the searchable depth. With random ordering, the improvement is about O(b^{3d/4}). Join Discord PreviousSequence AlignmentNextSSS* Algorithm