Breadth-first search

Published

2023-08-05

Recall that we refer to all nodes of the same distance from the root as “levels”. The root itself is the zero level. Then there are nodes of distance one, nodes of distance two, and so on. Breadth-first search proceeds by levels, first exploring the root node, then all nodes at distance one from the root, then all nodes of distance two from the root, and so on. This approach is different from DFS, and it has different applications including finding shortest paths, exploring a maze, or crawling the Internet.

Breadth-first Search (03:22)

We saw above what the worst-case graph looks like for depth-first search—a tree of height N - 1. What do you think a worst-case tree might look like for breadth-first search? Unsurprisingly, this is it:

Worst case BFS

As with depth-first search, the worst-case run-time complexity is \mathcal{O}(N + E).

We mentioned in the video that breadth-first search may take more memory than depth-first search. This is not uncommon in practice, but if we look at the worst-case for both breadth-first search and depth-first search, we see that space complexity is the same, of order \mathcal{O}(N).

Additional reading

Resources

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

All materials copyright © 2020–2023, The University of Vermont. All rights reserved.