Adversarial Search and Minimax
How a program plays a game against an opponent who is trying to beat it: the minimax value, why alpha-beta pruning reaches the same answer while examining fewer nodes, and a game tree pruned move by move.
Searching for a route differs from playing a game in one decisive respect: in a game, someone else moves next, and they are trying to make you lose. You cannot plan a fixed sequence of actions, because your opponent's replies are not yours to choose. Adversarial search handles this by assuming the opponent plays as well as possible, and computing the best response to that.
A. The game tree
Two players, conventionally MAX (who moves first and maximises) and MIN (who minimises the same quantity). A game tree has the initial position at the root, one branch per legal move, alternating layers of MAX and MIN nodes, and terminal positions at the leaves carrying a utility - the payoff to MAX.
Because MIN's utility is the negation of MAX's, this is a zero-sum game: what one gains the other loses exactly. That is what allows a single number per leaf to describe the outcome for both.
B. The minimax value
The value of a node is defined recursively:
MAX picks the largest value among the children; MIN picks the smallest. Values propagate from the leaves up to the root, and MAX's best move at the root is the one leading to the child whose value equals the root's.
What the assumption buys, and what it costs. Minimax assumes the opponent plays optimally. Against an optimal opponent the value is exactly what MAX can guarantee, so it is a genuine worst-case guarantee, not a prediction. Against a weak opponent it is conservative: it may pass up a trap that a fallible opponent would fall into.
C. Working a tree by hand
A three-ply tree: the root is MAX, its three children are MIN nodes, each with three terminal children.
| MIN node | Terminal values |
|---|---|
| 3, 12, 8 | |
| 2, 4, 6 | |
| 14, 5, 2 |
The MIN layer. Each MIN node takes the minimum of its children:
The root. MAX takes the maximum:
MAX should move to , guaranteeing at least 3.
Note how little the large leaf values matter. The under and the under are never obtained, because MIN would never allow them - MIN moves to the and the respectively. Only the minima of each branch survive.
D. Alpha-beta pruning
Minimax examines every leaf, which is hopeless for real games - the tree grows exponentially with depth. Alpha-beta pruning computes the identical value while skipping branches that provably cannot affect it.
Two values are carried down the search, defined by Russell & Norvig as:
- - the value of the best (highest-value) choice found so far at any choice point along the path for MAX;
- - the value of the best (lowest-value) choice found so far along the path for MIN.
The search prunes the remaining branches at a node as soon as the node's value is known to be worse than the current (for MAX) or (for MIN).
E. Pruning the tree, step by step
Tracing the same tree left to right:
Node . Examine , , . Nothing can be pruned - this is the first branch and MAX has no yet. , so the root sets : MAX can already guarantee 3.
Node . Examine the first leaf, . is a MIN node, so its final value is at most - MIN can only go lower from here.
But MAX already has a guaranteed 3 elsewhere. A branch worth at most 2 will never be chosen over one worth 3, regardless of what the remaining leaves contain. So the leaves and are never examined. This is the prune.
Node . Examine : value so far , still above , no prune. Examine : value so far , still above . Examine : value . .
Root. - the same answer as full minimax.
Alpha-beta examined 7 of the 9 leaves, pruning 's second and third children.
Interactive: the leaves it never has to look at
MAX at the root, MIN below, twelve leaves.
- Examined
- 7 / 9
- Never examined
- 2
- Root value
- 3
2 of the twelve leaves were never evaluated, and the answer is identical to minimax’s. Note node D: it contains the largest leaf in the whole tree, 14, and it is worth 2, because MAX does not choose which leaf of D is reached - MIN does. A branch is worth what your opponent will allow, not what it contains. And note that a cut node’s figure is shown as at most a value rather than equal to it: the search stopped before finding out how much lower it went, which is exactly the work it saved.
Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.
Running it prints value 3 from both methods, examined: [3, 12, 8, 2, 14, 5, 2]
(seven leaves), and the two pruned leaves.
F. Why move ordering decides everything
Pruning depends on finding good moves early. If MAX's best move is examined first, rises immediately and prunes aggressively. If it is examined last, there is nothing to prune against until the end.
With perfect ordering, alpha-beta examines roughly nodes instead of for branching factor and depth . That halved exponent means searching twice as deep in the same time - the difference between an amateur and an expert program.
Perfect ordering requires knowing the answer in advance, so real programs approximate it with heuristics: try captures first, try the move that was best at the previous shallower depth, and so on.
G. When the tree is too large regardless
Even halved, the exponent defeats games like chess or Go. Practical programs stop early and apply an evaluation function to non-terminal positions, estimating the utility rather than computing it.
This introduces two new problems worth naming. The horizon effect is the tendency to push an unavoidable loss just past the search depth, so it looks avoided when it is merely out of sight. And the evaluation function must be applied at quiescent positions - stopping in the middle of an exchange gives a badly wrong estimate, so search is extended until things settle.
Historically, alpha-beta search was conceived by John McCarthy in 1956; its correctness and time complexity were established by Knuth and Moore in 1975.
Key takeaways
- Adversarial search assumes an optimal opponent, making the minimax value a worst-case guarantee.
- MAX maximises, MIN minimises, and values propagate from the leaves to the root.
- Our tree: MIN nodes ; root value ; MAX plays to .
- Alpha-beta returns the identical value while skipping provably irrelevant branches - 7 leaves instead of 9 here.
- is MAX's best guarantee so far, is MIN's; a branch is cut once it cannot beat them.
- Move ordering determines the benefit; perfect ordering roughly halves the effective depth exponent.
What's next
Minimax assumes strict opposition. Most real strategic situations are not zero-sum - players may both gain or both lose, and "optimal play" needs redefining. That generalisation is Game Theory and Nash Equilibrium.
References & further reading
- Stuart Russell, Peter Norvig, Artificial Intelligence: A Modern Approach, Pearson (3rd edition), 2010· Kudos AI reference library
Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.