BFS settles by distance
Every vertex at distance k is visited before any at distance k+1, which is exactly why its tree gives shortest paths.
Breadth-first search settles vertices in order of distance and therefore finds shortest paths in an unweighted graph. Depth-first goes as deep as possible first and is what detects cycles and orders dependencies.
Core ideas
The two searches differ in one data structure and produce completely different trees.
Every vertex at distance k is visited before any at distance k+1, which is exactly why its tree gives shortest paths.
Then backtracks. Its tree is deep and narrow, and the order of backtracking is what topological sorting uses.
They differ in order, not in coverage. Both cost Θ(n + |E|) with adjacency lists.
The material
Both algorithms, what each guarantees, and where each is used.
In an unweighted graph, dist[v] after BFS from s is the minimum number of edges on any s–v path.
By induction on the distance. Vertices at distance 0 and 1 are handled correctly by initialisation and the first round.
Suppose every vertex at true distance ≤ k is dequeued with the correct dist. A vertex v at true distance k+1 has a neighbour u at distance k; when u is processed, v is either already marked with a value at most k+1, or is marked k+1 then. It cannot be marked less, since that would give a shorter path.
The queue therefore holds vertices in non-decreasing distance order throughout.
□
The queue is doing the work. Replacing it with a stack gives depth-first search, and the distance guarantee vanishes immediately — DFS may reach a neighbour of the start by a long detour and record that depth.
Both find the components and both produce a spanning tree, and either serves for those. The distinctive uses come from the ORDER: BFS knows distances, DFS knows the nesting of the recursion, and each of those is the key to different problems.
During a depth-first search of an undirected graph, an edge to an already-visited vertex that is not the current vertex's parent closes a cycle. A graph is acyclic exactly when no such edge is found.
This is what makes DFS the tool for cycle detection: the search tree plus a single back edge exhibits the cycle explicitly. The parent exclusion matters — without it every edge would look like a cycle of length 2.
Each vertex is marked once and each edge examined twice (once from each end), so both searches cost Θ(n + |E|) with adjacency lists and Θ(n²) with a matrix. For a sparse graph that is the difference between linear and quadratic, which is “Graph Representations”'s trade-off arriving in practice.
Interactive
Your graph, the course's algorithms
Method
Choose the structure from the question.
Shortest paths, level structure, bipartiteness. Anything where "how far" matters.
Cycles, dependency order, articulation points. Anything where the nesting of the search matters.
Marking a vertex when it is added to the queue prevents it being added twice. Marking on removal is the classic bug.
One search covers one component. A full traversal loops over starting points.
Vertices 1–6; edges 1–2, 1–3, 2–4, 3–4, 4–5, 5–6.
That last line is the reason DFS cannot be used for shortest paths. It found vertex 3 by the route 1→2→4→3, and the depth it records is a fact about the search order rather than about the graph. The edge 3–1 is a back edge — at vertex 3 the neighbour 1 is already visited and is not 3's parent — and it closes the cycle 1–2–4–3–1. 4–3 is a tree edge, since 3 was discovered from 4.
Take a graph on seven vertices, write out the BFS and DFS orders by hand, and mark the tree edges of each. The two trees should look nothing alike.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.