Minimax Algorithm in Game Theory | Set 4 (AlphaBeta Pruning) (original) (raw)

Last Updated : 16 Jan, 2023

Prerequisites: Minimax Algorithm in Game Theory, Evaluation Function in Game Theory
Alpha-Beta pruning is not actually a new algorithm, but rather an optimization technique for the minimax algorithm. It reduces the computation time by a huge factor. This allows us to search much faster and even go into deeper levels in the game tree. It cuts off branches in the game tree which need not be searched because there already exists a better move available. It is called Alpha-Beta pruning because it passes 2 extra parameters in the minimax function, namely alpha and beta.

Let's define the parameters alpha and beta.

Alpha is the best value that the maximizer currently can guarantee at that level or above.
Beta is the best value that the minimizer currently can guarantee at that level or below.

Pseudocode :

function minimax(node, depth, isMaximizingPlayer, alpha, beta):

**if** node is a leaf node :
    **return** value of the node

**if** isMaximizingPlayer :
    bestVal = -INFINITY 
    **for each** child node :
        value = minimax(node, depth+1, false, alpha, beta)
        bestVal = max( bestVal, value) 
        alpha = max( alpha, bestVal)
        **if** beta <= alpha:
            **break**
    **return** bestVal

**else** :
    bestVal = +INFINITY 
    **for each** child node :
        value = minimax(node, depth+1, true, alpha, beta)
        bestVal = min( bestVal, value) 
        beta = min( beta, bestVal)
        **if** beta <= alpha:
            **break**
    **return** bestVal

// Calling the function for the first time. minimax(0, 0, true, -INFINITY, +INFINITY)

Let's make the above algorithm clear with an example.

Alpha Beta Pruning

So far this is how our game tree looks. The 9 is crossed out because it was never computed.

Alpha Beta Pruning 2

This is how our final game tree looks like. As you can see G has been crossed out as it was never computed.

Alpha Beta Pruning 3

CPP `

// C++ program to demonstrate // working of Alpha-Beta Pruning #include<bits/stdc++.h> using namespace std;

// Initial values of // Alpha 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)
    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;

    // 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;
}

}

// Driver Code 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; }

Java

// Java program to demonstrate // working of Alpha-Beta Pruning import java.io.*;

class GFG {

// Initial values of // Alpha and Beta static int MAX = 1000; static int MIN = -1000;

// Returns optimal value for // current player (Initially called // for root and maximizer) static int minimax(int depth, int nodeIndex, Boolean maximizingPlayer, int values[], int alpha, int beta) { // Terminating condition. i.e // leaf node is reached if (depth == 3) 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 = Math.max(best, val);
        alpha = Math.max(alpha, best);

        // Alpha Beta Pruning
        if (beta <= alpha)
            break;
    }
    return best;
}
else
{
    int best = MAX;

    // 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 = Math.min(best, val);
        beta = Math.min(beta, best);

        // Alpha Beta Pruning
        if (beta <= alpha)
            break;
    }
    return best;
}

}

// Driver Code
public static void main (String[] args)
{
    
    int values[] = {3, 5, 6, 9, 1, 2, 0, -1};
    System.out.println("The optimal value is : " +
                        minimax(0, 0, true, values, MIN, MAX));

}

}

// This code is contributed by vt_m.

Python3

Python3 program to demonstrate

working of Alpha-Beta Pruning

Initial values of Alpha and Beta

MAX, MIN = 1000, -1000

Returns optimal value for current player

#(Initially called for root and maximizer) def minimax(depth, nodeIndex, maximizingPlayer, values, alpha, beta):

# Terminating condition. i.e 
# leaf node is reached 
if depth == 3: 
    return values[nodeIndex] 

if maximizingPlayer: 
 
    best = MIN 

    # Recur for left and right children 
    for i in range(0, 2): 
        
        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:
    best = MAX 

    # Recur for left and 
    # right children 
    for i in range(0, 2): 
     
        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 
 

Driver Code

if name == "main":

values = [3, 5, 6, 9, 1, 2, 0, -1]  
print("The optimal value is :", minimax(0, 0, True, values, MIN, MAX)) 

This code is contributed by Rituraj Jain

C#

// C# program to demonstrate // working of Alpha-Beta Pruning using System;

class GFG {

// Initial values of // Alpha and Beta static int MAX = 1000; static int MIN = -1000;

// Returns optimal value for // current player (Initially called // for root and maximizer) static int minimax(int depth, int nodeIndex, Boolean maximizingPlayer, int []values, int alpha, int beta) { // Terminating condition. i.e // leaf node is reached if (depth == 3) 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 = Math.Max(best, val);
        alpha = Math.Max(alpha, best);

        // Alpha Beta Pruning
        if (beta <= alpha)
            break;
    }
    return best;
}
else
{
    int best = MAX;

    // 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 = Math.Min(best, val);
        beta = Math.Min(beta, best);

        // Alpha Beta Pruning
        if (beta <= alpha)
            break;
    }
    return best;
}

}

// Driver Code public static void Main (String[] args) {

int []values = {3, 5, 6, 9, 1, 2, 0, -1};
Console.WriteLine("The optimal value is : " +
                    minimax(0, 0, true, values, MIN, MAX));

} }

// This code is contributed by 29AjayKumar

JavaScript

`

Output

The optimal value is : 5