Alpha Beta pruning

Learn via video courses
Topics Covered

Overview

Alpha Beta Pruning is an optimization technique of the Minimax algorithm. This algorithm solves the limitation of exponential time and space complexity in the case of the Minimax algorithm by pruning redundant branches of a game tree using its parameters Alpha(α\alpha) and Beta(β\beta).

Pre-requisites

  • Basic knowledge of space and time complexity
  • Minimax algorithm

Introduction

In AI, an agent takes various decisions in an environment to maximize its gains/rewards. Adversarial search is a paradigm wherein multiple agents are competing against each other in the same environment to maximize their gains while minimizing that of others. The Minimax algorithm is an adversarial search algorithm that involves a Depth-First-Search(DFS) on the game tree.

However, as the depth of the game tree increases, the number of states increases exponentially, leading to high time and space complexity. This is solved by the Alpha Beta Pruning algorithm, an optimization technique of the Minimax algorithm.

The following sections present the Alpha Beta Pruning algorithm details for the case when two competing agents take alternate turns. One agent is a maximizer who wants to maximize the utility, whereas the other agent is a minimizer who wants to minimize the utility. The maximizer gets the first turn. Our goal is to search for an optimal sequence of actions for the maximizer. The following concepts can be extrapolated for other multi-agent environments as well.

Interesting Fact: The literal meaning of pruning refers to the discarding of unwanted branches of a tree in gardening. Alpha Beta Pruning in AI is named as such because it involves pruning the unnecessary branches of a game tree by using two parameters, Alpha(α\alpha) and Beta(β\beta), described in the next section.

Parameters of the Alpha Beta Pruning Algorithm

  • Alpha(α\alpha): The best choice(highest utility/value) found till the current state on the path traversed by the maximizer.

  • Beta(β\beta): The best choice(lowest utility/value) found till the current state on the path traversed by the minimizer.

Salient Properties and Conditions of the Alpha Beta Pruning Algorithm

The critical points of Alpha Beta Pruning in AI are as follows.

  • The initialization of the parameters
    • Alpha(α\alpha) is initialized with -\infty.
    • Beta(β\beta) is initialized with ++\infty.
  • Updating the parameters
    • Alpha(α\alpha) is updated only by the maximizer at its turn.
    • Beta(β\beta) is updated only by the minimizer at its turn.
  • Passing the parameters
    • Alpha(α\alpha) and Beta(β\beta) are passed on to only child nodes.
    • While backtracking the game tree, the node values are passed to parent nodes.
  • Pruning Condition
    • The child sub-trees, which are not yet traversed, are pruned if the condition αβ\alpha \geq\beta holds.

In the next section, we conglomerate all these properties and conditions into the pseudo-code of Alpha Beta Pruning in AI.

Pseudocode for Alpha Beta Pruning in AI

Keeping all the pointers stated previously, we present the following pseudo-code for the Alpha Beta Pruning algorithm.

Working on Alpha Beta Pruning Algorithm

To illustrate the working of Alpha Beta Pruning in AI, we use an example game tree shown in Figure-1. The numbers shown in terminal nodes represent their respective utilities.

Figure-1.jpg
Figure-1

The Minimax algorithm would have traversed the complete game tree leading to the game tree shown in Figure-2.

Figure-2.jpg
Figure-2

Alpha Beta Pruning algorithm restricts the agent from visiting redundant sub-trees leading to faster average search time complexity. To visualize its working, we refer to the same game tree in Figure-1. We start from node N1N_1 with α=\alpha=-\infty and β=\beta=\infty. From N1N_1, we move to its child node N2N_2 with the same α\alpha and β\beta. Similarly, we move from N2N_2 to its child N4N_4, as shown in Figure-3.

Figure-3.jpg
Figure-3

From N4N_4, we move to its first terminal child and get back its utility as 11. Since N4N_4 is a maximiser, we update α=max(,1)=1\alpha=max(-\infty,1)=1. The pruning condition αβ\alpha\geq\beta remains unsatisfied. Therefore, we move to the second terminal child of N4N_4 and get its utility as 44. Further, we update α=max(1,4)=4\alpha=max(1,4)=4. After this step, we get the node value of N4N_4 as max(1,4)=4max(1,4)=4 and return it to its parent N2N_2. Since N2N_2 is a minimiser, it updates β=min(+,4)=4\beta=min(+\infty,4)=4. The game tree after this step is shown in Figure-4.

Figure-4.jpg
Figure-4

At N2N_2, the pruning condition αβ\alpha\geq\beta is still unsatisfied. Therefore, we visit its next child node N5N_5 with α=\alpha=-\infty and β=4\beta=4. At N5N_5, we get the utility of its first terminal child as 77. Since N5N_5 is a maximiser, we update α=max(,7)=7\alpha=max(-\infty,7)=7. Now, the pruning condition gets satisfied (74)(7\geq4), leading to the pruning of the other child node. After that, the node utility of N5=7N_5=7 is further sent back to the parent node N2N_2. N2N_2 updates β=min(4,7)=4\beta=min(4,7)=4. Further, the node value of N2=min(4,7)=4N_2=min(4,7)=4 is returned to its parent N1N_1. At N1N_1, we update α=max(,4)=4\alpha=max(-\infty,4)=4. After this step, the game tree looks like Figure-5.

Figure-5.jpg
Figure-5

The pruning condition is still unsatisfactory at N1N_1. Therefore, we continue towards the next child N3N_3, with the same values of α\alpha and β\beta. Similarly, we propagate from N3N_3 to its first child N6N_6. At N6N_6, we get back the utility of its first terminal child as 33 and update α=max(4,3)=4\alpha=max(4,3)=4. Since the pruning condition is still unsatisfied, we get the utility of the second terminal child as 00 and update α=max(4,0)=4\alpha=max(4,0)=4. After this, the node value of N6=max(3,0)=3N_6=max(3,0)=3, is returned to the parent N3N_3. Since N3N_3 is a minimiser, we update β=min(+,3)=3\beta=min(+\infty,3)=3. Now, the pruning condition gets satisfied (43)(4\geq3). Therefore, the other child sub-tree(rooted at N7N_7) of N3N_3 is pruned, and the node value of N3N_3 becomes 33, which is returned to parent N1N_1. At N1N_1, we again update α=max(4,3)=4\alpha=max(4,3)=4 and the final utility of N1N_1 becomes 44. All these steps are shown in Figure-6.

Figure-6.jpg
Figure-6

Figure-6 depicts the final game tree of the Alpha Beta Pruning algorithm. As evident from the figure, Alpha Beta Pruning in AI yields the same result as the Minimax algorithm by visiting fewer states.

Move Ordering in Alpha Beta pruning in AI

The performance of the Alpha Beta Pruning algorithm is highly dependent on the order in which the nodes are traversed. For instance,

Case-1(Worst Performance): If the best sequence of actions is on the right of the game tree, then there will be no pruning, and all the nodes will be traversed, leading to even worse performance than the Minimax algorithm because of the overhead of computing α\alpha and β\beta. An example game tree can be seen in Figure-7.

Figure-7.jpg
Figure-7

Case-2(Best Performance): If the best sequence of actions is on the left of the game tree, there will be a lot of pruning, and only about half of the nodes will be traversed to get the optimal result. An example game tree can be seen in Figure-8.

Figure-8.jpg
Figure-8

Rules to Find Good Ordering

  • The best move occurs from the shallowest node.
  • The game tree is ordered so that the best nodes are checked first.
  • Domain knowledge is used to find the best move.
  • Memoization is applied to prevent recomputation in case of repeating states.

Conclusion

In this article, we learned about

  • Significance of Alpha Beta Pruning algorithm
  • Parameters of Alpha Beta Pruning in AI
  • Properties and Conditions of Alpha Beta Pruning in AI
  • Pseudo-code of Alpha Beta Pruning algorithm
  • Working on Alpha Beta Pruning in AI
  • Significance of order of nodes for Alpha Beta Pruning algorithm and rules to decide good orders