Algorithms · For everyone
The alpha-beta algorithm: how a computer wins without checking everything
When you play chess or checkers against a computer, it tries to predict the future: "If I make this move, what will you do?" But there are so many possible moves that it can't check them all in time. Alpha-beta is a clever trick that lets the computer skip lots of moves and still pick the best one. Let's see how it works with a game of candy.
The candy box game
There are three boxes on the table: A, B and C. Each box holds three bags of candy. Every bag has a different number of candies, but the bags are closed, so you can't see how many.
- 1
First, you pick one box.
- 2
Then a robot looks inside that box and gives you one bag. The robot is cheeky: it always gives you the bag with the fewest candies.
Which box should you pick to get as many candies as possible? Opening bags takes time, so we want to find the answer while opening as few as we can. It's the same for a computer: every move it checks costs a bit of its time.
Way one: open everything
The simplest way is to look inside every bag. The robot always gives you the smallest bag, so box A is worth 3 candies, box B only 2, and box C also 2. Box A is the best: you'll get 3 candies from it.
That works, but we had to open all 9 bags. This method is called minimax: you look for the biggest number (the maximum), and the robot looks for the smallest (the minimum).
Way two: the clever alpha-beta trick
Now let's be smarter. Click "Next" and watch which bags we really need to open.
Order matters
What if the bag with 2 candies came first in box C? Then right after the first bag we'd know that C is worse than A, and we could cut off the rest. We'd only need to open 5 bags.
That's why good game programs check the moves that look best first. The sooner they find a good move, the more branches they can cut off.
Why does this matter so much?
In our game we only saved a couple of bags. But in chess there are about 30 possible moves in every position. To think 4 moves ahead (two of its own and two of the opponent's), a computer would have to check 30 × 30 × 30 × 30 positions.
810,000
positions to check without the trick
~1,800
positions with alpha-beta, when the best moves are checked first
That's over 400 times less work! So in the same amount of time the computer can think much further ahead and play much better. Most of the games on this site where you play against the computer use exactly this alpha-beta trick.
Play against the computerWhy is it called alpha-beta?
Alpha and beta are the first two letters of the Greek alphabet. While searching, the computer remembers two numbers:
α
Alpha is the best result you already have for sure. In our game, that was the 3 candies from box A.
β
Beta is the most the opponent will let you get in the branch you're checking right now. In box B, after the first bag, it was 2.
When beta drops to alpha or below, searching that branch any further is pointless, so the computer cuts it off. That's why it's called alpha-beta pruning.
Remember
- A computer plays by imagining a tree of possible moves: its own and its opponent's.
- If you already know a move is worse than the one you have, you don't need to find out exactly how much worse.
- Cutting off those branches is alpha-beta pruning: the same answer with much less work.