Minimax and alpha-beta
Adversarial search plans when another decision-maker can choose an opposing action. In a finite, deterministic, alternating, two-player, perfect-information zero-sum game, minimax finds the value a player can guarantee against an optimal opponent. Every score below is measured from MAX’s perspective; MIN wants that same number to be smaller.
Work backward from terminal scores
In a fictional board game, MAX chooses left or right. MIN then chooses a reply. Left leads to terminal scores 3 or 5, right to 2 or 8. MIN selects min(3,5)=3 or min(2,8)=2. MAX chooses max(3,2)=3, so left is safer despite the tempting 8 on the right. A terminal state is a state where the game has ended. The value at each internal node is the maximum or minimum of its child values, according to whose turn it is.
def minimax(tree, maximizing=True): if isinstance(tree, int): return tree values = [minimax(child, not maximizing) for child in tree] return max(values) if maximizing else min(values)
print(minimax([[3, 5], [2, 8]]))print(minimax([[3, 5], [2, 8]], False))Expected output: 3 and 5. In the second call MIN is at the root, MAX controls the next level, so min(max(3,5),max(2,8))=5. An integer represents a terminal score and a nonempty list represents available child positions. The tree must be finite; no cycles, empty lists, chance nodes, or hidden information are represented. Recursion follows one branch at a time. A full search examines every leaf.
Alpha-beta pruning preserves the root value
Alpha is the best lower bound MAX has established along the current search context; beta is the best upper bound MIN has established. Start with negative and positive infinity. Visit the left branch first; its value is 3, so alpha becomes 3 at the root. On the right MIN branch the first result is 2, setting beta to 2. Since alpha ≥ beta, visiting the remaining leaf 8 cannot improve the root’s choice. Pruning skips that work, not an actual legal move in the game.
def alpha_beta(tree, maximizing=True): visited = []
def visit(node, is_max, alpha, beta): if isinstance(node, int): visited.append(node) return node value = float("-inf") if is_max else float("inf") for child in node: score = visit(child, not is_max, alpha, beta) if is_max: value = max(value, score) alpha = max(alpha, value) else: value = min(value, score) beta = min(beta, value) if alpha >= beta: break return value
score = visit(tree, maximizing, float("-inf"), float("inf")) return score, visited
print(alpha_beta([[3, 5], [2, 8]]))print(alpha_beta([[8, 2], [3, 5]]))Expected output: (3, [3, 5, 2]) and (3, [8, 2, 3, 5]). Reordering the same branches and replies changes leaf count but not the root value. A cutoff can return a bound rather than an exact value for an internal node; the full-window root result is exact. This teaching function returns the root score and visited leaves, not a full strategy or all optimal moves. To select a move, evaluate child values with the appropriate next-player role and choose an optimal one, with an explicit tie policy.
Limits and Monte Carlo tree search
For branching factor b and depth d, a full uniform game tree can require on the order of b^d leaf visits. Alpha-beta has the same worst-case exponential order, with favorable ordering often visiting far fewer leaves. A depth-limited search can substitute a position evaluation at nonterminal cutoff states, but that estimate removes the full-game optimality guarantee.
Monte Carlo tree search (MCTS) instead allocates a simulation budget across a growing tree. A typical iteration selects a promising existing branch, expands an unexplored child, estimates an outcome with a rollout or evaluation, then backs up outcome statistics along the visited path. Selection balances trying uncertain moves and exploiting promising ones. For adversarial games, statistics must account for whose turn and whose reward is being measured. With a finite budget, the recommended move is an estimate, not a proof of optimality. A rollout average is not automatically a minimax value. Chance nodes, simultaneous moves and hidden information need different modeling assumptions.
Practice and common mistakes
For [[-4,-2],[-3,-8]], calculate the MAX-root value before running code. Reverse each reply list, compare the visited leaves, and explain why the value stays fixed. Avoid maximizing at every level, using different score perspectives inconsistently, or claiming that alpha-beta always halves runtime. For a much larger game, explain the trade-off between full search, depth-limited estimates and a bounded MCTS approach.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Minimax and alpha-beta — understanding check
Apply the search strategy and explain its assumptions.
What is the MAX-root value of [[3,5],[2,8]]?
After visiting leaves 3, 5 and 2 in the lesson tree, why may alpha-beta skip leaf 8?
What can change when the same complete game tree is searched in a different child order using full-window alpha-beta?
If depth-limited minimax replaces nonterminal subtrees with approximate evaluations, which guarantee is generally lost?
What does a typical finite-budget MCTS search provide?