Graphs
Breadth-first search vs Depth-first search
Breadth-first search and Depth-first search both explore a graph, in different ways. Breadth-first search: explore level by level with a queue. Depth-first search: go as deep as possible first, with recursion or a stack.
At a glance
| Property | Breadth-first search | Depth-first search |
|---|---|---|
| Technique | Explore level by level with a queue | Go as deep as possible first, with recursion or a stack |
| Best case time | O(V + E) | O(V + E) |
| Average case time | O(V + E) | O(V + E) |
| Worst case time | O(V + E) | O(V + E) |
| Extra space | O(V) | O(V) |
| Frontier (what holds the nodes to visit) | Queue: first in, first out | Call stack: last in, first out |
| Reads edge weights | No | No |
| Path guarantee | Fewest edges from the start | None: it finds a path, not a short one |
When to choose each
Breadth-first search: when to choose it
Choose breadth-first search when distance in edges is the answer you need. Its queue imposes an order with a guarantee attached: a node is finished only after every node closer to the start has been, so the first time BFS reaches a node it has reached it by a path with the fewest possible edges. That is what makes it the right tool for "how many hops away", for a shortest route on an unweighted map, and for anything that wants the graph level by level — a friends-of-friends listing, a web crawl by depth, a flood fill. It pays for the guarantee in memory: the queue holds a whole level at once.
Depth-first search: when to choose it
Choose depth-first search when the question is about structure rather than distance. Following one path to its end and backtracking is exactly the shape of cycle detection, topological ordering, finding connected components, and any exhaustive walk over paths or configurations. It is also the cheaper of the two to write, because the recursion stack is the data structure — in this lesson's implementation DFS keeps no distance array at all and records zero writes, where BFS writes a distance every time it discovers a node. Just do not ask it for a short path: the path it finds is the first one it stumbles into, not the shortest.
The same inputs, counted
These numbers come from running both real implementations: edge checks: one for every neighbour the algorithm looks at, on the three graphs from the lessons. Writes are not counted here: breadth-first search and depth-first search record very different amounts of state per node, so a combined total would compare two different things.
| Input | Breadth-first search | Depth-first search |
|---|---|---|
| Road map | 14 | 14 |
| Ring with shortcuts | 16 | 16 |
| Disconnected | 6 | 6 |
What the numbers show
On the road map and on the ring, the two run neck and neck: 14 edge checks for BFS and 14 for DFS on the road map, 16 and 16 on the ring. That is not a coincidence and it is the clearest thing these counts teach. Both algorithms look at every edge from both of its endpoints exactly once over a connected component, so both do 2E checks — 7 edges and 8 edges in these two graphs — and O(V + E) is the same bound for both. Order of exploration changes nothing about the total amount of work.
What differs is the order, and it is visible on the road map. BFS takes its nodes A, B, C, D, E, F — level by level, so it finishes D, two edges from the start, fourth. DFS takes them A, B, C, E, F, D: it commits to the first path out of A and only reaches D after walking five edges deep, because D is the last unvisited neighbour it backs up to. Same graph, same cost, completely different answer to "what have I found so far".
The disconnected graph shows the one guarantee they genuinely share. BFS makes 6 edge checks there and DFS the same 6, both report exactly A, B, C, and both leave D, E and F untouched: neither can cross to a component the start cannot reach. On that graph BFS returns the distances 0, 1, 1 and ∞ for the three unreachable nodes, while DFS returns the visiting order A, B, C — the difference between the two is not what they can reach, it is what they bother to record on the way.