markdown
DFS and BFS Basicsmd fc4dc8c
lecture/information/algorithm/search/dfs-and-bfs-basics.lecture.n.md
Download PDF

DFS and BFS Basics

date2026-07-14document_iddoc_27963c03131e4b56c071e4c2964429c7descriptionDFSとBFSを候補管理の違いから定義し、訪問済み管理、正当性、計算量、無重み最短距離を説明する。prerequisitesグラフの頂点・辺・隣接・道・距離 / スタックとキューの基本 / 再帰の基本type講義statusactiverelateddata/lecture/information/graph/graph-basics.lecture.n.md / data/lecture/information/algorithm/data-structures/stack-and-queue-basics.lecture.n.md
algorithmsearchundergraduatelecture

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.md

2A common traversal framework

Let G=(V,E) be a graph with a starting vertex sV. A traversal stores discovered vertices whose neighbors have not yet been fully examined as candidates.

  1. Initialize the visited set as S={s} and add s to the candidates.
  2. While candidates remain, remove one vertex v.
  3. For each neighbor uN(v), if uS, add u to both S 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 u from its parent v, it records the traversal level

[u]=[v]+1,

with [s]=0. The next section proves that this level equals the shortest-path distance in an unweighted graph.

5Why BFS gives shortest distances

Let δ(s,u) denote the true shortest-path distance. The parent chain by which BFS discovers u is a path of length [u], so δ(s,u)[u].

A vertex added from a level-k vertex has level k+1 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 r=δ(s,u). The case r=0 is the start vertex. For r>0, let w be the predecessor of u on a shortest path. By the induction hypothesis, [w]=r-1. When w is processed, either it discovers u at level r or u has already been discovered at a level at most r. Hence [u]δ(s,u), and the two inequalities give [u]=δ(s,u).

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 r of a path from the start. The case r=0 is the start vertex. For r>0, 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

O(|V|+|E|)

time and require O(|V|) 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 O(|V|+|E|) time with adjacency lists.
  • BFS processes layers in distance order and therefore computes shortest distances in unweighted graphs.
raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる