Adversarial Search — Minimax, Alpha–Beta Pruning and Expectimax
Games as search problems (deterministic, zero-sum, perfect information), game trees and the minimax algorithm, alpha–beta pruning traced step by step with the effect of move ordering, depth-limited search with evaluation functions, expectimax for games of chance, and a glimpse of Monte Carlo tree search.
Games as search
Classical game playing considers two-player, turn-taking, deterministic, perfect-information, zero-sum games (chess, checkers, Go, tic-tac-toe). A game is a search problem with:
- initial state, (whose move), , ;
- a terminal test and a utility for terminal states (e.g. win, draw, loss for MAX).
MAX tries to maximise the utility; MIN tries to minimise it. Unlike ordinary search, the agent needs a strategy — a move for every possible reply.
Minimax
def minimax(state, maximizing):
if is_terminal(state):
return utility(state)
values = [minimax(child, not maximizing) for child in successors(state)]
return max(values) if maximizing else min(values)
A complete depth-first exploration: time , space . Optimal against an optimal opponent (and never worse against a weaker one, in terms of the guaranteed value).
Alpha–beta pruning
Keep two bounds along the current path:
- — the best (highest) value MAX can already guarantee;
- — the best (lowest) value MIN can already guarantee.
Prune a node's remaining children as soon as : at a MIN node when its value drops to ; at a MAX node when its value rises to .
import math
def alphabeta(state, maximizing, alpha=-math.inf, beta=math.inf):
if is_terminal(state):
return utility(state)
if maximizing:
v = -math.inf
for child in successors(state):
v = max(v, alphabeta(child, False, alpha, beta))
alpha = max(alpha, v)
if alpha >= beta:
break # beta cut-off
return v
v = math.inf
for child in successors(state):
v = min(v, alphabeta(child, True, alpha, beta))
beta = min(beta, v)
if alpha >= beta:
break # alpha cut-off
return v
| Move ordering | Leaves examined |
|---|---|
| worst (weakest moves first) | — no savings |
| random | about |
| perfect (best moves first) | — doubles the reachable depth |
Good ordering heuristics: try captures and threats first, killer moves, iterative deepening (use the previous iteration's best move first), and transposition tables for repeated positions.
Imperfect real-time decisions
Real games are too deep to search to the end. Depth-limited search replaces the terminal test with a cut-off test and the utility with a heuristic evaluation function , e.g. a weighted sum of features (material balance, mobility, king safety). Problems: the horizon effect (pushing an unavoidable loss beyond the depth limit) — mitigated by quiescence search (extend search in volatile positions).
Games of chance: expectimax
Add chance nodes whose value is the expected value of their children:
Because values are averaged, the evaluation function's actual magnitudes matter (only its order matters in minimax). Pruning is harder, since bounds on averages need bounds on the utilities.
Beyond minimax
Monte Carlo tree search (used by AlphaGo) estimates move values by random playouts, balancing exploration and exploitation with the UCB1 rule; it works without a hand-made evaluation function and suits games with huge branching factors such as Go.
Quick revision
Practice questions
GATE-style questions: 3 one-mark and 6 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.
A two-ply game tree: the root is a MAX node with three MIN children B, C, D (evaluated left to right). The leaves (utilities for MAX) are B: 3, 12, 8 · C: 2, 4, 6 · D: 14, 5, 2.
The minimax value of the root is ______.
A two-ply game tree: the root is a MAX node with three MIN children B, C, D (evaluated left to right). The leaves (utilities for MAX) are B: 3, 12, 8 · C: 2, 4, 6 · D: 14, 5, 2.
Alpha–beta pruning is applied with children examined left to right. The number of leaves that are not evaluated (pruned) is ______.
Compared with plain minimax, alpha–beta pruning
A uniform game tree has branching factor and depth . With perfect move ordering, alpha–beta examines leaves. That number is ______.
Which statements about game-tree search are correct?
One or more options are correct.
MAX chooses between two moves leading to chance nodes. Move 1: outcome 2 with probability 0.5 and 8 with probability 0.5. Move 2: outcome 4 with probability 0.9 and 20 with probability 0.1. The expectimax value of MAX's position is ______.
During alpha–beta search, the remaining children of a MIN node can be pruned as soon as the MIN node's current value satisfies
A complete game tree with branching factor 3 and depth 4 has how many leaves? ______
Chess is best described as a game that is