0% found this document useful (0 votes)
63 views6 pages

Sathaye College: Practical No: 7

The document describes two search optimization algorithms: Mini-Max and Alpha Beta Pruning. Mini-Max is a recursive algorithm that searches a game tree to find the optimal move for a player, maximizing or minimizing the score at each level. Alpha Beta Pruning improves on Mini-Max by pruning branches that cannot affect the final output. Pseudocode and sample code is provided to implement both algorithms to find the optimal value in a game tree given input scores or values.

Uploaded by

Bhupendra Mish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views6 pages

Sathaye College: Practical No: 7

The document describes two search optimization algorithms: Mini-Max and Alpha Beta Pruning. Mini-Max is a recursive algorithm that searches a game tree to find the optimal move for a player, maximizing or minimizing the score at each level. Alpha Beta Pruning improves on Mini-Max by pruning branches that cannot affect the final output. Pseudocode and sample code is provided to implement both algorithms to find the optimal value in a game tree given input scores or values.

Uploaded by

Bhupendra Mish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SATHAYE COLLEGE

Intelligent Systems M.Sc.IT Part II SEM IV

Practical No: 7

Aim: Optimize the search strategy for the suggested problem using:

i.Mini-Max Algorithm.

ii Alpha Beta Pruning.

i.Mini-Max Algorithm
CODE:

import java.io.*;

class MinMax

static int minimax(int depth, int nodeIndex, boolean isMax,int scores[], int h)

// Terminating condition. i.e leaf node is reached

if (depth == h)

return scores[nodeIndex];

// If current move is maximizer, find the maximum attainable value


if (isMax)

return Math.max(minimax(depth+1, nodeIndex*2, false, scores,


h),minimax(depth+1, nodeIndex*2 + 1, false, scores, h));

// Else (If current move is Minimizer), find the minimum attainable


value

else

return Math.min(minimax(depth+1, nodeIndex*2, true, scores,


h),minimax(depth+1, nodeIndex*2 + 1, true, scores, h));

}
// A utility function to find Log n in base 2

BHUPENDRA MISHRA
SATHAYE COLLEGE
Intelligent Systems M.Sc.IT Part II SEM IV

static int log2(int n)

return (n==1)? 0 : 1 + log2(n/2);

// Driver code

public static void main (String[] args) throws Exception

// The number of elements in scores must be a power of 2.

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

System.out.print("Enter the number of values : ");

int no = Integer.parseInt(br.readLine());

int scores[] = new int[no];

System.out.println("Enter the values : ");

for(int i=0;i<no;i++)

scores[i] = Integer.parseInt(br.readLine());

//int scores[] = {3, 5, 2, 9, 12, 5, 23, 23};

int n = scores.length;

int h = log2(n);

int res = minimax(0, 0, true, scores, h);

System.out.println( "The optimal value is : " +res);

BHUPENDRA MISHRA
SATHAYE COLLEGE
Intelligent Systems M.Sc.IT Part II SEM IV

OUTPUT :

BHUPENDRA MISHRA
SATHAYE COLLEGE
Intelligent Systems M.Sc.IT Part II SEM IV

ii Alpha Beta Pruning.

CODE :
import java.io.*;

class AlphaBeta

static int MAX = 1000;

static int MIN = -1000;

static int minimax(int depth, int nodeIndex, Boolean maximizingPlayer,int values[], int alpha, int
beta)

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);

if (beta <= alpha)

break;

return best;

BHUPENDRA MISHRA
SATHAYE COLLEGE
Intelligent Systems M.Sc.IT Part II SEM IV

else

int best = MAX;

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) throws Exception

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

System.out.print("Enter the number of values : ");

int no = Integer.parseInt(br.readLine());

int values[] = new int[no];

System.out.println("Enter the values : ");

for(int i=0;i<no;i++)

BHUPENDRA MISHRA
SATHAYE COLLEGE
Intelligent Systems M.Sc.IT Part II SEM IV

values[i] = Integer.parseInt(br.readLine());

//int values[] = { 3, 5, 6, 9, 1, 2, 0, -1 };

System.out.println("The optimal value is : " + minimax(0, 0, true, values,


MIN, MAX));

OUTPUT :

BHUPENDRA MISHRA

You might also like