Alpha-Beta Pruning in Artificial Intelligence
Learn how Alpha-Beta Pruning improves the Minimax algorithm by eliminating unnecessary branches in a game tree. Understand alpha and beta values, the pruning condition, a worked example, and a Python implementation.
Imagine playing chess against a computer. You make a move, and the computer begins considering its possible responses. For every response, it considers your next move, then its own next move, and so on.
The number of possibilities grows quickly. Even a simple game can produce an enormous number of possible move sequences.
Does the computer really need to examine every possibility before choosing its move?
Not always.
There is a technique that allows an AI system to ignore certain branches of a game tree when it can prove that exploring them will not change the final decision. This technique is called Alpha-Beta Pruning.
It makes the Minimax algorithm more efficient without changing the decision it produces.
Let’s understand how it works.
1. What Is Alpha-Beta Pruning?
Alpha-Beta Pruning is an optimisation technique used with the Minimax algorithm in two-player, zero-sum games such as chess, checkers, and tic-tac-toe.
Its main purpose is to reduce the number of nodes that Minimax needs to examine.
In Minimax, the AI explores possible moves and assumes that both players make the best decisions available to them. However, while exploring the game tree, it may discover that a particular branch cannot possibly affect the final decision.
When that happens, it stops exploring that branch.
This process is called pruning.
The important point is that Alpha-Beta Pruning does not change the final result of Minimax. It simply avoids unnecessary calculations.
2. Understanding Alpha and Beta
Alpha-Beta Pruning uses two values to keep track of the best decisions found so far.
Alpha (α)
Alpha represents the best value that the MAX player can guarantee so far.
MAX tries to maximise the score, so alpha keeps track of the highest value MAX can secure.
Initially:
\[
\alpha = -\infty
\]
Beta (β)
Beta represents the best value that the MIN player can guarantee so far.
MIN tries to minimise the score, so beta keeps track of the lowest value MIN can secure.
Initially:
\[
\beta = +\infty
\]
As the algorithm explores the game tree, these values are updated.
The pruning condition is:
\[
\boxed{\alpha \geq \beta}
\]
When this condition becomes true, the remaining branches at that point do not need to be explored.
Why? Because one player already has a better option elsewhere, making the remaining possibilities irrelevant to the final decision.
3. How Does Alpha-Beta Pruning Work?
Let’s understand the process with a simple game tree.
Suppose MAX is at the root and has two possible choices: A and B. Each choice leads to a MIN player who must select the smaller value.
Consider this tree:
MAX
/ \
A B
/ \ / \
3 5 2 9
We will explore the tree from left to right.
Step 1: Explore the left branch
The MAX player first explores A.
A is a MIN node, so it selects the smaller value from its two children:
\[
A = \min(3,5)=3
\]
MAX now knows that it can secure a value of at least 3.
Therefore:
\[
\alpha = 3
\]
Step 2: Explore the right branch
Now the algorithm explores B.
B is also a MIN node. Its first child has a value of 2.
Therefore:
\[
\beta = 2
\]
Now compare alpha and beta:
\[
\alpha \geq \beta
\]
\[
3 \geq 2
\]
The condition is true.
Step 3: Prune the remaining branch
The MIN player at B can already choose 2. MAX has already found another option, A, that guarantees 3.
MAX will never choose B because MIN can force its value down to 2 or less.
Therefore, there is no need to examine the remaining child of B, which has value 9.
That branch is pruned.
Final decision
MAX compares the available outcomes:
- A gives a value of 3.
- B can give a value of 2 or less.
So MAX chooses A.
The final result is 3, exactly as it would be with ordinary Minimax.
The difference is that Alpha-Beta Pruning reached the same decision without examining every leaf.
4. The Alpha-Beta Pruning Algorithm
The algorithm works by exploring the game tree recursively while carrying two values: alpha and beta.
Its main steps are:
- Start at the root node.
- Set alpha to negative infinity and beta to positive infinity.
- If the current node is a MAX node, explore its children and keep the highest value.
- If it is a MIN node, explore its children and keep the lowest value.
- Update alpha at MAX nodes and beta at MIN nodes.
- If alpha becomes greater than or equal to beta, stop exploring the remaining children of that node.
- Return the best value found.
The process continues until the algorithm reaches terminal states or the chosen search depth.
5. Python Implementation of Alpha-Beta Pruning
Let’s implement the algorithm using Python.
We will use the same game tree from our earlier example.
def alpha_beta(node, depth, alpha, beta, maximizing):
# If we reach a leaf node
if not isinstance(node, list):
return node
if maximizing:
best = float('-inf')
for child in node:
value = alpha_beta(
child, depth + 1,
alpha, beta, False
)
best = max(best, value)
alpha = max(alpha, best)
# Pruning condition
if alpha >= beta:
break
return best
else:
best = float('inf')
for child in node:
value = alpha_beta(
child, depth + 1,
alpha, beta, True
)
best = min(best, value)
beta = min(beta, best)
# Pruning condition
if alpha >= beta:
break
return best
# Game tree
tree = [
[3, 5],
[2, 9]
]
result = alpha_beta(
tree,
0,
float('-inf'),
float('inf'),
True
)
print("Best value:", result)
Output:
Best value: 3
The algorithm returns 3, which is the same result produced by Minimax.
Notice the condition:
if alpha >= beta:
break
This is the pruning step. Once the condition is satisfied, the algorithm stops exploring the remaining children of that node.
6. Alpha-Beta Pruning vs Minimax
Alpha-Beta Pruning is not a replacement for Minimax. It is an improvement that makes Minimax more efficient.
| Feature | Minimax | Alpha-Beta Pruning |
|---|---|---|
| Main purpose | Find the best move | Find the best move more efficiently |
| Explores game tree | Explores all nodes within the search | Skips unnecessary branches |
| Final decision | Optimal under its assumptions | Same decision as Minimax |
| Efficiency | Can require many calculations | Often requires fewer calculations |
| Extra values | No alpha-beta bounds | Uses alpha and beta |
Both algorithms rely on the assumption that the two players make the best possible decisions.
7. Advantages of Alpha-Beta Pruning
1. Reduces unnecessary calculations
The algorithm avoids exploring branches that cannot influence the final decision.
2. Makes deeper searches possible
By examining fewer nodes, the AI can sometimes explore more levels of the game tree within the same computational budget.
3. Preserves the Minimax result
When used correctly with the same search depth and evaluation rules, it produces the same decision as Minimax.
4. Useful in competitive games
It is especially valuable in games where the number of possible moves grows rapidly.
8. Limitations of Alpha-Beta Pruning
Despite its advantages, Alpha-Beta Pruning has some limitations.
- Move ordering matters: The order in which moves are explored affects how many branches can be pruned.
- Large game trees remain challenging: Even with pruning, complex games can have enormous search spaces.
- Evaluation functions are still needed: When the search cannot reach the end of a game, the AI still needs an evaluation function to estimate the position.
- Does not improve the underlying evaluation: It reduces computation, but it does not make an inaccurate evaluation function more accurate.
9. Time Complexity of Alpha-Beta Pruning
The time complexity of Minimax is commonly expressed as:
\[
O(b^d)
\]
Where:
- \(b\) = average branching factor.
- \(d\) = maximum search depth.
Alpha-Beta Pruning can significantly reduce the number of nodes explored.
Its performance depends heavily on move ordering.
- Worst case: \(O(b^d)\), when pruning provides little or no benefit.
- Best case: \(O(b^{d/2})\), with ideal move ordering.
This best-case improvement is important. It means that, under ideal conditions, Alpha-Beta Pruning can search roughly twice as deep as Minimax using a comparable number of node evaluations.
However, this is a best-case result, not a guarantee for every game tree.
10. Applications of Alpha-Beta Pruning
Alpha-Beta Pruning is useful wherever an AI system needs to make decisions in competitive environments.
Some examples include:
- Chess: Exploring possible moves and opponent responses while skipping irrelevant branches.
- Checkers: Reducing the number of game positions that need evaluation.
- Tic-Tac-Toe: Making the search more efficient, even though the game tree is relatively small.
- Other turn-based games: Supporting decision-making in games that use Minimax-style search.
It is particularly useful when the search tree is large and exploring every possible move would be expensive.
Conclusion
The Minimax algorithm helps an AI system choose the best move by considering the decisions of both players. But as the number of possible moves increases, exploring every branch becomes computationally expensive.
Alpha-Beta Pruning solves part of this problem.
It keeps track of alpha and beta values, identifies branches that cannot affect the final decision, and stops exploring them.
The result is simple but powerful: the AI can make the same decision as Minimax while examining fewer possibilities.
In Artificial Intelligence, efficiency is not always about doing more calculations. Sometimes, it is about knowing which calculations are unnecessary.
Next Article: Adversarial Search and Optimal Decisions
Quick quiz
5 questionsOriginally published at farhanpaal.com — https://farhanpaal.com/articles/alpha-beta-pruning-in-artificial-intelligence/
Keep a copy
Read offline as a printout or PDF.
farhanpaal.com