function iterativeDeepening(root: Node, maxDepth: number, deadline: number) {let bestMove = root.children[0];
for (let limit = 1; limit <= maxDepth && Date.now() < deadline; limit++) {bestMove = depthLimitedMinimax(root, limit).move!;
}
return bestMove;
}
function depthLimitedMinimax(node: Node, limit: number, maximizing = true): { value: number; move?: Node } { if (node.isLeaf) return { value: node.value }; if (limit === 0) return { value: heuristic(node) }; const scored = node.children.map((child) => ({ child, value: depthLimitedMinimax(child, limit - 1, !maximizing).value }));const best = scored.reduce((a, b) => (maximizing ? (b.value > a.value ? b : a) : (b.value < a.value ? b : a)));
return { value: best.value, move: best.child };}
Iteration 1: search 1 ply deep. The move found so far stays valid if time runs out.
Understanding Iterative deepening
The idea
A game engine never knows how deep it can afford to search before the clock runs out. Iterative deepening searches to depth 1, then again to depth 2, then 3, keeping the best move of the last completed iteration. Whenever time is up, an answer from a full search at some depth is ready.
It looks wasteful, but the tree grows geometrically with depth, so all the shallow passes together cost less than the last one alone. And the shallow passes are not wasted: their best moves are used to order the children of the next pass, which is what makes alpha-beta prune well. Every serious chess engine works this way.
The four stages
Complexity
Versus its siblings
Pseudocode
ITERATIVEDEEPENING(root, maxDepth, deadline)
for limit ← 1, 2, …, maxDepth while time remains
bestMove ← DEPTHLIMITEDMINIMAX(root, limit)
return bestMove
DEPTHLIMITEDMINIMAX(node, limit): a leaf returns its score; at limit 0 return the static evaluation; else minimax over the children with limit − 1
When to use
Pitfalls
History
David Slate and Larry Atkin used iterative deepening in Chess 4.5 in 1975 to manage clock time, and Richard Korf analysed the general technique in 1985, showing the overhead is a constant factor. Every engine since, from Deep Blue to Stockfish, deepens iteratively.