Depth-first search
Traverse a graph with recursive depth-first search and with an explicit stack, record the discovery order, and use DFS to find connected components and cycles.
It opens on
BFS from A visited the vertices in the order A, B, C, D, E, F. Will depth-first search from A, with the same neighbour lists, visit D fourth as well?
Why it matters
DFS is the other basic graph walk; it explores mazes, finds what is reachable, splits a network into connected pieces, detects cycles such as circular dependencies, and underlies topological sort, all in O(V + E); tracing its order is a staple exam question next to BFS.
Good to know first
- Breadth-first search: adjacency lists, the visited check and O(V + E)
- Stacks: last in, first out, with append and pop
- Recursion and the call stack: each call waits on the stack until the calls it makes return
Step by step
Going deep
Trace recursive DFS from A, giving the discovery order and the depth of each call.
From A, the first neighbour is B, so dfs goes to B at once. B's first unvisited neighbour is C, A being visited, so it goes to C.
A common mix-up
DFS and BFS visit vertices in the same order with only the data structure changed in the explanation.
In fact: The orders differ, here A, B, C, E, D, F against A, B, C, D, E, F.
With an explicit stack
Write DFS with a list as a stack instead of recursion, reproduce the same order, and give its O(V + E) cost.
Pop A, mark it, and push C then B, so B is on top. Pop B and push its unvisited neighbours D then C.
the stack, top on the right
A common mix-up
Swapping BFS's queue for a stack gives exactly recursive DFS's order.
In fact: Only if neighbours are pushed in reverse and visited vertices are skipped when popped, not when pushed.
Components and cycles
Find the connected components of an undirected graph by restarting DFS, and detect a cycle by an edge to a visited vertex that is not the parent.
The run printed two components, A to F and G, H. The whole loop is still O(V + E), since every vertex and edge is handled once across all the searches.
A common mix-up
Any edge to a visited vertex means a cycle in an undirected graph.
In fact: The edge back to the parent is the same edge just used; only a visited non-parent closes a cycle.
Try it
Run dfs from F. What order does it give?Answer
F, E, C, A, B, D
Hint: F's only neighbour is E, and E's first is C
Without reversed, in what order would dfs_stack visit from A?Answer
A, C, E, F, D, B, since the last neighbour pushed, C, comes out first each time
Hint: C is on top after A
Add the edge F–G. How many components are there now, and does the graph still have a cycle?Answer
1 component, A to H, and yes, the cycle A, B, C is still there
Hint: F–G joins the two pieces
At C, which neighbour does dfs go to next, and from there where?Answer
E, since A and B are visited; then from E, its first unvisited neighbour, D.
The stack is now C, D, C. What is popped next, and what does it push?Answer
C, the top; it pushes E, its only unvisited neighbour, giving C, D, E.
