Breadth-first and depth-first search on one graph
Last updated: 2026-11-26
Breadth-first search visits a graph level by level with a queue, and depth-first search dives down one branch with a stack. Run on the same graph from the same start, they visit the same nodes and can do so in different orders. The order is the payload: it decides which search can read shortest paths off its own run in an unweighted graph.
Start learning No account needed
1. Two disciplines for one frontier
Both searches walk a graph from a start node and visit each reachable node once. The difference is which discovered node gets attention next. Wikipedia's article on breadth-first search describes finishing every node at one depth before moving a level further out, usually with a queue to hold nodes that have been found and not yet explored. The depth-first search article describes the opposite habit: follow one branch to its end, then backtrack, usually with a stack that keeps track of the nodes along the current branch.
Queue and stack set the two apart. A queue hands back the oldest discovered node, so earlier discoveries are handled before later ones, which makes the walk level by level. A stack hands back the most recent, so the walk keeps going down the branch it just opened and returns to earlier nodes when that branch runs out. A recursive depth-first search, like the one in the script, gets its stack from the function calls.
2. A worked example: six nodes, two orders
The graph has six nodes and six undirected edges: A-B, A-E, B-C, C-D, D-F and E-F. The script runs both searches from A, visiting neighbors in alphabetical order so the run is repeatable, and then reads distances off the BFS parents:
Output from the script scripts/breadth-first-vs-depth-first-search-worked-example.py:
One graph, one start node A, neighbors visited in alphabetical
order. Edges: A-B, A-E, B-C, C-D, D-F, E-F.
BFS from A (queue, level by level): A, B, E, C, F, D
DFS from A (stack, deep before wide): A, B, C, D, F, E
BFS distances, fewest edges from A:
A: 0 B: 1 E: 1 C: 2 D: 3 F: 2
Shortest walk from A to F: A - E - F, 2 edges.
DFS reached F first along A - B - C - D - F,
4 edges, because it walks one branch all the way
before trying the next one.
Same graph, same start, different visits. BFS holds untried
nodes in a queue and finishes each level first, so its
distances are the shortest-path costs in an unweighted graph.
DFS follows one branch first (here the stack is the chain of
function calls), so its first path need not be the shortest.
If A tries E before B:
BFS: A, E, B, F, C, D
DFS: A, E, F, D, C, B
checks passed
Follow F in both walks. Breadth-first search reaches F from E, one step past A's immediate neighbors, and its parent table prices F at 2 edges, the walk A - E - F. Depth-first search entered F while descending the A - B - C - D branch, so the first path it recorded runs 4 edges. Both searches visited all six nodes. Only one of the two orders came with distances that solve the shortest-path problem, because only breadth-first search finishes a whole level before touching the next.
3. A common misconception: both searches visit everything, so either works for shortest paths
It is true that both searches reach every node on a connected graph, and that alone makes either one usable for checking reachability. Visiting everything is not the same as measuring the way in. Breadth-first search fixes each node's distance at the moment it enters the queue, and because whole levels drain before deeper ones start, that first entry is along a path with the fewest edges. Depth-first search records the first path it happens to walk, which depends on the neighbor order and can run long, as the 4-edge route to F shows in a graph whose shortest route is 2 edges.
Neighbor order matters to both. If A tries E before B, depth-first search visits A, E, F, D, C, B, and breadth-first search visits A, E, B, F, C, D. The breadth-first distances stay the same, because they count the fewest edges, while the depth-first path to a node can change length. An interviewer's question about which structure the search holds is usually a question about what the search is for.
4. Test yourself
Close the page and answer three questions. First: a transit map with equal-cost legs, and you want the fewest-legs route; which search and which data structure? Second: in the worked graph, which node does depth-first search visit last, and why? Third: what price does the BFS parent table put on D, and through which parent? Reopen and compare; the one you hesitated on is the step to study again.
For practicing this under exam conditions, see how to study for a computer science exam, and for the interview version, how to prepare for a coding interview. For checking yourself without help, see how to test yourself.
What the sources found, and their limits
Wikipedia, "Breadth-first search". It describes the level-by-level discipline and the queue holding discovered nodes. Source: Breadth-first search, read on 2026-11-26.
Wikipedia, "Depth-first search". It describes following a branch to its end before backtracking, with a stack as the usual extra memory. Source: Depth-first search, read on 2026-11-26.
Wikipedia, "Shortest path problem". Its unweighted-graphs section lists breadth-first search as the solving algorithm, which carries the shortest-path claim. Source: Shortest path problem, read on 2026-11-26.
Limits: three encyclopedia articles, not course texts, and the six-node graph is ours, built so the two orders differ and the depth-first path to F is longer than the shortest one. The script computes both orders, the distances and the two paths. All three articles carry a CC BY-SA license.
How Professor Turing helps
Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start.
Bring the graph problems you are working through, such as a textbook exercise asking for both visit orders. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, such as a structure name, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller instead of handing over the answer. Later in a session it can offer a short test without help, scored apart from the tutoring, so you can tell what you can do alone from what the tutor carried you through. You must be 18 or older, and guest conversations are deleted 7 days after the last activity.
FAQ
Which search finds shortest paths? Breadth-first search, when every edge costs the same. It discovers nodes level by level, so the first time it reaches a node is along a path with the fewest edges. In the worked example it prices F at 2 edges through E, while the depth-first walk first reached F through 4 edges.
Do both searches visit the same nodes? On a connected graph started at the same node, both visit every reachable node exactly once. What differs is the order and the paths recorded along the way, which comes from the frontier discipline: a queue for breadth-first search, a stack for depth-first search.
Start with what you're working on
Tell the tutor which graph problem you are stuck on and let it ask you the questions.
Start learning No account needed
See also: stack and queue · what is recursion · all study guides