Alpha–Beta Pruning

Minimax, also called the minimax algorithm, is an algorithm that minimizes potential loss in the worst-case (that is, maximum-loss) situation. This page briefly introduces the Minimax algorithm and Alpha–Beta pruning.

Minimax Algorithm

Minimax, also called the minimax algorithm, is an algorithm that minimizes potential loss in the worst-case (that is, maximum-loss) situation.

Process

In a two-player zero-sum game with a determined state, adversarial search is often needed to construct a search tree in which every node is a definite state. Odd levels correspond to the first player, and even levels correspond to the opponent. Each leaf node in the search tree is assigned a score; a larger score means a greater chance of our side winning. Our side seeks a larger chance of winning, while the opponent tries to reduce our side's chance of winning. On the search tree, this means that nodes at odd levels (our nodes) always choose the child state with the greatest chance of winning, while nodes at even levels (the opponent's nodes) always choose the child state with the smallest chance of winning for our side.

The Minimax algorithm traverses the search tree from top to bottom and updates the answer with subtree information while backtracking, finally obtaining its value of the root node—the maximum score our side can obtain when both sides adopt optimal strategies.

Example

Consider a simple example.

Call our side MAX and the opponent MIN, as shown below:

Figure: minimax 1

For the following position, suppose the search proceeds from left to right, and its value of the root node represents our chance of winning:

Figure: minimax 2

We should choose the middle route. If the left route is chosen, its worst-case chance of winning is 33; if the middle route is chosen, its worst-case chance is 1515; if the right route is chosen, its worst-case chance is 11. Although the right route may have a chance of winning of 2222, a sufficiently rational opponent will leave our side with only a chance of 11. Thus, after weighing the options, the middle route is clearly better.

Figure: minimax 3

In fact, when examining the right route, once a possible chance of winning of 11 is found, there is no need to examine the branches with chances of winning of 1212, 2020, and 2222. Compared with the chances of winning on the two routes to the left, it is already certain that the right route is not the best.

The naive Minimax algorithm often needs to construct a huge search tree, making both its time and space complexity unacceptable. Alpha–Beta pruning is a method that uses the upper and lower bounds of the scores of both sides at each node in the search tree to prune and optimize Minimax.

Note that the meaning of its value at each node of the search tree differs between problems: it may be a heuristic value, a score, a probability of winning, and so on. For convenience, the following text uniformly calls it a score.

Alpha–Beta Pruning

Alpha–Beta pruning is search pruning for the Minimax algorithm.

Process

In the Minimax algorithm, if the scores of all children of a node are known, the score of that node can be calculated: a MAX node takes the maximum score, while a MIN node takes the minimum score.

When the search has reached a node but is not yet complete, its score cannot be calculated, but the range of scores that both sides can obtain among the nodes searched so far can be calculated. During the search, maintain two variables, α\alpha and β\beta, representing, among all nodes already searched when the game reaches this node, the lower and upper bounds of the scores that the Alpha player (the side seeking the maximum score) and Beta player (the side seeking the minimum score) can guarantee, respectively.

The pruning strategy of Alpha–Beta pruning depends on the values of α\alpha and β\beta when searching the current node. If the current node is a MAX node, Alpha can continue searching its children to raise the lower bound α\alpha. However, if some search results in α≥β\alpha\ge\beta, this node cannot occur in an actual game: once a node is reached, the Alpha player can guarantee a score of at least α\alpha; but the Beta player already knows a strategy (deviating from the current path) that guarantees a score no greater than β≤α\beta\le\alpha, so Beta naturally will not allow the game to develop to the current node. Similarly, if the current node is a MIN node and, after searching one of its children, β≤α\beta\le\alpha holds at that node, there is no need to continue searching its other children, because the Alpha player will not allow the game to enter the current node. In summary, when α≥β\alpha \geq \beta, the remaining branches of a node need not be searched (that is, pruning can be performed). Note that pruning is also required when α=β\alpha = \beta, because no better result is possible, although a worse result may be possible.

During the search, it is unnecessary to maintain node scores; maintaining only α\alpha and β\beta is sufficient. Initially, set α=−∞, β=+∞\alpha=-\infty,~\beta=+\infty. When searching downward, pass the information of α\alpha and β\beta along with the search to record the alternatives of the two players.

When a child has been searched, the information at the current node must be updated. Suppose the current node XX is a MAX node and its child YY has just been searched. The β\beta value at node XX does not change; only α\alpha needs to be updated with the maximum of its current value and the score of child YY. If child YY is a leaf node, directly use child YY's score to update α\alpha at XX; otherwise, only the β\beta value of YY needs to be used to update α\alpha at XX. There are three possibilities:

  1. The β\beta value of child YY lies strictly between the α\alpha and β\beta values of node XX. Since YY inherited the α\alpha value of XX and does not update it, having β>α\beta > \alpha after the search of YY is complete means that no pruning occurred while searching YY. The final β\beta value of YY equals the smaller of the β\beta value inherited from XX and the scores of all children of YY. Since this minimum is strictly less than the β\beta value of XX, it must be the minimum score among all children of YY. Therefore, as a MIN node, the score of YY is this β\beta value. It is valid to use it to update the α\alpha value of XX.
  2. The β\beta value of child YY equals the β\beta value of node XX. As described above, this means that all children of YY have scores no less than the β\beta value of XX. This further means that the Beta player will not allow the game to enter node XX: if the Alpha player chooses child YY, Beta cannot obtain a score lower than β\beta. Thus, using the β\beta value of YY to update the α\alpha value of XX makes α=β\alpha=\beta at XX, triggering the pruning condition. Its effect is the same as using the actual score at YY—a number greater than or equal to the β\beta value at XX—to update the α\alpha value of XX.
  3. The β\beta value of child YY is less than or equal to the α\alpha value of XX. In this case, YY triggered the pruning condition, so child YY's actual score is no greater than its β\beta value and certainly no greater than the α\alpha value of XX. Using the actual score of YY to update α\alpha at XX would not change α\alpha. This has the same effect as using the β\beta value of YY to update α\alpha at XX.

This analysis shows that after a child has been searched, α\alpha (or β\beta) accurately records the actual score of that child as a MAX node (or MIN node) only in the first case. In the other cases, although it may not be the exact score, it provides enough information to ensure that pruning is performed correctly and therefore does not affect the score recorded at the root.

Example

This section analyzes an example to show how the α\alpha and β\beta values at each node are updated during the search. The scores at the involved nodes are also calculated along the way. Thus, the relationship between the actual score at each node and its recorded α\alpha and β\beta values can be observed. However, note that an implementation of this algorithm does not calculate the actual scores of these nodes.

For the following position, suppose the search proceeds from left to right:

Figure: alpha beta 1

Initially, set α=−∞, β=+∞\alpha = -\infty,~\beta = +\infty, and pass this information down along the search path.

Figure: alpha beta 2

When node A is reached, because the score of its left child is 33 and A is a MIN node seeking a small score, its β\beta value is changed to 33, since 33 is less than the current β\beta value (β=+∞\beta = +\infty). The score of A's right child is then 1717, but A's β\beta value is not changed because 1717 is greater than the current β\beta value (β=3\beta = 3). At this point all children of A have been searched, so A's score can be calculated as 33, consistent with the β\beta value recorded at a node (case 1 above).

Figure: alpha beta 3

Node A is a child of node B. After calculating A's score, B's α\alpha and β\beta values can be updated. Since B is a MAX node seeking a large score, its α\alpha value is changed to 33, because the β\beta value at A (β=3\beta=3) is greater than the current α\alpha value (α=−∞\alpha = -\infty). Next, search B's right child C and pass B's α\alpha and β\beta values to C.

Figure: alpha beta 4

For node C, the score of its left child is 22 and C is a MIN node, so its β\beta value is changed to 22. Now α≥β\alpha \geq \beta, so C's remaining children need not be searched, because it can be determined that the Alpha player will not allow the game to develop to C. C is a MIN node, so its score is 22, no greater than the recorded β\beta value (case 3 above). Since all children of B have been searched, B's score can be calculated as 33, equal to its recorded α\alpha value (case 1 above).

Figure: alpha beta 5

After B's score is calculated, B is a child of D, so D's α\alpha and β\beta values can be updated. Since D is a MIN node, its β\beta value is changed to 33. D then passes α\alpha and β\beta to E, which passes them on to F. F has only one child with score 1515. Since 1515 is greater than the current β\beta value and F is a MIN node, its β\beta value is not updated. F's score can then be calculated as 1515, greater than the recorded β\beta value (case 2 above).

Figure: alpha beta 6

After F's score is calculated, F is a child of E, so E's α\alpha and β\beta values can be updated. E is a MAX node, so its α\alpha value is updated; now α≥β\alpha \geq \beta, so E's remaining branches (that is, node G) can be pruned. E is a MAX node, so set its score to 1515, strictly greater than its recorded α\alpha value (case 3 above). Use E's α\alpha value to update D's β\beta value, which remains 33. At this point all children of D have been searched, so D's score can be calculated as 33, equal to its recorded β\beta value (case 1 above).

Figure: alpha beta 7

After D's score is calculated, D is a child of H, so H's α\alpha and β\beta values can be updated. H is a MAX node, so update α\alpha. Then, in search order, pass H's α\alpha and β\beta values to I, J, and K in turn. For K, the score of its left child is 22, and K is a MIN node, so update β\beta. Now α≥β\alpha \geq \beta, and K's remaining branches can be pruned. Then set K's score to 22, less than or equal to its recorded β\beta value (case 3 above).

Figure: alpha beta 8

After K's score is calculated, K is a child of J, so J's α\alpha and β\beta values can be updated. J is a MAX node, so update α\alpha. However, because K's score is less than α\alpha, J's α\alpha value remains 33. Then pass J's α\alpha and β\beta values to L. Since L is a MIN node, update β=3\beta = 3. Now α≥β\alpha \geq \beta, so L's remaining branches can be pruned. Since L has no remaining branches, no actual pruning occurs here. Then set L's score to 33, less than or equal to its recorded β\beta value (case 3 above).

Figure: alpha beta 9

After L's score is calculated, L is a child of J, so J's α\alpha and β\beta values can be updated. J is a MAX node, so update α\alpha. However, because L's score is less than or equal to α\alpha, J's α\alpha value remains 33. At this point all children of J have been searched, so J's score can be calculated as 33, equal to its recorded α\alpha value (case 2 above).

After J's score is calculated, J is a child of I, so I's α\alpha and β\beta values can be updated. I is a MIN node, so update β\beta. Now α≥β\alpha \geq \beta, and I's remaining branches can be pruned. It is worth noting that because the right child exists, I's actual score is 22, less than the recorded β\beta value (case 3 above).

After I's score is calculated, I is a child of H, so H's α\alpha and β\beta values can be updated. H is a MAX node, so update α\alpha. However, because I's score is less than or equal to α\alpha, H's α\alpha value remains 33. At this point all children of H have been searched, so H's score can be calculated as 33, equal to its recorded α\alpha value (case 1 above).

Figure: alpha beta 10

This is the final result.

Implementation

Reference Code
int alpha_beta(int u, int alph, int beta, bool is_max) {
  if (!son_num[u]) return val[u];
  if (is_max) {
    for (int i = 0; i < son_num[u]; ++i) {
      int d = son[u][i];
      alph = max(alph, alpha_beta(d, alph, beta, !is_max));
      if (alph >= beta) break;
    }
    return alph;
  } else {
    for (int i = 0; i < son_num[u]; ++i) {
      int d = son[u][i];
      beta = min(beta, alpha_beta(d, alph, beta, !is_max));
      if (alph >= beta) break;
    }
    return beta;
  }
}

References and notes

This article partially quotes the blog post An In-Depth Explanation of the Minimax Algorithm and Alpha–Beta Pruning _Wenjianmuran, under the CC 4.0 BY-SA license. The content has been modified.

This content comes from OI Wiki: Alpha–Beta Pruning. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.