Compare depth-first and breadth-first discovery

Keep the names of states and actions distinct. · 178 words

Depth-first search (DFS) and breadth-first search (BFS) organize a frontier differently. A frontier holds states discovered but not yet expanded. DFS uses a last-in, first-out stack, so it follows one branch deeply before trying another. BFS uses a first-in, first-out queue, so it examines nearer states first. This choice affects route quality, computation, and memory use.

Imagine finding a picnic table in a park. The trail graph has S at the entrance, A and B as forks, C beyond A, and G, the table, beyond C. Edges are S-A, S-B, A-C, C-G, and B-G. If DFS tries A before B, it discovers S, A, C, G and returns the three-edge route S-A-C-G. BFS discovers S, then A and B, then C and G. It returns the two-edge route S-B-G. DFS may reach a goal quickly or waste effort down a long branch, but it usually uses less frontier memory. BFS guarantees a shortest route when every edge has equal cost, but it can store many states. If travel times differ, ordinary BFS does not guarantee the fastest route.

Check your recall

In the park example, which route does DFS return when it tries A first, and which route does BFS return?

Check your answer

DFS returns the longer route S-A-C-G with three edges. BFS returns the shorter route S-B-G with two edges.

Sources and credits

Original KeyTouch lesson. Background reading · UC Berkeley · Introduction to Artificial Intelligence.