|
| 1 | +--- |
| 2 | +title: "Minimax Algorithm" |
| 3 | +date: "2025-10-07" |
| 4 | +tags: [] |
| 5 | +related: [] |
| 6 | +slug: "minimax-algorithm" |
| 7 | +category: "algo" |
| 8 | +--- |
| 9 | + |
| 10 | +# Minimax Algorithm |
| 11 | + |
| 12 | +## Summary |
| 13 | +Minimax is a decision-making algorithm for two-player turn-based games that explores all possible future moves, assuming both players play optimally, with the maximizing player trying to maximize the score and the minimizing player trying to minimize it. |
| 14 | + |
| 15 | +## Details |
| 16 | +### 🧠 What is Minimax? |
| 17 | + |
| 18 | +Minimax is a decision-making algorithm used in two-player, turn-based games. It assumes both players play optimally: |
| 19 | +- The maximizing player tries to maximize the score. |
| 20 | +- The minimizing player tries to minimize the score. |
| 21 | + |
| 22 | +It works by exploring all possible future moves and backpropagating values from terminal states. |
| 23 | + |
| 24 | +### 🔄 How It Works |
| 25 | + |
| 26 | +The algorithm recursively explores: |
| 27 | +1. All possible moves |
| 28 | +2. At each level, switches perspective between max and min player |
| 29 | +3. Returns the optimal value up the call stack |
| 30 | + |
| 31 | +### 🧪 Optimizations |
| 32 | +- **Alpha-Beta Pruning:** Cuts off branches that don't influence the final decision. |
| 33 | +- **Memoization / DP:** Avoids recomputation of the same state. |
| 34 | +- **Transposition Tables:** Cache evaluated board positions. |
| 35 | + |
| 36 | +### ✅ When to Use Minimax |
| 37 | + |
| 38 | +Use Minimax when the problem: |
| 39 | +- Involves two players taking turns |
| 40 | +- Requires determining if the first player can guarantee a win |
| 41 | +- Has a small state space or constraints (e.g., n <= 20) |
| 42 | +- Benefits from a game tree analysis (with recursion + memoization) |
| 43 | + |
| 44 | +### 📂 LeetCode Problems Using Minimax |
| 45 | + |
| 46 | +- **294. Flip Game II:** Determine if the starting player can guarantee a win |
| 47 | +- **464. Can I Win:** Use memoized minimax to check if first player can win |
| 48 | +- **877. Stone Game:** Two players taking optimal moves |
| 49 | +- **913. Cat and Mouse:** Graph game with alternating turns |
| 50 | +- **486. Predict the Winner:** Classic turn-based minimax |
| 51 | +- **1406. Stone Game III:** Advanced version with more choices and scoring |
| 52 | + |
| 53 | +## Examples / snippets |
| 54 | + |
| 55 | +### Basic Minimax Implementation |
| 56 | +```python |
| 57 | +def minimax(state, depth, is_maximizing): |
| 58 | + if game_over(state) or depth == 0: |
| 59 | + return evaluate(state) |
| 60 | + |
| 61 | + if is_maximizing: |
| 62 | + max_val = float('-inf') |
| 63 | + for move in get_moves(state): |
| 64 | + val = minimax(apply_move(state, move), depth - 1, False) |
| 65 | + max_val = max(max_val, val) |
| 66 | + return max_val |
| 67 | + else: |
| 68 | + min_val = float('inf') |
| 69 | + for move in get_moves(state): |
| 70 | + val = minimax(apply_move(state, move), depth - 1, True) |
| 71 | + min_val = min(min_val, val) |
| 72 | + return min_val |
| 73 | +``` |
| 74 | + |
| 75 | +### Minimax with Memoization |
| 76 | +```python |
| 77 | +from functools import lru_cache |
| 78 | + |
| 79 | +@lru_cache(maxsize=None) |
| 80 | +def minimax_memo(state, depth, is_maximizing): |
| 81 | + if game_over(state) or depth == 0: |
| 82 | + return evaluate(state) |
| 83 | + |
| 84 | + if is_maximizing: |
| 85 | + max_val = float('-inf') |
| 86 | + for move in get_moves(state): |
| 87 | + new_state = apply_move(state, move) |
| 88 | + val = minimax_memo(new_state, depth - 1, False) |
| 89 | + max_val = max(max_val, val) |
| 90 | + return max_val |
| 91 | + else: |
| 92 | + min_val = float('inf') |
| 93 | + for move in get_moves(state): |
| 94 | + new_state = apply_move(state, move) |
| 95 | + val = minimax_memo(new_state, depth - 1, True) |
| 96 | + min_val = min(min_val, val) |
| 97 | + return min_val |
| 98 | +``` |
| 99 | + |
| 100 | +### Alpha-Beta Pruning Implementation |
| 101 | +```python |
| 102 | +def minimax_alpha_beta(state, depth, alpha, beta, is_maximizing): |
| 103 | + if game_over(state) or depth == 0: |
| 104 | + return evaluate(state) |
| 105 | + |
| 106 | + if is_maximizing: |
| 107 | + max_val = float('-inf') |
| 108 | + for move in get_moves(state): |
| 109 | + new_state = apply_move(state, move) |
| 110 | + val = minimax_alpha_beta(new_state, depth - 1, alpha, beta, False) |
| 111 | + max_val = max(max_val, val) |
| 112 | + alpha = max(alpha, val) |
| 113 | + if beta <= alpha: |
| 114 | + break # Beta cutoff |
| 115 | + return max_val |
| 116 | + else: |
| 117 | + min_val = float('inf') |
| 118 | + for move in get_moves(state): |
| 119 | + new_state = apply_move(state, move) |
| 120 | + val = minimax_alpha_beta(new_state, depth - 1, alpha, beta, True) |
| 121 | + min_val = min(min_val, val) |
| 122 | + beta = min(beta, val) |
| 123 | + if beta <= alpha: |
| 124 | + break # Alpha cutoff |
| 125 | + return min_val |
| 126 | +``` |
| 127 | + |
| 128 | +### Example Usage for Game State Evaluation |
| 129 | +```python |
| 130 | +# Example for a simple game like Tic-Tac-Toe or Connect Four |
| 131 | +def can_first_player_win(initial_state): |
| 132 | + # Returns True if first player can force a win |
| 133 | + result = minimax_alpha_beta(initial_state, max_depth, float('-inf'), float('inf'), True) |
| 134 | + return result > 0 # Assuming positive values favor first player |
| 135 | + |
| 136 | +# Helper functions (to be implemented based on specific game) |
| 137 | +def game_over(state): |
| 138 | + # Return True if game is finished |
| 139 | + pass |
| 140 | + |
| 141 | +def evaluate(state): |
| 142 | + # Return a score for the state (positive if first player advantage) |
| 143 | + pass |
| 144 | + |
| 145 | +def get_moves(state): |
| 146 | + # Return list of possible moves from current state |
| 147 | + pass |
| 148 | + |
| 149 | +def apply_move(state, move): |
| 150 | + # Return new state after applying move |
| 151 | + pass |
| 152 | +``` |
| 153 | +``` |
0 commit comments