Skip to content

    Minimax & Alpha-Beta Pruning

    AI · Intermediate

    Definition

    Minimax picks the best move assuming the opponent plays optimally; alpha-beta skips branches that can't matter.

    Classic game AI for tic-tac-toe and chess.

    Diagram

    MAXMIN MINleaves 3 5 2 9

    Flowchart

    Build game treeMax player picks maxMin player picks minPrune when alpha ≥ beta

    Example: Minimax on a tree

    def mm(n, is_max):
        if isinstance(n, int): return n
        vals = [mm(c, not is_max) for c in n]
        return max(vals) if is_max else min(vals)
    print(mm([[3, 5], [2, 9]], True))

    Output: 3

    Complexity

    O(b^d) · alpha-beta best case O(b^(d/2))

    Key points

    • Good move ordering improves pruning
    • Depth limits + evaluation function in practice

    Interview questions

    Does pruning change the answer?

    No, it only skips branches that cannot affect the result.

    More AI topics