Chapter 2: Minimax and Alpha-Beta Pruning
Multiple Choice · Branching factor
Question 1
In a game there are 10 legal moves in every position. If the search depth of minimax is increased from 4 plies to 6 plies, by roughly what factor does the number of leaves read grow?
The correct answer is C. In a tree where every internal node has b children, depth d has bd leaves: 104 = 10,000 leaves become 106 = 1,000,000, that is, 100 times. Each ply multiplies the work by b = 10; two plies give 10 · 10 = 100. B adds the factors instead of multiplying them. A and D assume that growth is linear in the depth, but it is exponential.
Multiple Choice · The minimax guarantee
Question 2
The minimax value of a position is +2. MAX plays the minimax move at every turn, while MIN is an opponent who makes mistakes from time to time. Which statement about MAX's final utility is definitely true?
The correct answer is A. The minimax value is the value MAX guarantees under the assumption that MIN answers every move optimally. If MIN departs from the best reply, MAX gets at least this value and never less: the minimax value is a safe lower bound. The result being exactly 2 (B) is certain only if both sides play flawlessly. Minimax does not look for the move that maximizes the gain against a weak opponent, but its guarantee does not drop either (C and D are wrong).
Classic Question · Minimax by hand
Question 3: Minimax in a Three-Level Tree
Root A (MAX), second level MIN, third level MAX. The leaves from left to right are E = [2, 8], F = [5, 3], G = [9, 1], H = [4, 6], I = [7, 2], J = [3, 10].
a) What is the root's minimax value, and which move does MAX choose?
b) If both sides play their best, at which leaf does the game end?
c) Why is the largest leaf (10) an unreachable outcome?
Show answer
a) Third level (MAX): E = 8, F = 5, G = 9, H = 6, I = 7, J = 10.
Second level (MIN): B = min(8, 5) = 5, C = min(9, 6) = 6, D = min(7, 10) = 7.
Root: A = max(5, 6, 7) = 7; MAX picks D.
b) The path A → D → I → 7: at D, MIN prefers I (7) to J (10); at I, MAX picks 7.
c) 10 lies below J. Whether J is reached is decided by MIN at D, who picks I, which gives 7: for MAX to reach 10, MIN would have to make a mistake. The 9 at G is unreachable for the same reason.
Multiple Choice · P- and N-positions
Question 4
In Nim, where whoever takes the last token wins, which is a P-position (the player to move loses if the opponent plays flawlessly)?
The correct answer is D. By Bouton's rule a position is a P-position ⇔ the nim-sum (XOR) of the heaps is 0. 1 ⊕ 4 ⊕ 5 = 001 ⊕ 100 ⊕ 101 = 000 = 0: (1, 4, 5) is a P-position. In the others the sum is nonzero: 2 ⊕ 4 ⊕ 5 = 3, 1 ⊕ 3 ⊕ 5 = 7, 2 ⊕ 3 ⊕ 4 = 5; these are N-positions. On the full tree, negamax gives the same result: the value of (1, 4, 5) is −1 for the player to move, and +1 for the others.
Classic Question · Negamax and the XOR rule
Question 5: Negamax in Nim
The Nim position is (2, 4); whoever takes the last token wins. Values are written from the mover's view: value(s) = maxa −value(result(s, a)), and a position with no moves has value −1.
a) What is the value of positions of the form (k, k) (two equal heaps)? Why?
b) What are the values of the six children of (2, 4) and the value of (2, 4)? Which move wins?
c) Does the XOR rule give the same move?
d) How many nodes does the full tree rooted at (2, 4) have?
Show answer
a) (0, 0) has no moves: −1. Every move from (k, k) leaves the heaps unequal; the opponent takes the same number of tokens from the other heap and makes (j, j) again. By this mirror strategy (k, k) is −1, that is, a P-position, for every k.
b) The children and their values for the mover: (1, 4): +1, (0, 4): +1, (2, 3): +1, (2, 2): −1, (2, 1): +1, (2, 0): +1.
Since (2, 2) = −1, value(2, 4) = −(−1) = +1 (N). The only winning move is to take 2 tokens from the second heap, leaving (2, 2); the other five children are +1, that is, they win for the opponent.
c) 2 ⊕ 4 = 010 ⊕ 100 = 110 = 6. 4 ⊕ 6 = 2 < 4: the second heap drops to 2. 2 ⊕ 6 = 4 > 2: there is no such move from the first heap. The two methods find the same move.
d) 216 nodes including the root; 33 of them are the (2, 2) subtree alone. The XOR rule finishes in a few operations.
Multiple Choice · Depth penalty
Question 6
In tic-tac-toe a depth penalty of ±(10 − d) is used; values are written from X's view and it is O's turn (MIN). O's four moves lead to these outcomes: (1) O wins 1 ply after the root, (2) O wins after 3 plies, (3) a draw, (4) X wins after 2 plies. What are the values and O's choice?
The correct answer is B. If O wins, the value is −(10 − d): −9 for d = 1, −7 for d = 3. If X wins, it is +(10 − d): +8 for d = 2; a draw is 0. Since O is MIN, it picks the smallest value, −9, that is, the move that wins at once. A mixes up the viewpoint: the tree has a single set of numbers, always from X's view. "Delaying the loss" is for the side that is losing; here the loser is X (C). D is the ±1 scale; it cannot tell a fast win from a slow one.
Classic Question · Tic-tac-toe and the depth penalty
Question 7: Evaluating a Tic-Tac-Toe Position
X is at top left and top middle, O at top right and middle left; it is X's turn (MAX). Values are from X's view with a depth penalty: +(10 − d) if X wins, −(10 − d) if O wins, 0 for a draw; d is the number of moves from the root.
a) What is the minimax value of each of the five empty squares?
b) Which square does X choose? What is the reason?
c) Why do the squares in the bottom row lose?
Show answer
a) Center +7, middle right 0, bottom left, bottom middle, and bottom right −6.
b) The center: it makes a fork. X gets two threats: the middle column (bottom middle is empty) and the diagonal (bottom right is empty). O blocks one, and X completes the other on its third move: 10 − 3 = +7.
c) If X plays in the bottom row, O makes a fork: if X plays bottom left, O plays middle right (threats: center and bottom right); if X plays bottom middle or bottom right, O plays the center (threats: middle right and bottom left). X blocks one, and O wins four moves from the root: −(10 − 4) = −6. Middle right prevents these forks but gives X no threat: 0.
Multiple Choice · α and β
Question 8
A MAX node receives the window [α, β] = [3, 9]. Its first child is a MIN node called with the same window; in the child β drops to 5 and the child returns 5. What is the window at the MAX node now?
The correct answer is A. The window passes from parent to child unchanged; only a value comes back to the parent. The MAX node sets best = 5 with the returned 5 and α = max(3, 5) = 5; β = 9 does not change: [5, 9]. β = 5 in the child is the child's local variable and is not carried to the parent (B). Only MIN nodes lower β (C). D forgets that the returned value raises α at the parent.
Step-by-Step Trace · Alpha-beta pruning
Question 9: Tracing the Alpha-Beta Code
Root A (MAX), B, C, D (MIN). What does the program print? Which leaves are never read, and at which nodes does pruning happen?
from math import inf T = {"A": ["B", "C", "D"], "B": [5, 8], "C": [6, 2, 9], "D": [4, 8]} def alphabeta(n, a, b, maxi): if isinstance(n, int): # leaf return n best = -inf if maxi else inf for c in T[n]: v = alphabeta(c, a, b, not maxi) if maxi: best = max(best, v); a = max(a, best) else: best = min(best, v); b = min(b, best) if a >= b: break # prune return best print(alphabeta("A", -inf, inf, True))
The tree built by the code: root MAX, second level MIN. No leaf has been read yet.
First call: root A is a MAX node; best = -inf, a = -inf, b = inf.
alphabeta("B") is called: MIN, best = inf; a and b come from the caller: [-inf, inf].
alphabeta(5) is a leaf: returns 5. With min, best = 5, b = 5; a < b, continue.
alphabeta(8) is a leaf: returns 8. With min, best = 5, b = 5; a < b, continue; B is done and returns 5.
The call on B returned 5. With max, best = 5, a = 5; a < b, continue.
alphabeta("C") is called: MIN, best = inf; a and b come from the caller: [5, inf].
alphabeta(6) is a leaf: returns 6. With min, best = 6, b = 6; a < b, continue.
alphabeta(2) is a leaf: returns 2. With min, best = 2, b = 2; a ≥ b.
a = 5 ≥ b = 2: break. C gives at most 2 and A has already secured 5. Leaf 9 is never read; C returns 2.
The call on C returned 2. With max, best = 5, a = 5; a < b, continue.
alphabeta("D") is called: MIN, best = inf; a and b come from the caller: [5, inf].
alphabeta(4) is a leaf: returns 4. With min, best = 4, b = 4; a ≥ b.
a = 5 ≥ b = 4: break. D gives at most 4 and A has already secured 5. Leaf 8 is never read; D returns 4.
The call on D returned 4. With max, best = 5, a = 5; a < b, continue; A is done and returns 5.
The program prints 5: A picks the move to B. 5 of the 7 leaves were read; C's 9 and D's 8 were pruned. Minimax also finds 5 (C = 2, D = 4).
Step-by-Step Trace · Pruning at a MAX node
Question 10: When the Root Is MIN
The alpha-beta code is called with a MIN root (False in the last line); B, C, D are MAX nodes. What does the program print? At which node does pruning happen?
from math import inf T = {"A": ["B", "C", "D"], "B": [3, 6], "C": [4, 7, 2], "D": [5, 1]} def alphabeta(n, a, b, maxi): if isinstance(n, int): # leaf return n best = -inf if maxi else inf for c in T[n]: v = alphabeta(c, a, b, not maxi) if maxi: best = max(best, v); a = max(a, best) else: best = min(best, v); b = min(b, best) if a >= b: break # prune return best print(alphabeta("A", -inf, inf, False))
The tree built by the code: root MIN, second level MAX. No leaf has been read yet.
First call: root A is a MIN node; best = inf, a = -inf, b = inf.
alphabeta("B") is called: MAX, best = -inf; a and b come from the caller: [-inf, inf].
alphabeta(3) is a leaf: returns 3. With max, best = 3, a = 3; a < b, continue.
alphabeta(6) is a leaf: returns 6. With max, best = 6, a = 6; a < b, continue; B is done and returns 6.
The call on B returned 6. With min, best = 6, b = 6; a < b, continue.
alphabeta("C") is called: MAX, best = -inf; a and b come from the caller: [-inf, 6].
alphabeta(4) is a leaf: returns 4. With max, best = 4, a = 4; a < b, continue.
alphabeta(7) is a leaf: returns 7. With max, best = 7, a = 7; a ≥ b.
a = 7 ≥ b = 6: break. C gives at least 7 and A has already secured 6. Leaf 2 is never read; C returns 7.
The call on C returned 7. With min, best = 6, b = 6; a < b, continue.
alphabeta("D") is called: MAX, best = -inf; a and b come from the caller: [-inf, 6].
alphabeta(5) is a leaf: returns 5. With max, best = 5, a = 5; a < b, continue.
alphabeta(1) is a leaf: returns 1. With max, best = 5, a = 5; a < b, continue; D is done and returns 5.
The call on D returned 5. With min, best = 5, b = 5; a < b, continue; A is done and returns 5.
The program prints 5: the MIN root picks the move to D. Pruning happened only at C (a MAX node): C's 2 was not read. There is no pruning at D: D's value 5 lies inside the incoming window [−∞, 6].
Classic Question · Alpha-beta by hand
Question 11: Alpha-Beta by Hand
Root A (MAX), B, C, D (MIN), E–J (MAX); children are tried from left to right.
a) What are the root's value and MAX's move by minimax?
b) What [α, β] window does each internal node receive in alpha-beta?
c) Which leaves are pruned, and how many leaves are read?
Show answer
a) E = 7, F = 9, B = 7; G = 5, H = 6, C = 5; I = 8, J = 9, D = 8. Root = max(7, 5, 8) = 8, and MAX picks D.
| Node | Incoming [α, β] | Returns |
|---|---|---|
| B | [−∞, +∞] | 7 |
| E | [−∞, +∞] | 7 |
| F | [−∞, 7] | 9 |
| C | [7, +∞] | 5 |
| G | [7, +∞] | 5 |
| D | [7, +∞] | 8 |
| I | [7, +∞] | 8 |
| J | [7, 8] | 9 |
- F: 9 ≥ β = 7 → the 3 is pruned.
- C: with G = 5, β = 5 ≤ α = 7 → H (1, 6) is pruned.
- J: 9 ≥ β = 8 → the 2 is pruned.
c) Of the twelve leaves, 8 are read. The 9 returned by F and J is not an exact value but a lower bound. The root rises from 7 to 8 at D: D's value lies inside the incoming window.
Multiple Choice · Reading code
Question 12
The line if a >= b: in the alpha-beta code is changed to if a > b:. What happens?
The correct answer is D. When α = β, the node's value cannot change the root: a value that could affect the result must lie between α and β, and there is no such value. a > b does not prune in this case; the remaining branches are read for nothing, but the root value does not change. Example: root MAX, B = [5, 7], C = [5, 9]. With B = 5, α = 5; with the first leaf of C being 5, β = 5. a >= b prunes the 9 (3 leaves read), a > b reads it (4 leaves); both codes find 5. When α > β, pruning still happens (B is wrong); the condition is the same line for both node types (C).
Multiple Choice · The Knuth–Moore formula
Question 13
In a uniform tree of depth 5 in which every internal node has 5 children, the children are tried in the best order. How many leaves does alpha-beta read?
The correct answer is C. With the best ordering, the number of leaves read is b⌈d/2⌉ + b⌊d/2⌋ − 1: 53 + 52 − 1 = 125 + 25 − 1 = 149. 3,125 = 55 is the count of the search without pruning, or of the worst ordering. 125 is only the first term, and 250 is 2 · 53. When d is odd, the two exponents differ (3 and 2). Against minimax's 3,125 leaves, that is about 21 times fewer reads.
Step-by-Step Trace · Move ordering
Question 14: Ordering and Leaves Read
The children are given in the best order. What does the program print? How many leaves would be read in the worst order?
from math import inf T = {"A": ["B", "C", "D"], "B": [7, 9, 8], "C": [3, 8, 10], "D": [5, 12, 6]} leaves_read = [] def alphabeta(n, a, b, maxi): if isinstance(n, int): # leaf leaves_read.append(n) return n best = -inf if maxi else inf for c in T[n]: v = alphabeta(c, a, b, not maxi) if maxi: best = max(best, v); a = max(a, best) else: best = min(best, v); b = min(b, best) if a >= b: break # prune return best print(alphabeta("A", -inf, inf, True), leaves_read)
The tree built by the code: root MAX, second level MIN. No leaf has been read yet.
First call: root A is a MAX node; best = -inf, a = -inf, b = inf.
alphabeta("B") is called: MIN, best = inf; a and b come from the caller: [-inf, inf].
alphabeta(7) is a leaf: returns 7. With min, best = 7, b = 7; a < b, continue.
alphabeta(9) is a leaf: returns 9. With min, best = 7, b = 7; a < b, continue.
alphabeta(8) is a leaf: returns 8. With min, best = 7, b = 7; a < b, continue; B is done and returns 7.
The call on B returned 7. With max, best = 7, a = 7; a < b, continue.
alphabeta("C") is called: MIN, best = inf; a and b come from the caller: [7, inf].
alphabeta(3) is a leaf: returns 3. With min, best = 3, b = 3; a ≥ b.
a = 7 ≥ b = 3: break. C gives at most 3 and A has already secured 7. Leaves 8 and 10 are never read; C returns 3.
The call on C returned 3. With max, best = 7, a = 7; a < b, continue.
alphabeta("D") is called: MIN, best = inf; a and b come from the caller: [7, inf].
alphabeta(5) is a leaf: returns 5. With min, best = 5, b = 5; a ≥ b.
a = 7 ≥ b = 5: break. D gives at most 5 and A has already secured 7. Leaves 12 and 6 are never read; D returns 5.
The call on D returned 5. With max, best = 7, a = 7; a < b, continue; A is done and returns 7.
Output: 7 [7, 9, 8, 3, 5]. 5 of the 9 leaves are read: 3 + 3 − 1 = 5. In the worst order (C, D, B at the root; leaves from largest to smallest) there is no pruning and all 9 leaves are read; the root is still 7.
Classic Question · Evaluation function
Question 15: Connect Four: Computing E(s)
E(s) = Σ p(w) + 3 · (Ccenter − Bcenter); window scores: 3 Red + 1 empty +5, 2 Red + 2 empty +2, and −5 and −2 for Blue, other windows 0. Red = MAX.
a) Which windows score in this position, and what is E(s)?
b) It is Red's turn; if Red plays column 5, the piece lands in row 2. What is E(s) in the new position?
Show answer
Squares are (column, row), with rows counted from the bottom. a) All horizontal and vertical windows contain both colors, one piece, or none: 0. The two diagonals that score:
- (5, 1), (4, 2), (3, 3), (2, 4): two Red + two empty → +2
- (6, 1), (5, 2), (4, 3), (3, 4): two Blue + two empty → −2
Center column: 3 · (2 − 1) = +3. E(s) = 2 − 2 + 3 = +3.
b) The new piece at (5, 2) makes the Blue diagonal mixed: the −2 disappears. Six windows score +2: the horizontals (2–5), (3–6), (4–7) in row 2; the vertical (5, 1)–(5, 4) in column 5; the diagonals (4, 1)–(7, 4) and (5, 1)–(2, 4). E(s) = 6 · 2 + 3 = +15.
Multiple Choice · The horizon effect
Question 16
A depth-limited chess program sees that it cannot save a piece, so it gives pointless checks to push the loss of the piece beyond the search limit. What is this phenomenon called, and which method reduces it most directly?
The correct answer is B. The search cannot see past its limit; moves that push an unavoidable loss beyond the horizon do not remove the loss, they only hide it: the horizon effect. If a volatile situation such as a capture or a check is going on at the limit, the search continues with only those moves and the position is evaluated when it becomes quiet: quiescence search. A transposition table (C) avoids searching the same position again; it does not extend the horizon. Move ordering (A) and the pruning condition (D) change only the cost, not the root value.
Classic Question · The horizon effect
Question 17: Depth 2 or Depth 3?
Red (MAX) is at the root. D, E, F, G are the positions at depth 2; the number beside each is its E(s). The leaves are the E(s) values of the positions at depth 3. The dashed line is the depth-2 limit.
a) Which move does the depth-2 search choose, and what is the root value?
b) What does the depth-3 search choose?
c) Why does the difference arise? Is the depth-3 value certain?
Show answer
a) Depth 2: B = min(+5, +7) = +5, C = min(+2, +4) = +2. Root +5, choice B.
b) Depth 3: D = max(−6, −8) = −6, E = +8, B = min(−6, +8) = −6; F = +3, G = +5, C = +3. Root +3, choice C.
c) D looks good at depth 2 with +5; but when it is Red's turn, every move leads to a bad position (−6, −8). The depth-2 search cannot see this because it lies beyond the limit: the horizon effect. The depth-3 value is also an estimate; the horizon has moved only one ply further.
Coding Question · Negamax
Question 18: Nim with Negamax
A Nim position is a tuple of heap sizes, for example (1, 2, 3). Write two functions in the same style as the recursive minimax code:
value(h): +1 if the player to move wins with best play, −1 if they lose (negamax).winning_moves(h): the list of child positions whose value is −1.
What do these three lines print?
print(value((1, 2)), value((2, 2))) print(value((1, 2, 3)), value((2, 5))) print(winning_moves((2, 5)))
Show answer
def children(h): # results of all moves return [h[:i] + (h[i] - k,) + h[i+1:] for i in range(len(h)) for k in range(1, h[i] + 1)] def value(h): # h: heap sizes if sum(h) == 0: # no move: P return -1 return max(-value(z) for z in children(h)) def winning_moves(h): return [z for z in children(h) if value(z) == -1] print(value((1, 2)), value((2, 2))) print(value((1, 2, 3)), value((2, 5))) print(winning_moves((2, 5)))
Output:
1 -1 -1 1 [(2, 2)]
If one child is −1, then max(−value) = +1 (N); if all children are +1, the result is −1 (P). In (2, 5) the only winning move is to reduce the second heap to 2: 2 ⊕ 5 = 7, 5 ⊕ 7 = 2. The same positions are searched again and again; functools.lru_cache makes each position be computed once (a transposition table).
Multiple Choice · Iterative deepening
Question 19
In a game with 4 moves in every position, iterative deepening runs searches of depth 1, 2, 3, 4, and 5 in turn (no pruning). Roughly how many times the cost of a single direct depth-5 search is the total number of leaves read?
The correct answer is B. A search of depth d reads bd leaves: 4, 16, 64, 256, 1,024. The total is 1,364; the last layer (1,024) is larger than all the earlier ones together (340). The ratio is about b/(b − 1) = 4/3 ≈ 1.33 (7/6 for b = 7 in the lecture). A assumes every search has the same length, but shallow searches are very short. C and D also exaggerate the repeated work: all the earlier searches together are only about 1/(b − 1) of the last one.
Multiple Choice · Transposition table
Question 20
An entry in the transposition table of a Connect Four search: value +12, searched depth 2, type exact, best move column 3. The search now reaches the same position with a remaining depth of 5. How should the entry be used?
The correct answer is D. The +12 in the entry rests on a search of only 2 plies; now a depth of 5 is requested, and a shallow value cannot replace a deep search (A). This is why the searched depth is stored in the entry: the value is used only if this depth is at least as large as the one requested. The best-move information, however, is still valuable: column 3 is tried first, move ordering improves, and alpha-beta prunes more. B throws this information away. C is wrong: the value of a shallow search is neither a lower nor an upper bound on the deep value.
Multiple Choice · Maximin and minimax
Question 21
In a zero-sum game the rows are MAX's pure strategies, the columns are MIN's, and the cells are MAX's payoff. Row 1: (3, −1, 2), row 2: (1, 0, 4), row 3: (−2, 5, 1). Which statement is correct?
The correct answer is A. The row minima are −1, 0, −2 ⇒ maximin = 0 (row 2). The column maxima are 3, 5, 4 ⇒ minimax = 3 (column 1). In every game maximin ≤ minimax: MAX's guarantee cannot exceed the value MIN can hold it to. Since 0 < 3, there is no saddle point. With mixed strategies the gap closes (the minimax theorem) and the common value lies in between: v = 13/11 ≈ 1.18 (MAX plays row 1 with probability 7/11 and row 3 with 4/11). B reverses the definitions; C takes the value to be the average of the two ends; D forgets the minimax theorem.