Skip to the workspace
Pebellslearn by building
Hard Problems & Adversarial Search  ›  Lesson

Minimax & Alpha-Beta

Minimax & Alpha-Beta 34 minSearch a tree of moves and counter-moves; prune a whole subtree the instant it can no longer change the outcome

dsa / Minimax & Alpha-Beta

Explore each scene at your own pace.

01 · Why alpha ≥ beta ends the search early

Why alpha ≥ beta ends the search early

Minimax & Alpha-Beta

Definition
Minimax is a search over a game tree in which one player maximises the outcome and the opponent minimises it; alpha-beta pruning skips branches that cannot change the result, returning the same value while examining fewer positions.
In plain words
Plan your move by imagining your rival's best reply, then your best answer to that, and so on. Once you see that a branch is already worse than an option you have, you stop exploring it, and your choice stays the same.

In short: when a competitor is also optimising, searching their replies is a tree: Meridian's bid desk picks a move to maximise the outcome, the rival picks their reply to minimise it, alternating all the way down to the leaves. Plain minimax visits every leaf. Alpha-beta pruning keeps two running numbers — alpha, the best the maximiser can already guarantee, and beta, the best the minimiser can already guarantee — and the moment alpha >= beta, the rest of the current node's children can be skipped entirely: nothing left in that subtree could ever change the parent's decision. The value found is always identical to plain minimax. Only the amount of tree actually looked at changes, and how the leaves are ordered changes that amount a lot.

A MAX root over three MIN nodes with leaves 3-5-2, 9-1-0 and 7-4-8; the middle node's third leaf (0) is pruned with a dashed line and a red "pruned" tag, next to an (α=2, β=1) badge showing why

Why alpha ≥ beta ends the search early

At a MIN node, beta only ever falls as better (lower) replies are found. The moment beta drops to or below the alpha the MAX parent already has in hand from an earlier sibling, the parent already has something at least as good available elsewhere — so this MIN node can never be the parent's actual choice, no matter what its remaining children turn out to be:

watch it move · tree

MAX at the root, MIN at L and R, MAX again at LL/LR/RL/RR. Alpha starts at the value the root already has in hand from an earlier branch; the moment it meets or beats beta, the rest of that branch is skipped -- here, all of RR.

LL (a MAX node over leaves 3 and 5) settles at 5. Binary tree, 15 nodes, height 4. Settled: LL.

1 / 6
Scene 1 of 6
Project context and further resources
You're building a piece ofMeridian — the bid desk
This piece — best_counter_bid(): Searches a tree of bid and counter-bid moves against a competitor who is also trying to win, pruning branches that can't change the outcome.
Scenario A rival bidder is also optimising, move for move, against Meridian's own bids on a contract. The bid desk needs the value of the position both sides will settle on -- without wasting time exploring branches that can't possibly change the outcome.
best_counter_bid.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
best_counter_bid(tree) → dictThe minimax value Meridian can guarantee, how many leaves alpha-beta had to look at, how many it pruned, and the tree's total leaf count.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.