Alpha-beta pruning
transposition table
iterative deepening
game tree optimization
artificial intelligence

Alpha-beta pruning with a transposition table and iterative deepening

Codemia Knowledge Hub ·

In a strong game-tree search engine these three techniques work as one system, not as separate tricks. Alpha-beta cuts branches that cannot affect the result, the transposition table avoids re-searching positions reached by different move orders, and iterative deepening supplies the move ordering that lets alpha-beta prune much more.

Alpha-beta pruning shrinks the minimax tree

Alpha-beta keeps two bounds during minimax search:

  • alpha: the best score the maximizing side can already guarantee
  • beta: the best score the minimizing side can already guarantee

When a branch cannot improve the current result, the search prunes it. A simplified version:

python
1def alphabeta(node, depth, alpha, beta, maximizing):
2    if depth == 0 or node.is_terminal():
3        return node.evaluate()
4
5    if maximizing:
6        value = float("-inf")
7        for child in node.children():
8            value = max(value, alphabeta(child, depth - 1, alpha, beta, False))
9            alpha = max(alpha, value)
10            if alpha >= beta:
11                break
12        return value
13    else:
14        value = float("inf")
15        for child in node.children():
16            value = min(value, alphabeta(child, depth - 1, alpha, beta, True))
17            beta = min(beta, value)
18            if alpha >= beta:
19                break
20        return value

Alpha-beta performance depends heavily on move ordering: the better the ordering, the earlier the cutoffs happen.

Transposition tables reuse work across equivalent positions

Game trees often contain transpositions: different move sequences that reach the same board state. A transposition table stores previously searched positions so the engine can reuse the result instead of searching again.

A useful entry typically stores:

  • a hash key for the position
  • search depth
  • score
  • bound type such as exact, lower, or upper
  • best move if known

The bound type matters because alpha-beta does not always return an exact minimax value. Sometimes it only proves that the score is at least this high, or at most this low. That is still valuable when reused correctly.

Iterative deepening makes move ordering stronger

Iterative deepening searches depth 1, then depth 2, then depth 3, and so on until time runs out. Re-searching shallow depths looks wasteful, but shallow searches reveal a strong move ordering for the deeper ones:

  • depth 1 finds a plausible best move
  • depth 2 searches that move first
  • better ordering causes more alpha-beta cutoffs

So iterative deepening is not just time management; it directly improves search efficiency.

How the three reinforce each other

  • iterative deepening discovers good candidate moves
  • those moves are stored in the transposition table
  • deeper alpha-beta searches consult the table and search promising moves first
  • better ordering produces more pruning

An engine with all three usually performs much better than one with raw alpha-beta alone.

A typical search loop

At a high level, engines often do something like:

python
1best_move = None
2
3for depth in range(1, max_depth + 1):
4    score, move = search_root(position, depth, table)
5    best_move = move

Inside search_root, the engine:

  • probes the transposition table
  • orders moves using the stored best move first
  • runs alpha-beta search
  • stores the resulting score and best move back into the table

This is an anytime algorithm: if time runs out, the best move from the deepest completed iteration is still available.

Correctness depends on storing depth and bound flags

A common mistake is storing only a raw score per position and ignoring the depth searched. A score from a shallow search is not always safe to reuse in a deeper one, and a bound is not an exact value. That is why real tables store the position key, search depth, score and flag type. Without them, reuse becomes unsound or much less effective.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design