Minimax Algorithm
Prerequisites
- Game Theory Basics — Game trees, MAX/MIN players, utility
- Depth-First Search — DFS traversal of trees
🎯 Learning Objectives
- Understand the minimax decision rule for two-player zero-sum games
- Trace the backup of values from terminal nodes to the root
- Write and explain the minimax pseudocode
- Analyse time and space complexity of the minimax algorithm
- Apply minimax to game tree problems (GATE style)
1. The Minimax Idea
In a two-player zero-sum game, MAX wants to maximize the utility and MIN wants to minimize it. The minimax algorithm computes the optimal move for MAX by assuming both players play optimally.
The minimax value of a node n is defined recursively:
- MINIMAX(n) = UTILITY(n) if n is a terminal node
- MINIMAX(n) = maxc ∈ children(n) MINIMAX(c) if n is a MAX node
- MINIMAX(n) = minc ∈ children(n) MINIMAX(c) if n is a MIN node
The algorithm performs a complete depth-first exploration of the game tree. At terminal (leaf) nodes, the utility value is known. These values are then backed up through the tree: MAX nodes take the maximum of their children; MIN nodes take the minimum.
2. Full Game Tree Example
Consider this depth-3 game tree with branching factor 2–3. Terminal utilities are shown at the leaves.
A (MAX)
/ \
B (MIN) C (MIN)
/ | \ / \
D(MAX) E(MAX) F(MAX) G(MAX) H(MAX)
/ \ / \ | / \ |
3 5 6 9 1 2 7 4
Step-by-step backup:
D = max(3, 5) = 5 G = max(2, 7) = 7
E = max(6, 9) = 9 H = max(4) = 4
F = max(1) = 1
B = min(5, 9, 1) = 1 C = min(7, 4) = 4
A = max(1, 4) = 4
Optimal play: MAX chooses C at root (value 4).
MIN at C chooses H (value 4). MAX at H gets 4.
Always start from the leaves (bottom) and work upward. At each internal node, apply max or min depending on whose turn it is. The value at the root tells MAX the best guaranteed outcome under optimal play.
3. Minimax Pseudocode
function MINIMAX-DECISION(state):
return argmaxa ∈ ACTIONS(state) MIN-VALUE(RESULT(state, a))
function MAX-VALUE(state):
if IS-TERMINAL(state):
return UTILITY(state)
v ← −∞
for each a in ACTIONS(state):
v ← max(v, MIN-VALUE(RESULT(state, a)))
return v
function MIN-VALUE(state):
if IS-TERMINAL(state):
return UTILITY(state)
v ← +∞
for each a in ACTIONS(state):
v ← min(v, MAX-VALUE(RESULT(state, a)))
return v
The algorithm uses two mutually recursive functions. MINIMAX-DECISION returns the action (not just the value) that leads to the state with the highest minimax value.
4. Complexity Analysis
For a game tree with branching factor b and maximum depth m:
- Time Complexity: O(bm) — must explore every leaf node
- Space Complexity: O(b · m) — DFS keeps one path in memory plus siblings at each level
| Game | b (avg) | m (avg) | bm |
|---|---|---|---|
| Tic-Tac-Toe | ~4 | 9 | ~2.6 × 105 |
| Chess | ~35 | ~80 | ~10123 |
| Go (19×19) | ~250 | ~150 | ~10360 |
Minimax is feasible for Tic-Tac-Toe but completely impractical for Chess or Go without enhancements like alpha-beta pruning and depth cutoffs.
5. Worked Examples
MAX (root)
/ | \
MIN MIN MIN
/ \ / \ / \
3 12 8 2 4 6
Step 1 (MIN level):
Left MIN = min(3, 12) = 3
Mid MIN = min(8, 2) = 2
Right MIN = min(4, 6) = 4
Step 2 (MAX level):
Root MAX = max(3, 2, 4) = 4
MAX chooses the right branch (value 4).
MAX (A)
/ \
MIN (B) MIN (C)
/ \ |
MAX(D) MAX(E) MAX(F)
/ \ | / | \
7 2 5 1 8 3
Backup from leaves:
D = max(7, 2) = 7
E = max(5) = 5
F = max(1, 8, 3) = 8
B = min(7, 5) = 5
C = min(8) = 8
A = max(5, 8) = 8
MAX at root chooses C (value 8).
MIN at C is forced to pick F (only child).
MAX at F picks action leading to utility 8.
MAX (root)
/ \
MIN (L) MIN (R)
/ \ / \
MAX(LL) MAX(LR) MAX(RL) MAX(RR)
/ \ / \ / \ / \
5 3 9 1 2 6 4 7
Level 3 (MAX): LL=max(5,3)=5 LR=max(9,1)=9 RL=max(2,6)=6 RR=max(4,7)=7
Level 2 (MIN): L=min(5,9)=5 R=min(6,7)=6
Level 1 (MAX): root=max(5,6)=6
MAX chooses R at root (value 6).
6. Properties of Minimax
In a finite two-player zero-sum game with perfect information, the minimax algorithm computes the optimal strategy for MAX. The minimax value at the root equals the game’s value (the outcome under optimal play by both sides).
- Complete: Yes, for finite game trees
- Optimal: Yes, against an optimal opponent
- Against suboptimal opponents: Minimax may not exploit mistakes (it assumes worst-case). The result can only be as good or better than the minimax value.
Students sometimes back up values in the wrong direction — always remember: start from the leaves toward the root. Also ensure you apply max at MAX nodes and min at MIN nodes (not the reverse!).
7. GATE-Style Problems
Q: Consider the following game tree where MAX moves first. What is the minimax value at the root?
MAX
/ \
MIN MIN
/ \ / \
5 3 8 2
Solution: Left MIN = min(5,3) = 3. Right MIN = min(8,2) = 2. Root MAX = max(3,2) = 3.
Q: In a minimax game tree of depth d and uniform branching factor b, how many leaf nodes are evaluated?
Solution: All leaf nodes must be evaluated. Number of leaves = bd. So bd leaf evaluations are needed. Total nodes visited = (bd+1 − 1)/(b − 1) for the full tree.
Q: A minimax tree has branching factor 3 and depth 4. What are the time and space complexities?
Solution: Time = O(bm) = O(34) = O(81). Space = O(b × m) = O(3 × 4) = O(12). In general terms: Time = O(bm), Space = O(bm).
Summary
- Minimax computes optimal play by backing up values from leaves to the root.
- MAX nodes take the maximum; MIN nodes take the minimum of their children.
- Time complexity is O(bm); space complexity is O(bm) using DFS.
- Minimax is optimal against a rational opponent but explores the entire tree.
- Alpha-beta pruning (next topic) dramatically reduces the number of nodes explored.
References
- Russell, S. & Norvig, P. Artificial Intelligence: A Modern Approach, 4th Ed. — Chapter 5.2
- Knuth, D. & Moore, R. “An Analysis of Alpha-Beta Pruning”, Artificial Intelligence, 1975