The Alpha-Beta algorithm (Alpha-Beta Pruning, Alpha-Beta Heuristic) is a significant enhancement to the minimax search algorithm that eliminates the need to search large portions of the game tree applying a branch-and-bound technique.
The savings of alpha beta can be considerable.
The minimum number of leaves with depth n and b = 40
Depth | Worst case | Best case |
---|---|---|
n | b^n | b^ceil(n/2) + b^floor(n/2) - 1 |
1 | 40 | 40 |
2 | 1,600 | 79 |
3 | 64,000 | 1,639 |
4 | 2,560,000 | 3,199 |
5 | 102,400,000 | 65,569 |
6 | 4,096,000,000 | 127,999 |
7 | 163,840,000,000 | 2,623,999 |
8 | 6,553,600,000,000 | 5,119,999 |
NOTE: Assuming constantly b moves for each node visited and search depth n, the maximal number of leaves in alpha-beta is equivalent to Minimax, b ^ n.
int alphaBeta( int alpha, int beta, int depthleft ) { if( depthleft == 0 ) return quiesce( alpha, beta ); for ( all moves) { score = -alphaBeta( -beta, -alpha, depthleft - 1 ); if( score >= beta ) return beta; // fail hard beta-cutoff if( score > alpha ) alpha = score; // alpha acts like max in MiniMax } return alpha; }
NOTE: This makes use of a Quiescence Search, which makes the alpha-beta search much more stable.
score = alphaBetaMax(-oo, +oo, depth);
int alphaBetaMax( int alpha, int beta, int depthleft ) { if ( depthleft == 0 ) return evaluate(); for ( all moves) { score = alphaBetaMin( alpha, beta, depthleft - 1 ); if( score >= beta ) return beta; // fail hard beta-cutoff if( score > alpha ) alpha = score; // alpha acts like max in MiniMax } return alpha; } int alphaBetaMin( int alpha, int beta, int depthleft ) { if ( depthleft == 0 ) return -evaluate(); for ( all moves) { score = alphaBetaMax( alpha, beta, depthleft - 1 ); if( score <= alpha ) return alpha; // fail hard alpha-cutoff if( score < beta ) beta = score; // beta acts like min in MiniMax } return beta; }