🧠 AI Foundations · Lecture 7 of 24

Adversarial Search: Minimax and Alpha–Beta Pruning

When an opponent is trying to defeat you, search must account for their choices. We derive minimax, prove alpha–beta pruning correct, and see how real game engines evaluate positions.

So far our agents have been alone in the world. Today an adversary enters the room. In chess, draughts or tic-tac-toe, the opponent's moves are chosen to minimise your outcome. Planning under such opposition is called adversarial search, and it produced some of AI's most famous triumphs — from Deep Blue to AlphaZero.

Games as search problems#

We model a two-player, zero-sum, perfect-information game with:

  • $s_0$: the initial position;
  • $\text{ToMove}(s)$: whose turn it is (MAX or MIN);
  • $\text{Actions}(s)$ and $\text{Result}(s, a)$;
  • $\text{IsTerminal}(s)$;
  • $\text{Utility}(s, p)$: the final payoff for player $p$ (e.g. +1 win, 0 draw, −1 loss).

"Zero-sum" means one player's gain is the other's loss, so a single number describes the outcome.

The minimax value#

Assume both players play optimally. The minimax value of a state is

$$ \text{MM}(s) = \begin{cases} \text{Utility}(s) & \text{if terminal} \\ \max_{a} \text{MM}(\text{Result}(s,a)) & \text{if MAX to move} \\ \min_{a} \text{MM}(\text{Result}(s,a)) & \text{if MIN to move} \end{cases} $$

MAX chooses the move leading to the highest minimax value. This recursion is a depth-first exploration of the whole game tree, with time $O(b^m)$ and space $O(bm)$. For chess ($b \approx 35$, $m \approx 80$ plies) that is about $10^{123}$ nodes — utterly infeasible. We need two ideas: pruning and evaluation functions.

Alpha–beta pruning#

Alpha–beta computes exactly the same decision as minimax while ignoring branches that cannot influence it. It carries two bounds:

  • $\alpha$: the best value MAX can already guarantee on the current path;
  • $\beta$: the best value MIN can already guarantee on the current path.

Whenever $\alpha \ge \beta$, the current node cannot affect the final decision, so we stop exploring it.

python
import math

def alphabeta(state, depth, alpha, beta, maximizing, game):
    if depth == 0 or game.is_terminal(state):
        return game.evaluate(state)
    if maximizing:
        value = -math.inf
        for move in game.ordered_moves(state):
            value = max(value, alphabeta(game.result(state, move), depth - 1,
                                         alpha, beta, False, game))
            alpha = max(alpha, value)
            if alpha >= beta:
                break           # beta cut-off: MIN will never allow this line
        return value
    else:
        value = math.inf
        for move in game.ordered_moves(state):
            value = min(value, alphabeta(game.result(state, move), depth - 1,
                                         alpha, beta, True, game))
            beta = min(beta, value)
            if alpha >= beta:
                break           # alpha cut-off: MAX already has a better option
        return value

Why pruning is safe — an intuition#

Suppose MAX has already found a move worth 5. Exploring a second move, MIN's first reply gives 3. MIN will choose at most 3 at that node, which is already worse for MAX than 5. Whatever the remaining replies are, MAX will not choose this move, so we prune them.

How much does it help?#

The effectiveness depends on move ordering. With perfect ordering alpha–beta examines about $O(b^{m/2})$ nodes — the effective branching factor drops from $b$ to $\sqrt{b}$, letting you search roughly twice as deep in the same time. With random ordering it is about $O(b^{3m/4})$.

Imperfect real-time decisions#

Since we cannot reach terminal states, we cut off search at a depth limit and apply a heuristic evaluation function $\text{Eval}(s)$ estimating the expected utility. Classical chess engines used weighted linear features:

$$ \text{Eval}(s) = w_1 f_1(s) + w_2 f_2(s) + \dots + w_n f_n(s) $$

where $f_1$ might be material balance (queen = 9, rook = 5, …), $f_2$ mobility, $f_3$ king safety, and so on. Modern engines replace this hand-crafted function with a neural network trained on millions of positions — a beautiful meeting of search and learning.

Two pitfalls arise with cut-offs:

  • The horizon effect: a bad event (losing a queen) can be pushed just beyond the search depth by delaying moves.
  • Non-quiescent positions: evaluating in the middle of a capture sequence is misleading. Quiescence search extends the search on such positions until they are "quiet".

Beyond two-player deterministic games#

  • Stochastic games (backgammon) use expectiminimax, with chance nodes averaging over dice outcomes.
  • Imperfect information (poker) requires reasoning over belief states and game-theoretic equilibria.
  • Monte Carlo Tree Search, covered in a later lecture, replaces exhaustive search with random simulations and powers Go programs.
JA
Written by

Janin A Apurba

B.Sc. in CSE, AUST · Advanced ICT Officer, CNRS-UNHCR. Teaching AI, ML and Deep Learning to the next generation of engineers and researchers.

Keep learning

Related lectures

🧠 AI Foundations

Informed Search: Greedy Best-First and A* with Admissible Heuristics

Knowledge about where the goal lies transforms search. We derive A*, prove its optimality with admissible heuristics, and learn how to invent good heuristics by relaxing problems.

Intermediate⏱ 5 min#006
🧠 AI Foundations

Uninformed Search: BFS, DFS, Uniform-Cost and Iterative Deepening

Many AI problems reduce to finding a path in a huge graph. We formalise search problems and analyse the classic blind strategies for completeness, optimality, time and space.

Beginner⏱ 5 min#005
🧠 AI Foundations

Local Search: Hill Climbing, Simulated Annealing and Beam Search

When only the final configuration matters, we can abandon paths and move through the space of complete solutions. Local search is simple, memory-light and the conceptual ancestor of gradient descent.

Beginner⏱ 5 min#017