Search

Search papers and lessons

Tree Search

Level 2 · 8 min read

Tree search explores the states reachable from a start node in a systematic order, such as level by level, deepest first, or best guess first, until it finds a goal.

On this page

  • Colour = topic mix
  • Algorithms
  • Discrete math
  • Sign in to see your progress

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

Definition: Search frontier

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:

  1. Put the start state in the frontier and mark it as seen.
  2. Remove one state from the frontier, chosen by the search strategy.
  3. If it is a goal, return the path to it.
  4. For each neighbour not yet seen, mark it as seen and add it to the frontier.
  5. If the frontier is empty, report that no goal is reachable.

The strategy is the only difference between the algorithms.

The frontier is a first-in, first-out queue. States are removed in the order they were added, so all states at distance kk are expanded before any at distance k+1k+1. The first goal found is at the smallest possible number of moves when every move has the same cost.

If the search reaches depth kk with branching factor bb, the number of states generated is at most

1+b+b2+⋯+bk=bk+1−1b−1.1 + b + b^2 + \cdots + b^{k} = \frac{b^{k+1} - 1}{b - 1}.

The memory cost is the frontier, which can hold about bkb^{k} states. That is the main weakness.

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.

The frontier is a priority queue ordered by a score f(s)f(s), which estimates how promising state ss is. A common choice is f(s)=g(s)+h(s)f(s) = g(s) + h(s), where g(s)g(s) is the cost so far and h(s)h(s) is an estimate of the cost left. This is the idea of A* (Hart, Nilsson and Raphael, 1968). If hh never overestimates the true remaining cost, A* still returns a shortest path.

Worked examples

Example
The same graph, two orders

Take the graph with start AA and goal FF. The edges are:

  • A→BA \to B, A→CA \to C
  • B→DB \to D, B→EB \to E
  • C→FC \to F
  • D→GD \to G, E→GE \to G

Every state is marked when it is first added to the frontier.

Breadth-first search with a queue:

  1. Start: queue is [A][A]. Pop AA. Add BB and CC. Queue is [B,C][B, C].
  2. Pop BB. Add DD and EE. Queue is [C,D,E][C, D, E].
  3. Pop CC. Add FF. Queue is [D,E,F][D, E, F].
  4. Pop DD. Add GG. Queue is [E,F,G][E, F, G].
  5. Pop EE. Its neighbour GG is already seen, so nothing is added. Queue is [F,G][F, G].
  6. Pop FF, a goal. The path is A→C→FA \to C \to F, of length 2.

BFS expands 6 states.

Depth-first search with a stack, pushing neighbours so that BB is explored first:

  1. Start: stack is [A][A]. Pop AA. Push CC, then BB. Stack is [C,B][C, B].
  2. Pop BB. Push EE, then DD. Stack is [C,E,D][C, E, D].
  3. Pop DD. Push GG. Stack is [C,E,G][C, E, G].
  4. Pop GG. It has no neighbours. Stack is [C,E][C, E].
  5. Pop EE. Its neighbour GG is already seen. Stack is [C][C].
  6. Pop CC. Push FF. Stack is [F][F].
  7. Pop FF, a goal. The path is A→C→FA \to C \to F, the same, but found after 7 expansions.

Interpretation: both found the same path here. DFS spent four expansions (BB, DD, GG, EE) on the branch under BB before reaching CC. On a larger graph with a deep wrong branch, that cost can grow to most of the graph.

Example
Why breadth-first memory hurts

Take a search where each state has b=10b = 10 neighbours and the goal is 6 moves away.

  1. States generated in the worst case: 1+10+102+103+104+105+106=1,111,1111 + 10 + 10^2 + 10^3 + 10^4 + 10^5 + 10^6 = 1{,}111{,}111.
  2. The frontier at the last level alone can hold about 10610^6 states.
  3. A depth-first search with the same branching factor keeps only about 6×10=606 \times 10 = 60 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

SituationProblemBetter choice
Every move costs the same, shortest path neededDepth-first gives long pathsBreadth-first search
Large branching factor, goal is deepBreadth-first memory explodesDepth-first, or best-first with a good score
Graph with cycles and no seen-setSearch loops foreverAdd a seen-set (the mark in step 4)
Infinite graph with no goalDepth-first may never returnDepth limit, or iterative deepening
Good estimate of distance to the goalBreadth-first wastes workBest-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 AA the goal FF is two moves away through CC, while the deepest branch goes A→B→D→GA \to B \to D \to G. 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 FF 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 1+10+100+1,000+10,000=11,1111 + 10 + 100 + 1{,}000 + 10{,}000 = 11{,}111 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