TL;DR
Many problems ask for a path from a start state to a goal state: a route on a map, a sequence of moves in a puzzle, a plan. The states and the moves form a graph. Tree search walks that graph in a fixed order, keeps a list of states it has yet to visit, and stops when it reaches a goal. The order decides what you find first and how much memory the search uses.
Where you'll see it: route finding, game-playing programs, parsers and planners, and the search that language models use when they consider several reasoning paths.
Intuition
Searching a building
Suppose you need to find a room in a large building, and you can only walk through doors. You could check every room on the ground floor before going upstairs. That is breadth-first: you finish one layer of distance before the next. Or you could pick a corridor and follow it to its end before trying any other corridor. That is depth-first: you go as far as you can, then back up and try the next branch. A third option is to look at the map and pick the door that seems closest to the room you want. That is best-first.
The three methods visit the same rooms in different orders. The order matters because a lucky search finds the goal quickly, and an unlucky one wanders into dead ends.
Why the idea exists
Without a plan, a search can revisit the same state again and again, or loop forever on a cycle. Tree search gives one simple loop with three choices: how to pick the next state, and whether to remember the states already seen. Every algorithm in this family is that loop with a different choice of the next state to take from the list.
The math
The frontier is the set of states that have been discovered but not yet expanded. Expanding a state means generating its neighbours, the states reachable in one move, and adding the new ones to the frontier. The search repeats this until it pops a goal state or the frontier is empty.
The generic loop is:
- Put the start state in the frontier and mark it as seen.
- Remove one state from the frontier, chosen by the search strategy.
- If it is a goal, return the path to it.
- For each neighbour not yet seen, mark it as seen and add it to the frontier.
- If the frontier is empty, report that no goal is reachable.
The strategy is the only difference between the algorithms.
Breadth-first search
The frontier is a first-in, first-out queue. States are removed in the order they were added, so all states at distance are expanded before any at distance . The first goal found is at the smallest possible number of moves when every move has the same cost.
If the search reaches depth with branching factor , the number of states generated is at most
The memory cost is the frontier, which can hold about states. That is the main weakness.
Depth-first search
The frontier is a last-in, first-out stack, or equivalently the call stack of a recursive function. The search always expands the most recently added state, so it goes deep before going wide. It needs memory proportional to the depth times the branching factor, which is small, but it does not find the shortest path, and on an infinite or very deep graph it can run for a very long time before finding a goal near the start.
Best-first search
The frontier is a priority queue ordered by a score , which estimates how promising state is. A common choice is , where is the cost so far and is an estimate of the cost left. This is the idea of A* (Hart, Nilsson and Raphael, 1968). If never overestimates the true remaining cost, A* still returns a shortest path.
Worked examples
Take the graph with start and goal . The edges are:
- ,
- ,
- ,
Every state is marked when it is first added to the frontier.
Breadth-first search with a queue:
- Start: queue is . Pop . Add and . Queue is .
- Pop . Add and . Queue is .
- Pop . Add . Queue is .
- Pop . Add . Queue is .
- Pop . Its neighbour is already seen, so nothing is added. Queue is .
- Pop , a goal. The path is , of length 2.
BFS expands 6 states.
Depth-first search with a stack, pushing neighbours so that is explored first:
- Start: stack is . Pop . Push , then . Stack is .
- Pop . Push , then . Stack is .
- Pop . Push . Stack is .
- Pop . It has no neighbours. Stack is .
- Pop . Its neighbour is already seen. Stack is .
- Pop . Push . Stack is .
- Pop , a goal. The path is , the same, but found after 7 expansions.
Interpretation: both found the same path here. DFS spent four expansions (, , , ) on the branch under before reaching . On a larger graph with a deep wrong branch, that cost can grow to most of the graph.
Take a search where each state has neighbours and the goal is 6 moves away.
- States generated in the worst case: .
- The frontier at the last level alone can hold about states.
- A depth-first search with the same branching factor keeps only about pending states at a time.
Interpretation: BFS finds the shortest path, but its memory grows with the last level. Depth-first is light on memory, but it may explore many wrong branches before the goal. Best-first search reduces the number of states expanded when the score is good, and the choice depends on the problem.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Every move costs the same, shortest path needed | Depth-first gives long paths | Breadth-first search |
| Large branching factor, goal is deep | Breadth-first memory explodes | Depth-first, or best-first with a good score |
| Graph with cycles and no seen-set | Search loops forever | Add a seen-set (the mark in step 4) |
| Infinite graph with no goal | Depth-first may never return | Depth limit, or iterative deepening |
| Good estimate of distance to the goal | Breadth-first wastes work | Best-first search (A*), if the estimate never overestimates |
The most common bug is leaving out the seen-set. On a tree, every state has one parent and cycles cannot occur, so the search is safe without it. On a graph, the same state can be reached by several paths. Without the seen-set, the search repeats work, or never ends.
Tree search is also expensive when the branching factor is large, because it expands states that a human would dismiss at once. Heuristics and pruning reduce this, but the search still has to be correct for the problem.
History
Breadth-first search for shortest paths in mazes is often credited to E. F. Moore, and depth-first search grew out of early graph-traversal work. Both are old enough that their origins are hard to pin to one paper.
The best-first family became central to artificial intelligence through Hart, Nilsson and Raphael's 1968 paper on A*. It showed that a heuristic which never overestimates lets the search return a shortest path while expanding fewer states than breadth-first search. A* remains a standard tool in route planning and game AI.
Check yourself
QuestionWhat single change turns breadth-first search into depth-first search?Show answer
Replace the first-in, first-out queue with a last-in, first-out stack, so the most recently discovered state is expanded next. Equivalently, the recursive version uses the call stack.
QuestionOn a graph where every edge costs 1, from start the goal is two moves away through , while the deepest branch goes . Which search finds the shortest path, and why?Show answer
Breadth-first search. It expands all states at distance 1 before any at distance 2, so the first goal it pops is at the minimum distance. Depth-first search follows the long branch first and only finds after exploring it.
QuestionA search generates 10 neighbours per state and the goal is 4 moves away. What is the largest number of states generated by breadth-first search, counting the start?Show answer
The count is in the worst case, when every state is expanded. The frontier at the last level can hold about 10,000 states.
QuestionWhy does a search on a graph with cycles need a seen-set, while a search on a tree does not?Show answer
In a tree each state has exactly one path from the start, so a search can never return to a state it has visited. In a graph, a cycle lets the search return to an earlier state and loop, or reach the same state by many paths and repeat work. The seen-set records each state once and stops both problems.
Sources
- Breadth-first search: https://en.wikipedia.org/wiki/Breadth-first_search
- Depth-first search: https://en.wikipedia.org/wiki/Depth-first_search
- A* search algorithm: https://en.wikipedia.org/wiki/A*_search_algorithm