---
title: 'Minimax Algorithm and Alpha-Beta Pruning Explained'
source: 'https://youtube.com/watch?v=l-hh51ncgDI'
video_id: 'l-hh51ncgDI'
date: 2026-07-13
duration_sec: 661
---

# Minimax Algorithm and Alpha-Beta Pruning Explained

> Source: [Minimax Algorithm and Alpha-Beta Pruning Explained](https://youtube.com/watch?v=l-hh51ncgDI)

## Summary

This video explains the minimax algorithm used in turn-based games like chess, including how it evaluates positions by searching ahead through a game tree. It also introduces alpha-beta pruning to optimize the search by cutting off branches that cannot affect the final decision.

### Key Points

- **Game Tree Concept** [00:00] — In turn-based games like chess, a program can visualize future positions as a tree of moves, with branches representing possible moves from each position.
- **Static Evaluation** [00:38] — At leaf nodes (end of game or depth limit), a static evaluation is performed, e.g., summing piece values in chess: positive for white, negative for black.
- **Minimax Principle** [01:08] — White (maximizing player) chooses moves leading to highest evaluation; black (minimizing player) chooses moves leading to lowest evaluation. Values propagate up the tree.
- **Minimax Code Structure** [02:29] — The minimax function takes position, depth, and a boolean maximizingPlayer. It recursively evaluates children, returning max or min based on whose turn it is.
- **Alpha-Beta Pruning Introduction** [05:12] — Pruning avoids evaluating branches that cannot affect the outcome. If a branch is worse than an already known option, it is cut off.
- **Move Ordering Importance** [06:44] — Pruning efficiency depends on move order. Good moves (e.g., captures in chess) should be explored first to maximize pruning.
- **Alpha-Beta Code Implementation** [08:08] — Add alpha (best for max) and beta (best for min) parameters. If beta <= alpha, break the loop, pruning remaining children.
- **Historical Context** [10:31] — The video ends with the first time a computer defeated a reigning world champion in classical time controls (Deep Blue vs. Kasparov).

### Conclusion

The minimax algorithm, combined with alpha-beta pruning, allows AI to efficiently search game trees and make optimal decisions. Proper move ordering significantly enhances pruning effectiveness.

## Transcript

say we want to write a program that can play some turn-based game like chess one ahead at possible future positions before deciding what move it wants to
make in the current position this white dot represents some position in our game with the white side move to keep things simple let's say that in every position there are only two possible moves to choose between we can visualize these
moves as two separate branches at the end of which or two new positions where it's now of course blacks turn to move we can continue expanding the tree of moves until either we reach the end of the game or we decide to stop because
going deeper would take too much time either way at the end of the tree we no need to perform a static evaluation on these final positions a static without making any more moves for example a crude approach in chess
would be to add up the values of the remaining white pieces and subtract from that the values of all the remaining black pieces so large values would favor white while small values would favor black for this reason white is always
trying to maximize the evaluation while black is trying to minimize it so let's not with these two positions on the bottom left so we evaluate them and they come out as minus 1 and plus 3 well in the previous position
it was White's turn to move and since white will of course choose the move that leads to the highest evaluation we can assign this position a value of 3 as well next let's evaluate these two positions and so we get plus 5 and plus
1 once again from the previous position white would pick the move leading to the highest evaluation and so we can assign it a value of 5 we've now evaluated both the positions stemming from this position where it's
blacks turn to move black will choose the move that leads to the lowest evaluation and so we can assign this position a value of 3 I'll very quickly step through the other half of the tree so we evaluate these as minus 6 and
minus 4 which will pick the minus 4 the next two positions are evaluated as 0 and 9 so white will pick the 9 and between minus 4 and 9 pick - for at last we've arrived at the top of the tree or where we can see that
why should choose the move on the left since that way even if black plays the best move white will still get a +3 position so now that the basic idea is hopefully clear let's look at how this is implemented in code we have a
function called minimax which takes in the current position a depth for how many moves ahead we want to search and a bool called maximizing player we begin position in which case we return the static evaluation of that position
otherwise if it's currently the turn of the maximizing player which in our example means it's white to move then we want to find the highest evaluation that can be obtained from this position so we create a variable called Max evaluation
and initialize that to negative infinity we then loop through all the children of reached in a single move to find the evaluation of each child we make a
recursive call to the minimax function passing in the child depth minus one and false since it will now be the other players turn to move we can then set max eval equal to whichever is greater between the current max evaluation and
the evaluation of the child position once we've evaluated all the children we can return the maximum evaluation that we found now we do essentially the same thing for the minimizing player creating mini eval set initially to positive
infinity and for each child position we call minimax passing in the child depth minus 1 and this time true the mini value and get set whichever is smaller between the current minimum evaluation and the child evaluation and finally
we'll return them in evaluation let's step through this example again this time with the code in front of us first though we'll need an initial call to the minimax algorithm to start things off ok we're at the first position and we want
to find the max of the two children so we call minimax on the first child it first child which in turn wants to find the max of its two children so it calls
minimax and first at this point though depth is equal to 0 so minimax returns the static evaluation of that position this value on its second child receives the static evaluation from that and returns the max
between the two children that value gets passed up to its parent which now calls minimax on its second child minimax is called on its two children getting their static evaluations and the max is passed up to the parent which passes the min of
its two children up to its parent which knucles minimax on its second child I'll stop with the blow-by-blow narration now but hopefully it's quite clear how the algorithm uses recursion to search through the tree
now that we've seen minimax works let's run through this example yet again to look at how it can be sped up using pruning these first few steps are the same as before but consider the situation we have after
evaluating this +5 position without yet evaluating the other position we know being greater than or equal to 5 we can now see that black won't go down this
branch because he already has a better option available this observation means that we don't have to waste any computation on evaluating this final pruned it from the tree things now continue as normal again for a few steps
until we get here and black to play in this position will be choosing whichever of the two moves leads to the lowest evaluation so we know the evaluation here is going to be less than or equal to minus 4 we can now be sure that white
him and so we can prune these positions as you can see the results of the search is exactly the same as before we've just saved some time by not considering
positions when they can't affect the outcome ok I want to quickly go through a slightly deeper tree I'll just fly through these first few steps because there's no pruning happening so it's quite straightforward in fact it is
been able to prune a single position yet so pruning isn't guaranteed to occur it very much depends on what order the moves are in ideally the moves would be
for example if these two moves had been the other way around we would have been able to prune the second one for this reason it's usually a good idea to order the moves based on how likely they are to be good for example in chess
capturing a piece with a pawn is very likely to be a good move and so it would be wise to explore at first anyway let's continue with this tree so this next position has been evaluated as plus one and now I have a question do we need to
evaluate the other position here or can we prune it if you're in the mood so without evaluating the other position we can say for sure that the previous
position is less than or equal to one since black is of course just gotta choose whichever is lowest this means that white will never go down this particular branch because if the best he can hope for is a plus one he'd
rather just go the other way at the start where he's guaranteed a plus 3 so we can indeed put in that position all right let's finish off this tree here we got a 5 and a 2 so black would of course choose the 2 that means this position is
branch since you can get a 3 by going the other way this means that we can prune the rest of these positions the tree is not complete and we can see that with best play from both sides the game will follow this
path let's look at how this pruning stuff works in code we'll start with our original minimax algorithm and add two parameters called alpha and beta which assuming best play from the opponent will measure update our recursive calls
to minimax to pass in these two new values now for the maximizing player we'll set alpha to whichever is greater between alpha and the latest evaluation then if beta is less than or equal to alpha we'll break out of the loop
similarly for the minimizing player will set beta equal to whichever is smaller between beta and the latest evaluation and then once again if beater is less than or equal to alpha we'll break out of the loop to better understand what's
going on let's step through the example one last time in our initial call to the worst possible score for white as our value for alpha and positive
infinity the worst possible score for black as the value for beta these values get passed down until we reach our first end position here negative 1 is greater than alpha so the alpha value of the parent gets set to negative 1 3 is then
greater than negative 1 so the alpha value gets updated once again however beatriz not less than alpha here so no pruning occurs next in the parent position B to get set to 3 and this is passed down to its second child after
evaluating this +5 position alpha get set to +5 and now beta is less than move BtoB unless an alpha means that black had a better option available
earlier on in the tree and so we prune going back up the tree alpha get set to three in the first position so we know white is guaranteed at least a three but that so coming down here near the negative
so alpha doesn't change in the parent position be sure now get set to negative four which is less than alpha since this position is black to move
earlier on in the tree and so once again we can prune we've now completed the tree so hopefully this video has made sense of the minimax algorithm and how
it can be sped up using this idea of pruning any branches that can't affect the outcome due to the fact that one side will prevent that branch from ever I'll leave you with the last few moves of this game which was the first time a
computer was able to defeat a reigning world champion in classical time controls
you
