2. Introduction
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)
Alpha-beta pruning gets its name from two parameters. –
They describe bounds on the values that appear anywhere
along the path under consideration:
α = the value of the best (i.e., highest value) choice
found so far along the path for MAX .
β = the value of the best (i.e., lowest value) choice
found so far along the path for MIN.
4. Minimax Algorithm
Assign a initial value to each possible ending.
Assures best possible ending, assuming opponent also plays
perfectly.
opponent tries to give you worst possible ending.
Depth-first search tree traversal that updates initial values as
it recourses back up the tree.
Time complexity O(b^m)
5. Example
A
B C
GFED
-2 1 1 4 -1 0 2 6
MIN
MAX
2 3 1 5
MAX
α= -
∞
β= ∞
α= -
∞
β= ∞
α= -
∞
β= ∞
α= -
∞
β= ∞
α= -
∞
β= ∞
α= -
∞
β= ∞
α= -
∞
β= ∞
Max = α = -∞
Min = β = ∞
7. Alpha-Beta Pruning
Avoid processing subtrees that have no effect on the result
Two new parameters
• α: The best value for MAX seen so far
• β: The best value for MIN seen so far
α is used in MIN nodes, and is assigned in MAX nodes
β is used in MAX nodes, and is assigned in MIN nodes
When maximizing, cut off values lower than Alpha
When minimizing, cut off values greater than Beta
8. When to Prune
Prune whenever α ≥ β
Prune below a Max node whose alpha value becomes greater than
or equal to the beta value of its ancestors.
• Max nodes update alpha based on children’s returned
values.
Prune below a Min node whose beta value becomes less than or
equal to the alpha value of its ancestors.
• Min nodes update beta based on children’s returned values.
9. Properties of α-β
Pruning preserves completeness and optimality of original
minimax algorithm
Good move ordering improves effectiveness of pruning
With "perfect ordering," time complexity = O(b^m/2)
Therefore, doubles depth of search
10. Alpha-beta Algorithm
Depth first search
– only considers nodes along a single path from root at any time
α = highest-value choice found at any choice point of path for MAX
(initially, α = −infinity)
β = lowest-value choice found at any choice point of path for MIN
(initially, β = +infinity)
Pass current values of α and β down to child nodes during search.
Update values of α and β during search:
– MAX updates α at MAX nodes
– MIN updates β at MIN nodes
Prune remaining branches at a node when α ≥ β
11. Initial Assumption
At the start of the problem, you see only the current state. As
for upper and lower bounds, all you know is that it's a number
less than infinity and greater than negative infinity. Thus,
here's what the initial situation looks like:
≥-∞
≤ ∞
Max = α = -∞
Min = β = ∞
52. CODE
// C++ program to demonstrate working of Alpha-Beta Pruning
#include<bits/stdc++.h>
using namespace std;
//# Initial values of Aplha and Beta
const int MAX = 1000;
const int MIN = -1000;
// Returns optimal value for current player (Initially called
// for root and maximizer)
int minimax(int depth, int nodeIndex, bool maximizingPlayer,
int values[], int alpha, int beta)
{
// Terminating condition. i.e leaf node is reached
if (depth == 3)
53. return values[nodeIndex];
if (maximizingPlayer)
{
int best = MIN;
// Recur for left and right children
for (int i=0; i<2; i++)
{
int val = minimax(depth+1, nodeIndex*2+i,
false, values, alpha, beta);
best = max(best, val);
alpha = max(alpha, best);
// Alpha Beta Pruning
if (beta <= alpha)
break;
}
return best;
}
else
{
int best = MAX;
54. // Recur for left and right children
for (int i=0; i<2; i++)
{
int val = minimax(depth+1, nodeIndex*2+i,
true, values, alpha, beta);
best = min(best, val);
beta = min(beta, best);
// Alpha Beta Pruning
if (beta <= alpha)
break;
}
return best;
}
}
int main()
{
int values[8] = { 3, 5, 6, 9, 1, 2, 0, -1 };
cout << "The optimal value is : "
<< minimax(0, 0, true, values, MIN, MAX);;
return 0;
}
55. Conclusion
Minimax finds optimal play for deterministic, fully observable,
two-player games
Alpha-Beta reduction makes it faster