Table of Contents

Chess - Programming - Search - Alpha-Beta

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.


Savings

The savings of alpha beta can be considerable.

The minimum number of leaves with depth n and b = 40

DepthWorst caseBest case
nb^nb^ceil(n/2) + b^floor(n/2) - 1
14040
21,60079
364,0001,639
42,560,0003,199
5102,400,00065,569
64,096,000,000127,999
7163,840,000,0002,623,999
86,553,600,000,0005,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.

  • Considering always the best move first, it is b ^ ceil(n/2) plus b ^ floor(n/2) minus one.
  • This table which also demonstrates the odd-even effect.

Alpha-Beta

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;
}

References

http://web.archive.org/web/20120421170110/http://chessprogramming.wikispaces.com/Alpha-Beta

http://web.archive.org/web/20120222165548/http://chessprogramming.wikispaces.com/Odd-Even+Effect