Minimax explanation for dummies
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Overview
Minimax is a decision-making algorithm that's used extensively in artificial intelligence, especially in games like chess, tic-tac-toe, and other two-player competitive games. It helps determine the best move for a player, assuming that the opponent is also playing optimally.
How Minimax Works
The Concept
The Minimax algorithm explores all possible moves in a game, forming a game tree with these moves. It assigns a value to each board configuration that represents the best possible outcome for the player whose turn it is to play. The algorithm contains two players: the "maximizing" player who tries to get the highest score, and the "minimizing" player who tries to lower the maximizing player's score.
Step-by-Step Process
- Generate the Game Tree: Enumerate all possible moves leading from the current state down to a predefined depth, or until a terminal state is reached where the game is over.
- Evaluate the Terminal States: At the terminal states, evaluate the game board using a heuristic function that assigns a score based on favorability to the maximizing player.
- Backtrack Values: Move back up the tree:
- If it's the maximizing player's turn, consider the maximum score achievable from the child nodes.
- If it's the minimizing player's turn, consider the minimum score achievable.
- Choose the Optimal Move: At the root of the tree, the maximizing player chooses the move associated with the highest score.
Example: Tic-Tac-Toe
Suppose you are playing tic-tac-toe, and the current board state is as follows:
- If is a maximizing node, .
- If is a minimizing node, .
- Optimality: Ensures the best possible outcome, provided the opponent also plays optimally.
- Predictive: Allows the player to anticipate and plan for the opponent's moves.
- Computationally Intensive: The complexity increases significantly as the depth and number of possible moves increase.
- Limited Real-world Application: Not directly useful for probabilistic environments or games with a chance component.
- Alpha-Beta Pruning: Reduces the number of nodes evaluated, accelerating the decision process without affecting the final decision.
- Iterative Deepening: Allows for deeper search by gradually exploring further under a time limit.
Related reading
- Minimize Cross Edges in a Graph
- Minimize maximum manhattan distance of a point to a set of points
- Minimize Sum of Absolute Difference of Two Arrays
- Minimize sum of distances in point pairs
- Minimizing Sum of Distances Optimization Problem
- Minimum-Waste Print Job Grouping Algorithm?
- Minimum add to make parentheses string consisting of '''', '''', '''', '''', '''', '''' valid
- Minimum area quadrilateral algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.