# Alpha–beta pruning

Jump to navigation Jump to search
Class Search algorithm ${\displaystyle O(b^{d})}$ ${\displaystyle O\left({\sqrt {b^{d}}}\right)}$

Alpha–beta pruning is a search algorithm that seeks to decrease the number of nodes that are evaluated by the minimax algorithm in its search tree. It is an adversarial search algorithm used commonly for machine playing of two-player games (Tic-tac-toe, Chess, Go, etc.). It stops evaluating a move when at least one possibility has been found that proves the move to be worse than a previously examined move. Such moves need not be evaluated further. When applied to a standard minimax tree, it returns the same move as minimax would, but prunes away branches that cannot possibly influence the final decision.[1]

## History

Allen Newell and Herbert A. Simon who used what John McCarthy calls an "approximation"[2] in 1958 wrote that alpha–beta "appears to have been reinvented a number of times".[3] Arthur Samuel had an early version for a checkers simulation. Richards, Timothy Hart, Michael Levin and/or Daniel Edwards also invented alpha–beta independently in the United States.[4] McCarthy proposed similar ideas during the Dartmouth workshop in 1956 and suggested it to a group of his students including Alan Kotok at MIT in 1961.[5] Alexander Brudno independently conceived the alpha–beta algorithm, publishing his results in 1963.[6] Donald Knuth and Ronald W. Moore refined the algorithm in 1975.[7][8] Judea Pearl proved its optimality for trees with randomly assigned leaf values in terms of the expected running time in two papers.[9][10] The optimality of the randomized version of alpha-beta was shown by Michael Saks and Avi Wigderson in 1986.[11]

## Core idea

The algorithm maintains two values, alpha and beta, which represent the minimum score that the maximizing player is assured of and the maximum score that the minimizing player is assured of respectively. Initially, alpha is negative infinity and beta is positive infinity, i.e. both players start with their worst possible score. Whenever the maximum score that the minimizing player (i.e. the "beta" player) is assured of becomes less than the minimum score that the maximizing player (i.e., the "alpha" player) is assured of (i.e. beta < alpha), the maximizing player need not consider further descendants of this node, as they will never be reached in the actual play.

To illustrate this with a real-life example, suppose you're playing chess and it is your turn. You have found a good move that will improve your position. Denote this move A. You continue to look for moves, making sure you haven't missed an even better one. You find a move that appears to be good. Denote this move B. You then realize that move B allows your opponent to force checkmate in two moves. Thus, you no longer need to consider any other possible outcomes from playing move B since your opponent can force a win. The maximum score that your opponent could force after move B is negative infinity: a loss for you. This is less than the minimum position that you've already found: position A does not result in a forced loss in two moves.

## Improvements over naive minimax

An illustration of alpha–beta pruning. The grayed-out subtrees don't need to be explored (when moves are evaluated from left to right), since we know the group of subtrees as a whole yields the value of an equivalent subtree or worse, and as such cannot influence the final result. The max and min levels represent the turn of the player and the adversary, respectively.

The benefit of alpha–beta pruning lies in the fact that branches of the search tree can be eliminated. This way, the search time can be limited to the 'more promising' subtree, and a deeper search can be performed in the same time. Like its predecessor, it belongs to the branch and bound class of algorithms. The optimization reduces the effective depth to slightly more than half that of simple minimax if the nodes are evaluated in an optimal or near optimal order (best choice for side on move ordered first at each node).

With an (average or constant) branching factor of b, and a search depth of d plies, the maximum number of leaf node positions evaluated (when the move ordering is pessimal) is O(b×b×...×b) = O(bd) – the same as a simple minimax search. If the move ordering for the search is optimal (meaning the best moves are always searched first), the number of leaf node positions evaluated is about O(b×1×b×1×...×b) for odd depth and O(b×1×b×1×...×1) for even depth, or ${\displaystyle O(b^{d/2})=O({\sqrt {b^{d}}})}$. In the latter case, where the ply of a search is even, the effective branching factor is reduced to its square root, or, equivalently, the search can go twice as deep with the same amount of computation.[12] The explanation of b×1×b×1×... is that all the first player's moves must be studied to find the best one, but for each, only the second player's best move is needed to refute all but the first (and best) first player move—alpha–beta ensures no other second player moves need be considered. When nodes are considered in a random order (i.e., the algorithm randomizes), asymptotically, the expected number of nodes evaluated in uniform trees with binary leaf-values is ${\displaystyle \Theta (((b-1+{\sqrt {b^{2}+14b+1}})/4)^{d})}$ .[11] For the same trees, when the values are assigned to the leaf values independently of each other and say zero and one are both equally probable, the expected number of nodes evaluated is ${\displaystyle \Theta ((b/2)^{d})}$, which is much smaller than the work done by the randomized algorithm, mentioned above, and is again optimal for such random trees.[9] When the leaf values are chosen independently of each other but from the ${\displaystyle [0,1]}$ interval uniformly at random, the expected number of nodes evaluated increases to ${\displaystyle \Theta (b^{d/log(d)})}$ in the ${\displaystyle d\to \infty }$ limit,[10] which is again optimal for these kind random trees. Note that the actual work for "small" values of ${\displaystyle d}$ is better approximated using ${\displaystyle 0.925d^{0.747}}$.[10][9]

An animated pedagogical example that attempts to be human-friendly by substituting initial infinite (or arbitrarily large) values for emptiness and by avoiding using the negamax coding simplifications.

Normally during alpha–beta, the subtrees are temporarily dominated by either a first player advantage (when many first player moves are good, and at each search depth the first move checked by the first player is adequate, but all second player responses are required to try to find a refutation), or vice versa. This advantage can switch sides many times during the search if the move ordering is incorrect, each time leading to inefficiency. As the number of positions searched decreases exponentially each move nearer the current position, it is worth spending considerable effort on sorting early moves. An improved sort at any depth will exponentially reduce the total number of positions searched, but sorting all positions at depths near the root node is relatively cheap as there are so few of them. In practice, the move ordering is often determined by the results of earlier, smaller searches, such as through iterative deepening.

Additionally, this algorithm can be trivially modified to return an entire principal variation in addition to the score. Some more aggressive algorithms such as MTD(f) do not easily permit such a modification.

## Pseudocode

The pseudo-code for depth limited minimax with alpha-beta pruning is as follows:[12]

function alphabeta(node, depth, α, β, maximizingPlayer) is
if depth = 0 or node is a terminal node then
return the heuristic value of node
if maximizingPlayer then
value := −∞
for each child of node do
value := max(value, alphabeta(child, depth − 1, α, β, FALSE))
α := max(α, value)
if α ≥ β then
break (* β cut-off *)
return value
else
value := +∞
for each child of node do
value := min(value, alphabeta(child, depth − 1, α, β, TRUE))
β := min(β, value)
if β ≤ α then
break (* α cut-off *)
return value

(* Initial call *)
alphabeta(origin, depth, −∞, +∞, TRUE)


Implementations of alpha-beta pruning can often be delineated by whether they are "fail-soft," or "fail-hard". The pseudo-code illustrates the fail-soft variation. With fail-soft alpha-beta, the alphabeta function may return values (v) that exceed (v < α or v > β) the α and β bounds set by its function call arguments. In comparison, fail-hard alpha-beta limits its function return value into the inclusive range of α and β.

## Heuristic improvements

Further improvement can be achieved without sacrificing accuracy by using ordering heuristics to search earlier parts of the tree that are likely to force alpha–beta cutoffs. For example, in chess, moves that capture pieces may be examined before moves that do not, and moves that have scored highly in earlier passes through the game-tree analysis may be evaluated before others. Another common, and very cheap, heuristic is the killer heuristic, where the last move that caused a beta-cutoff at the same tree level in the tree search is always examined first. This idea can also be generalized into a set of refutation tables.

Alpha–beta search can be made even faster by considering only a narrow search window (generally determined by guesswork based on experience). This is known as aspiration search. In the extreme case, the search is performed with alpha and beta equal; a technique known as zero-window search, null-window search, or scout search. This is particularly useful for win/loss searches near the end of a game where the extra depth gained from the narrow window and a simple win/loss evaluation function may lead to a conclusive result. If an aspiration search fails, it is straightforward to detect whether it failed high (high edge of window was too low) or low (lower edge of window was too high). This gives information about what window values might be useful in a re-search of the position.

Over time, other improvements have been suggested, and indeed the Falphabeta (fail-soft alpha-beta) idea of John Fishburn is nearly universal and is already incorporated above in a slightly modified form. Fishburn also suggested a combination of the killer heuristic and zero-window search under the name Lalphabeta ("last move with minimal window alpha-beta search").

## Negamax variation

The negamax variation evaluates each node from the last player's point of view. To predict if a move is a good one, we have to generate a tree of depth as large as possible, evaluate heuristically the leaves (from the point of view of the player that made this move) and then calculate the score of the root with the assumption that each player chooses the best move. For instance, with a tree of depth= 1, if the opponent has three possible reponses evaluated −1, −2 and +∞, we evaluate the root configuration as −∞. We assume that the opponent will indeed choose the best configuration evaluated +∞. From this example, we can deduce the general rule : the score of a node is given by the opposite of the maximum score of its children.

Alpha beta pruning is an optimisation of the minimax algorithm that avoids computing the whole tree to the leaves for a given depth.

Here is the algorithm in pseudocode where i is the limit value under which it is useless going on compututing. Initially, we take i = -∞. Moreover, the children of a node are computed only if necessary. That is why, we use an iterator to compute them.

alphabeta(node, depth, i) gives the score of node node knowing that it is useless to continue if the score is lower than or equal to i (in this case, it gives a wrong score that has no impact on the final result).

function alphabeta(node, depth, i) is
if depth = 0 or node is a terminal node then
return the heuristic value of node
else
j := -∞
for each child of node do
j := max(j, alphabeta(child, depth − 1, j))
if -j ⩽ i then
return -j '(* cut-off *)
return -j


## Other algorithms

Since the minimax algorithm and its variants are inherently depth-first, a strategy such as iterative deepening is usually used in conjunction with alpha–beta so that a reasonably good move can be returned even if the algorithm is interrupted before it has finished execution. Another advantage of using iterative deepening is that searches at shallower depths give move-ordering hints, as well as shallow alpha and beta estimates, that both can help produce cutoffs for higher depth searches much earlier than would otherwise be possible.

Algorithms like SSS*, on the other hand, use the best-first strategy. This can potentially make them more time-efficient, but typically at a heavy cost in space-efficiency.[13]

## References

1. ^ Russell, Stuart J.; Norvig, Peter (2010). Artificial Intelligence: A Modern Approach (3rd ed.). Upper Saddle River, New Jersey: Pearson Education, Inc. p. 167. ISBN 978-0-13-604259-4.
2. ^ McCarthy, John (27 November 2006). "Human Level AI Is Harder Than It Seemed in 1955". Retrieved 2006-12-20.
3. ^ Newell, Allen; Simon, Herbert A. (1 March 1976). "Computer science as empirical inquiry: symbols and search". Communications of the ACM. 19 (3): 113–126. doi:10.1145/360018.360022.
4. ^ Edwards, D.J.; Hart, T.P. (4 December 1961). "The Alpha–beta Heuristic (AIM-030)". Massachusetts Institute of Technology. hdl:1721.1/6098. Cite journal requires |journal= (help)
5. ^ Kotok, Alan (3 December 2004). "MIT Artificial Intelligence Memo 41". Retrieved 2006-07-01.
6. ^ Marsland, T.A. (May 1987). "Computer Chess Methods (PDF) from Encyclopedia of Artificial Intelligence. S. Shapiro (editor)" (PDF). J. Wiley & Sons. pp. 159–171. Archived from the original (PDF) on October 30, 2008. Retrieved 2006-12-21.
7. ^ Knuth, Donald E.; Moore, Ronald W. (1975). "An analysis of alpha-beta pruning". Artificial Intelligence. 6 (4): 293–326. doi:10.1016/0004-3702(75)90019-3. S2CID 7894372.
8. ^ Abramson, Bruce (1 June 1989). "Control strategies for two-player games". ACM Computing Surveys. 21 (2): 137–161. doi:10.1145/66443.66444.
9. ^ a b c Pearl, Judea (1980). "Asymptotic Properties of Minimax Trees and Game-Searching Procedures". Artificial Intelligence. 14 (2): 113–138. doi:10.1016/0004-3702(80)90037-5.
10. ^ a b c Pearl, Judea (1982). "The Solution for the Branching Factor of the Alpha-Beta Pruning Algorithm and Its Optimality". Communications of the ACM. 25 (8): 559–64. doi:10.1145/358589.358616.
11. ^ a b Saks, M.; Wigderson, A. (1986). "Probabilistic Boolean Decision Trees and the Complexity of Evaluating Game Trees". 27th Annual Symposium on Foundations of Computer Science. pp. 29–38. doi:10.1109/SFCS.1986.44. ISBN 0-8186-0740-8.
12. ^ a b Russell, Stuart J.; Norvig, Peter (2003), Artificial Intelligence: A Modern Approach (2nd ed.), Upper Saddle River, New Jersey: Prentice Hall, ISBN 0-13-790395-2
13. ^ Pearl, Judea; Korf, Richard (1987), "Search techniques", Annual Review of Computer Science, 2: 451–467, doi:10.1146/annurev.cs.02.060187.002315, Like its A* counterpart for single-player games, SSS* is optimal in terms of the average number of nodes examined; but its superior pruning power is more than offset by the substantial storage space and bookkeeping required.

## Bibliography

• George T. Heineman; Gary Pollice; Stanley Selkow (2008). "Chapter 7: Path Finding in AI". Algorithms in a Nutshell. Oreilly Media. pp. 217–223. ISBN 978-0-596-51624-6.
• Judea Pearl, Heuristics, Addison-Wesley, 1984
• John P. Fishburn (1984). "Appendix A: Some Optimizations of α-β Search". Analysis of Speedup in Distributed Algorithms (revision of 1981 PhD thesis). UMI Research Press. pp. 107–111. ISBN 0-8357-1527-2.