1. Home
  2. AI & Machine Learning
  3. Minimax & Alpha–Beta Pruning

Minimax & Alpha–Beta Pruning

How a computer plays games like tic-tac-toe and chess. Watch values flow up a 3D game tree — and see alpha-beta cut away branches that can't matter.

Interactive 3DIntermediate13 min readAI/MLUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

Minimax is how a computer plays games like tic-tac-toe or chess: it imagines every move, every reply, and so on, assuming the opponent always plays their best. Alpha-beta pruning skips moves that can’t change the result. Press Run.

Pseudocode

  1. procedure minimax(node, α, β, maximizing):
  2. if node is a leaf: return its score
  3. if maximizing:
  4. best ← −∞; for each child: best ← max(best, minimax(child, α, β, false))
  5. α ← max(α, best); if β ≤ α: prune the remaining children
  6. else:
  7. best ← +∞; for each child: best ← min(best, minimax(child, α, β, true))
  8. β ← min(β, best); if β ≤ α: prune the remaining children
  9. return best
Read this model as text · 25 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. Minimax is how a computer plays games like tic-tac-toe or chess: it imagines every move, every reply, and so on, assuming the opponent always plays their best. Alpha-beta pruning skips moves that can’t change the result. Press Run.
  2. Two players take turns. MAX (boxes) wants the highest score, MIN (spheres) the lowest. The leaves are how good each final position is for MAX. α = best score MAX can already guarantee, β = best score MIN can guarantee.
  3. MAX node: look at each move and keep the highest result.
  4. MIN node: look at each move and keep the lowest result.
  5. Leaf: score 3.
  6. Back at this MIN node: child returned 3, so its value is now 3. (α = −∞, β = 3)
  7. Leaf: score 12.
  8. Back at this MIN node: child returned 12, so its value is now 3. (α = −∞, β = 3)
  9. Leaf: score 8.
  10. Back at this MIN node: child returned 8, so its value is now 3. (α = −∞, β = 3)
  11. Back at this MAX node: child returned 3, so its value is now 3. (α = 3, β = +∞)
  12. MIN node: look at each move and keep the lowest result.
  13. Leaf: score 2.
  14. Back at this MIN node: child returned 2, so its value is now 2. (α = 3, β = 2)
  15. β (2) ≤ α (3): MAX already has a better option elsewhere, so MAX will never choose this branch. Prune the rest without looking.
  16. Back at this MAX node: child returned 2, so its value is now 3. (α = 3, β = +∞)
  17. MIN node: look at each move and keep the lowest result.
  18. Leaf: score 14.
  19. Back at this MIN node: child returned 14, so its value is now 14. (α = 3, β = 14)
  20. Leaf: score 5.
  21. Back at this MIN node: child returned 5, so its value is now 5. (α = 3, β = 5)
  22. Leaf: score 2.
  23. Back at this MIN node: child returned 2, so its value is now 2. (α = 3, β = 2)
  24. Back at this MAX node: child returned 2, so its value is now 3. (α = 3, β = +∞)
  25. The root's value is 3: with perfect play from both sides, MAX can guarantee a score of 3 by taking the green move. Alpha-beta evaluated only 7 of 9 leaves — same answer, less work.

Try this in the 3D model

  • Run Plain minimax on the textbook tree and count the evaluated leaves. Then run alpha-beta.
  • Pause when a branch gets pruned. Read the α and β tags — why can it be skipped safely?
  • Try a Random tree a few times. Does pruning ever change the final answer?

Games as trees

In a two-player game like tic-tac-toe or chess, every position has possible moves; each move leads to a new position with the opponent’s possible replies, and so on. Drawn out, this is a game tree:

  • the root is the current position,
  • each level alternates between MAX (you) and MIN (the opponent),
  • the leaves are final (or far-enough) positions with a score — high is good for MAX.

Minimax

Assume both players play perfectly:

  • at a MAX node, pick the child with the highest value,
  • at a MIN node, pick the child with the lowest value,
  • at a leaf, use its score.

Values are computed bottom-up (with a depth-first search), and the root’s value tells MAX the best guaranteed outcome. The move leading to it is the best move.

In the textbook example of the model: the three MIN nodes get values min(3,12,8) = 3, min(2,4,6) = 2 and min(14,5,2) = 2, so MAX picks the first move with value 3.

Alpha–beta pruning

Minimax looks at every leaf — impossible for chess, which has about 35 moves per position. Alpha–beta gets the same answer while skipping branches that cannot matter. It carries two numbers down the tree:

  • α — the best score MAX can already guarantee (starts at −∞, only goes up),
  • β — the best score MIN can already guarantee (starts at +∞, only goes down).

Whenever β ≤ α at a node, its remaining children are pruned: one player already has a better option elsewhere and will never let the game reach this point.

Example: after the first MIN node, α = 3 at the root. In the second MIN node, the first leaf is 2 — so MIN can force ≤ 2 here. MAX already has 3 elsewhere, so MAX will never choose this branch: the leaves 4 and 6 don’t need to be looked at.

With good move ordering (best moves first), alpha-beta searches twice as deep as plain minimax in the same time.

Code

import math

def alphabeta(node, alpha, beta, maximizing):
    if isinstance(node, (int, float)):          # leaf: a score
        return node
    if maximizing:
        best = -math.inf
        for child in node:
            best = max(best, alphabeta(child, alpha, beta, False))
            alpha = max(alpha, best)
            if beta <= alpha:
                break                           # β cut-off: prune
        return best
    else:
        best = math.inf
        for child in node:
            best = min(best, alphabeta(child, alpha, beta, True))
            beta = min(beta, best)
            if beta <= alpha:
                break                           # α cut-off: prune
        return best

tree = [[3, 12, 8], [2, 4, 6], [14, 5, 2]]      # MAX root, three MIN nodes
print(alphabeta(tree, -math.inf, math.inf, True))   # 3

Real game engines add…

  • Depth limits + evaluation functions: stop at a fixed depth and estimate the score (material, mobility, king safety…).
  • Move ordering and iterative deepening to prune more.
  • Transposition tables (hash tables) to remember positions already searched.
  • Modern engines like AlphaZero and Stockfish NNUE combine search with neural networks.

Common mistakes

  • Swapping max and min at the wrong levels.
  • Forgetting that pruning depends on the order children are visited.
  • Thinking alpha-beta is an approximation — it gives exactly the minimax value.

Complexity at a glance

Case / operationTimeWhy
Minimax (branching b, depth d)O(bᵈ)Visits every leaf.
Alpha-beta, best move orderingO(b^(d/2))Can search twice as deep in the same time.
Alpha-beta, worst orderingO(bᵈ)
Extra spaceO(b · d)

Quick check

Test yourself — pick an answer to see if you got it.

1. In minimax, what does a MIN node choose?

2. What is α in alpha-beta pruning?

3. When does alpha-beta prune the remaining children of a node?

4. Does alpha-beta pruning ever change the move that minimax chooses?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Minimax & Alpha–Beta Pruning. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.