0% found this document useful (0 votes)
3 views

Minmax algorithm and Description

The document describes the Minimax algorithm used in two-player games, where one player (Maximizer) aims to maximize scores while the other (Minimizer) seeks to minimize them. It outlines the algorithm's workflow, including generating a game tree, applying utility functions, and backtracking to determine optimal moves. Additionally, it highlights properties such as completeness, optimality, and the time and space complexities of the algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Minmax algorithm and Description

The document describes the Minimax algorithm used in two-player games, where one player (Maximizer) aims to maximize scores while the other (Minimizer) seeks to minimize them. It outlines the algorithm's workflow, including generating a game tree, applying utility functions, and backtracking to determine optimal moves. Additionally, it highlights properties such as completeness, optimality, and the time and space complexities of the algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

import math

def minimax (curDepth, nodeIndex,

maxTurn, scores,

targetDepth):

# base case : targetDepth reached

if (curDepth == targetDepth):

return scores[nodeIndex]

if (maxTurn):

return max(minimax(curDepth + 1, nodeIndex * 2,

False, scores, targetDepth),

minimax(curDepth + 1, nodeIndex * 2 + 1,

False, scores, targetDepth))

else:

return min(minimax(curDepth + 1, nodeIndex * 2,

True, scores, targetDepth),

minimax(curDepth + 1, nodeIndex * 2 + 1,

True, scores, targetDepth))

# Driver code

scores = [3, 5, 2, 9, 12, 5, 23, 23]

treeDepth = math.log(len(scores), 2)

print("The optimal value is : ", end = "")

print(minimax(0, 0, True, scores, treeDepth))

Working of Min-Max Algorithm:


o The working of the minimax algorithm can be easily described using
an example. Below we have taken an example of game-tree which is
representing the two-player game.
o In this example, there are two players one is called Maximizer and
other is called Minimizer.
o Maximizer will try to get the Maximum possible score, and Minimizer
will try to get the minimum possible score.
o This algorithm applies DFS, so in this game-tree, we have to go all
the way through the leaves to reach the terminal nodes.
o At the terminal node, the terminal values are given so we will
compare those value and backtrack the tree until the initial state
occurs. Following are the main steps involved in solving the two-
player game tree:
Step-1: In the first step, the algorithm generates the entire game-tree
and apply the utility function to get the utility values for the terminal
states. In the below tree diagram, let's take A is the initial state of the
tree. Suppose maximizer takes first turn which has worst-case initial value
=- infinity, and minimizer will take next turn which has worst-case initial
value = +infinity.

Step 2: Now, first we find the utilities value for the Maximizer, its initial
value is -∞, so we will compare each value in terminal state with initial
value of Maximizer and determines the higher nodes values. It will find the
maximum among the all.
o For node D max(-1,- -∞) => max(-1,4)= 4
o For Node E max(2, -∞) => max(2, 6)= 6
o For Node F max(-3, -∞) => max(-3,-5) = -3
o For node G max(0, -∞) = max(0, 7) = 7

Step 3: In the next step, it's a turn for minimizer, so it will compare all
nodes value with +∞, and will find the 3 rd layer node values.

o For node B= min(4,6) = 4


o For node C= min (-3, 7) = -3
Step 4: Now it's a turn for Maximizer, and it will again choose the
maximum of all nodes value and find the maximum value for the root
node. In this game tree, there are only 4 layers, hence we reach
immediately to the root node, but in real games, there will be more than 4
layers.

o For node A max(4, -3)= 4


That was the complete workflow of the minimax two player game.

Properties of Mini-Max algorithm:


o Complete- Min-Max algorithm is Complete. It will definitely find a
solution (if exist), in the finite search tree.
o Optimal- Min-Max algorithm is optimal if both opponents are
playing optimally.
o Time complexity- As it performs DFS for the game-tree, so the
time complexity of Min-Max algorithm is O(bm), where b is branching
factor of the game-tree, and m is the maximum depth of the tree.
o Space Complexity- Space complexity of Mini-max algorithm is also
similar to DFS which is O(bm).

You might also like