DFS and BFS Basics
1Introduction
This lecture defines depth-first search and breadth-first search through their management of candidate vertices and explains why a visited set is necessary, why both traversals correctly characterize reachability, their complexity, and why BFS computes unweighted shortest-path distances.
Linear search and binary search locate a target value in an array. This lecture instead considers traversals that enumerate graph vertices by following edges.
The lecture assumes the definitions of vertices, edges, adjacency, paths, and distance. Review Graph Basics first if these concepts are unfamiliar.
data/lecture/information/graph/graph-basics.lecture.n.md data/lecture/information/algorithm/data-structures/stack-and-queue-basics.lecture.n.md2A common traversal framework
Let be a graph with a starting vertex . A traversal stores discovered vertices whose neighbors have not yet been fully examined as candidates.
- Initialize the visited set as and add to the candidates.
- While candidates remain, remove one vertex .
- For each neighbor , if , add to both and the candidates.
A vertex must be marked visited when it is added. If marking is delayed until removal, multiple edges may add the same vertex repeatedly.
3DFS
Depth-first search (DFS) manages candidates with a stack and prioritizes the most recently added candidate. In a recursive implementation, DFS chooses one unexamined neighbor and completes that recursive call before examining the next neighbor. It therefore extends one path and then backtracks.
In a recursive implementation, the function-call stack acts as the candidate stack. Even in the recursive version, a visited set is required for termination on graphs containing cycles.
4BFS
Breadth-first search (BFS) manages candidates with a queue. When BFS first discovers a vertex from its parent , it records the traversal level
with . The next section proves that this level equals the shortest-path distance in an unweighted graph.
5Why BFS gives shortest distances
Let denote the true shortest-path distance. The parent chain by which BFS discovers is a path of length , so .
A vertex added from a level- vertex has level and is placed after vertices already in the queue. FIFO order therefore makes queue removals occur in nondecreasing level order. We prove the reverse inequality by induction on . The case is the start vertex. For , let be the predecessor of on a shortest path. By the induction hypothesis, . When is processed, either it discovers at level or has already been discovered at a level at most . Hence , and the two inequalities give .
This claim is restricted to unweighted graphs, where every edge has equal cost. Graphs with unequal nonnegative edge weights require another method, such as Dijkstra's algorithm.
6Reachability and termination
Every visited vertex is reached by extending a known path by one edge, so it is reachable from the start. Conversely, use induction on the length of a path from the start. The case is the start vertex. For , the preceding vertex is discovered by the induction hypothesis. Because the finite candidate set is processed in turn, that vertex is eventually removed and discovers the next vertex. Hence both DFS and BFS visit every reachable vertex exactly once.
In a finite graph, each vertex is added to the candidates at most once, so the traversal terminates.
7Complexity
With adjacency lists, each vertex is processed once, and each undirected edge is examined at most once from each endpoint. Therefore both DFS and BFS take
time and require additional space for the visited set and candidates.
8Choosing between them
- Either DFS or BFS can enumerate reachable vertices and connected components.
- Use BFS for unweighted shortest distances and minimum-step problems.
- DFS is suitable for recursive structure, postorder processing, and prioritizing deeper candidates.
- Do not apply ordinary BFS to shortest paths with unequal edge weights.
9Summary
data/exercise/information/algorithm/search/graph-traversal-and-unweighted-distance.exercise.n.md- The essential difference between DFS and BFS is whether candidates are managed by a stack or a queue.
- The visited set prevents nontermination on cycles and duplicate candidates.
- Both traversals visit each reachable vertex exactly once and run in time with adjacency lists.
- BFS processes layers in distance order and therefore computes shortest distances in unweighted graphs.