Part 3: Machine learning Lesson 11 of 20 in this part
Search and games, the classic AI way
What you will learn. Explain how breadth-first search and A* find a route through a maze, why a good guess about the remaining distance saves work, and how minimax with alpha-beta pruning chooses a move in a two-player game.
Before this lesson: What AI, machine learning, deep learning and generative AI are. Already know these? Carry on.
In plain words
Before machine learning, much of AI was search: write down every situation the program can be in, every move it can make, and look for a sequence of moves that reaches a goal. A satnav looking for a route, a puzzle solver and a robot planning a path all work this way. Breadth-first search explores outwards one step at a time, so it always finds the route with the fewest steps, but it may look at a lot of places. A\ (say "A star") is smarter: for each place it adds the steps taken so far to a guess of the steps still needed, and explores the most promising place first. When the guess never over-estimates, A still finds the shortest route. For games with two players, minimax assumes your opponent always makes the move that is worst for you, and picks the move whose worst case is best. Alpha-beta pruning skips branches that cannot change the answer.
- InputStart with the starting place in a queueThe queue holds places to explore
- ProcessTake the oldest place from the queueCheck if it is the goal
- ModelAdd its unvisited neighbours to the backThey are one step further out
- OutputStop when the goal comes out of the queueThe route found is the shortest
Every place at distance one is explored before any at distance two, and so on.
Uninformed (breadth-first)
- No idea where the goal is
- Explores in all directions
Informed (A*)
- Uses a guess of the distance still to go
- Explores the promising places first
The only difference is whether the program has a guess about how far the goal is.
Minimax
- Looks at every possible move and reply
- Assumes the opponent plays their best
Alpha-beta
- Same answer as minimax
- Skips branches that cannot beat the best so far
Pruning never changes the answer. It only avoids work that cannot matter.
Real-world example
A delivery app needs the quickest way across a city of a million junctions. Breadth-first search would look at junctions in every direction, including those far away from the destination. The app uses A* with the straight-line distance to the destination as its guess. Straight-line distance never over-estimates the road distance, so A* is guaranteed to find the best route, and it explores far fewer junctions. The same family of ideas lies behind game-playing programs, which look ahead several moves with minimax and prune the branches that cannot matter.
How it works
- Describe the problem. A set of states (cells in the maze), the moves from each state, the starting state and the goal.
- Breadth-first search. Keep a queue of places to visit. Take the oldest, add its unvisited neighbours at the back, repeat until the goal is taken.
- **A\.* Give each place a score f = g + h, where g is the steps taken to reach it and h is a guess of the steps still to go. Always explore the place with the smallest f. If h never over-estimates, the first time the goal is taken, the route is the shortest.
- Minimax. In a game tree, the player to move picks the move with the best value. At the opponent's turn, the value is the lowest value for you among their replies.
- Alpha-beta. While checking replies to one move, stop as soon as one reply is already worse for you than a move you have already found. The remaining replies cannot make this move the best.
Live simulation
Search is a way to find a path or a move. Two pictures show the main ideas.
The question. How do different search methods find a way through a maze, and what does each one cost?
What to watch
- Slide the number of cells examined, or run to the end. The numbers show the order in which cells were examined.
- Breadth-first spreads out evenly. Depth-first dives as far as it can. A* leans toward the goal.
- Compare the path length and the number of cells examined for the three methods.
This simulation needs JavaScript, which is switched off or not loaded. The lists above say what it shows, and the example programs below print the same kind of numbers.
Try this
- Run A* to the end and see it examine fewer cells than breadth-first while still finding the shortest path (13 steps).
What this leaves out. One fixed 8 by 8 maze where every step costs the same. Real search problems have steps with different costs and far larger spaces, and the choice of guide for A* matters.
Python program that prints the same headline numbers: examples/sim-ai/ai.py.
The question. How does a program choose a move when an opponent will answer it, and how can it skip moves that cannot matter?
What to watch
- You pick one of three moves, then the opponent picks a reply. The numbers at the bottom say how good each result is for you.
- The opponent is assumed to pick the reply that is worst for you. You pick the move whose worst case is best (minimax).
- Switch on pruning and see how many positions can be skipped without changing the answer.
This simulation needs JavaScript, which is switched off or not loaded. The lists above say what it shows, and the example programs below print the same kind of numbers.
Try this
- Switch on pruning and see the program look at fewer than 9 positions.
What this leaves out. A tiny tree with nine made-up values. Real game programs search millions of positions, use a scoring function for positions they cannot finish, and order the moves so that pruning works best.
Python program that prints the same headline numbers: examples/sim-ai/ai.py.
Python example
Python: example run The example programme behind this lesson's quoted output ran on 2026-11-19 (Python 3.12.10). 1 code block in the lesson passed a syntax check, 1 also ran on their own.
A 5 by 5 maze with walls, solved by breadth-first search and by A*, followed by a small game tree solved by minimax and by alpha-beta. The standard library is enough.
"""Search in a grid maze (breadth-first and A*) and a two-player game tree (minimax with alpha-beta pruning).
Run: python search.py
"""
from collections import deque
maze = ["S..#.",
".#.#.",
".#...",
".###.",
"....G"]
H, W = len(maze), len(maze[0])
start, goal = (0, 0), (4, 4)
moves = [(-1, 0), (0, 1), (1, 0), (0, -1)] # up, right, down, left
def neighbours(p):
for dr, dc in moves:
r, c = p[0] + dr, p[1] + dc
if 0 <= r < H and 0 <= c < W and maze[r][c] != "#":
yield (r, c)
def bfs():
queue, seen, expanded = deque([(start, 0)]), {start}, 0
while queue:
p, d = queue.popleft(); expanded += 1
if p == goal:
return d, expanded
for q in neighbours(p):
if q not in seen:
seen.add(q); queue.append((q, d + 1))
def astar():
h = lambda p: abs(p[0] - goal[0]) + abs(p[1] - goal[1]) # Manhattan distance: never over-estimates
frontier, best, expanded, counter = [(h(start), 0, 0, start)], {start: 0}, 0, 1 # (f, order, g, cell)
while frontier:
frontier.sort(key=lambda t: (t[0], t[1]))
f, _, g, p = frontier.pop(0); expanded += 1
if p == goal:
return g, expanded
for q in neighbours(p):
if q not in best or g + 1 < best[q]:
best[q] = g + 1
frontier.append((g + 1 + h(q), counter, g + 1, q)); counter += 1
d1, e1 = bfs(); d2, e2 = astar()
print("maze: shortest path has %d steps" % d1)
print("breadth-first search looked at %d cells; A* looked at %d cells (path %d steps)" % (e1, e2, d2))
# Minimax on a game tree: the first player picks a branch, then the second player picks the worst leaf for the first.
tree = [[3, 5, 2], [2, 9, 1], [4, 6, 8]]
print("minimax value of the game: %d" % max(min(b) for b in tree))
def alphabeta():
alpha, looked = -99, 0
for branch in tree:
beta = 99
for leaf in branch:
looked += 1
beta = min(beta, leaf)
if beta <= alpha: # the first player already has something better elsewhere
break
alpha = max(alpha, beta)
return alpha, looked
value, looked = alphabeta()
print("alpha-beta gives the same value %d after looking at %d of 9 leaves" % (value, looked))maze: shortest path has 8 steps
breadth-first search looked at 18 cells; A* looked at 16 cells (path 8 steps)
minimax value of the game: 4
alpha-beta gives the same value 4 after looking at 7 of 9 leavesBoth searches find a path of 8 steps. The maze has 25 cells and 7 walls, so 18 open cells, and breadth-first search looked at all 18 of them. A* looked at 16, so it saved 2 cells on this small maze. The saving grows with the size of the maze. In the game tree, the best the first player can do is 4. Alpha-beta found it after looking at 7 of the 9 leaves.
.NET (C#) example
C#: compiled and run The C# sample compiled on 2026-11-19 with .NET SDK 10.0.400 (no extra packages). It also ran to the end without error.
Breadth-first search, A* and alpha-beta pruning written from scratch in C#, with no packages. It prints the same output as the Python run. .NET has no search library that this guide could verify, and these algorithms are short enough to write directly.
// Breadth-first search, A* and alpha-beta pruning from scratch. Same output as search.py. No packages.
string[] maze = { "S..#.", ".#.#.", ".#...", ".###.", "....G" };
int H = maze.Length, W = maze[0].Length;
(int r, int c) start = (0, 0), goal = (4, 4);
(int dr, int dc)[] moves = { (-1, 0), (0, 1), (1, 0), (0, -1) }; // up, right, down, left
IEnumerable<(int r, int c)> Neighbours((int r, int c) p)
{
foreach (var (dr, dc) in moves)
{
int r = p.r + dr, c = p.c + dc;
if (r >= 0 && r < H && c >= 0 && c < W && maze[r][c] != '#') yield return (r, c);
}
}
(int dist, int expanded) Bfs()
{
var queue = new Queue<((int r, int c) p, int d)>(); queue.Enqueue((start, 0));
var seen = new HashSet<(int, int)> { start }; int expanded = 0;
while (queue.Count > 0)
{
var (p, d) = queue.Dequeue(); expanded++;
if (p == goal) return (d, expanded);
foreach (var q in Neighbours(p)) if (seen.Add(q)) queue.Enqueue((q, d + 1));
}
return (-1, expanded);
}
(int dist, int expanded) AStar()
{
int Heur((int r, int c) p) => Math.Abs(p.r - goal.r) + Math.Abs(p.c - goal.c); // Manhattan distance: never over-estimates
var frontier = new List<(int f, int order, int g, (int r, int c) p)> { (Heur(start), 0, 0, start) };
var best = new Dictionary<(int, int), int> { [start] = 0 };
int expanded = 0, counter = 1;
while (frontier.Count > 0)
{
var top = frontier.OrderBy(t => t.f).ThenBy(t => t.order).First();
frontier.Remove(top); expanded++;
if (top.p == goal) return (top.g, expanded);
foreach (var q in Neighbours(top.p))
if (!best.TryGetValue(q, out int old) || top.g + 1 < old)
{
best[q] = top.g + 1;
frontier.Add((top.g + 1 + Heur(q), counter++, top.g + 1, q));
}
}
return (-1, expanded);
}
var (d1, e1) = Bfs(); var (d2, e2) = AStar();
Console.WriteLine($"maze: shortest path has {d1} steps");
Console.WriteLine($"breadth-first search looked at {e1} cells; A* looked at {e2} cells (path {d2} steps)");
// Minimax on a game tree: the first player picks a branch, then the second player picks the worst leaf for the first.
int[][] tree = { new[] { 3, 5, 2 }, new[] { 2, 9, 1 }, new[] { 4, 6, 8 } };
Console.WriteLine($"minimax value of the game: {tree.Max(b => b.Min())}");
int alpha = -99, looked = 0;
foreach (var branch in tree)
{
int beta = 99;
foreach (int leaf in branch)
{
looked++;
beta = Math.Min(beta, leaf);
if (beta <= alpha) break; // the first player already has something better elsewhere
}
alpha = Math.Max(alpha, beta);
}
Console.WriteLine($"alpha-beta gives the same value {alpha} after looking at {looked} of 9 leaves");Maths behind it
- Counting steps and adding costs (arithmetic)
- Manhattan distance on a grid (geometry)
- Exponential growth of choices (powers)
- Maximum and minimum along a tree (comparison)
Common mistakes
- Using a guess in A* that sometimes over-estimates the true distance, which can return a route that is not the shortest.
- Forgetting to remember visited places, so the search loops forever.
- Assuming minimax is enough for a large game without pruning or a good scoring guess.
- Searching a state space that is far too big for the time available, without a plan for cutting it down.
Quick check
Why does breadth-first search always find the shortest route in a maze?
Show answer
It explores all places at distance one before any at distance two, and so on, so the first time it reaches the goal it has used the fewest steps.
What is the guess in A* for, and what must be true of it?
Show answer
It estimates the steps still to go so that promising places are explored first. It must never over-estimate, or A* may miss the shortest route.
What does alpha-beta pruning change about the minimax answer?
Show answer
Nothing. It gives exactly the same value but skips branches that cannot matter.
A game has 3 moves per turn. How many end positions are there after 4 moves?
Show answer
3^4 = 81.
Completion
Evidence of completion. A maze solved by breadth-first search and by A* with the number of cells each looked at, and a small game tree solved by minimax with the number of leaves alpha-beta skipped.
Saved in this browser only. Reading and exercise are ticked separately because they are different achievements. A tick records your own work; it is not a test result. See all progress.
This lesson prepares you for: Logic, constraints and planning.