Algorithms · Visualized

Game: Minimax Tree

Game Theory & Minimax

Two-player games — who wins with perfect play, and by how much.

An optimization of plain Minimax that skips exploring branches that can't possibly affect the final decision. Time: O(b^(3d/4)) average, O(b^d) — same as plain Minimax if pruning never triggers (worst move ordering) worst; space: O(d) — recursion depth.

Big-O: Winning positions, DP over states, and minimax evaluation.

Minimax with Alpha-Beta Pruning

Game Theory / Minimax

An optimization of plain Minimax that skips exploring branches that can't possibly affect the final decision. It tracks 'alpha' (the best score the maximizer is guaranteed so far) and 'beta' (the best score the minimizer is guaranteed so far); if a branch is found where the minimizer's option is already worse than something the maximizer already has elsewhere, that branch can be safely skipped ('pruned').

How Minimax Tree Works

  1. Run Minimax as usual, but carry two extra values through the recursion: alpha (the best score found so far for the maximizer, starting at -infinity) and beta (the best score found so far for the minimizer, starting at +infinity).
  2. At a maximizing node, update alpha with the best score found among children explored so far.
  3. At a minimizing node, update beta with the best (lowest) score found among children explored so far.
  4. After evaluating each child, check if alpha ≥ beta — if so, stop exploring the remaining children of this node immediately ('prune' them), since the opponent would never let the game reach this branch anyway.
  5. Propagate the final best score up through the recursion, just like plain Minimax, but having skipped many unnecessary branches.

Minimax Tree Worked Example

Input: a game tree where an early branch already guarantees the maximizer a score of 5

While exploring a minimizing node's second child, the algorithm finds a score of 3 there. Since alpha is already 5 (guaranteed elsewhere) and this branch's beta has now dropped to 3 (alpha ≥ beta, i.e. 5≥3), the maximizer would never choose to enter this branch in the first place — so the remaining children of this minimizing node are skipped entirely, saving significant computation without changing the final decision.

Python · Game Theory Algorithms in Code
def minimax(scores, depth, is_max, alpha, beta):
    if depth == 0 or len(scores) == 0:
        return scores[0] if scores else 0
    mid = len(scores) // 2
    if is_max:
        best = float('-inf')
        for child in (scores[:mid], scores[mid:]):
            v = minimax(child, depth - 1, False, alpha, beta)
            best = max(best, v)
            alpha = max(alpha, best)
            if beta <= alpha:
                break    # prune
        return best
    else:
        best = float('inf')
        for child in (scores[:mid], scores[mid:]):
            v = minimax(child, depth - 1, True, alpha, beta)
            best = min(best, v)
            beta = min(beta, best)
            if beta <= alpha:
                break    # prune
        return best

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Minimax Tree Time & Space Complexity

BestO(b^(d/2)) — with optimal move ordering, effectively doubling the searchable depth
AverageO(b^(3d/4))
WorstO(b^d) — same as plain Minimax if pruning never triggers (worst move ordering)
SpaceO(d) — recursion depth

When to Use Minimax Tree

  • Powers real game AI for Chess, Checkers, Connect Four, and other turn-based games, where plain Minimax would be far too slow to search deep enough.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.