DFSとBFSの基本
1導入
この
グラフの
2共通 の探索 枠組 み
グラフ と
訪問済 み集合 を とし、候補 へ を追加 する。候補 が空 でない間 、頂点 を一 つ取 り出 す。- の
各隣接頂点 について、 なら を と候補 へ追加 する。
3DFS
4BFS
BFSで
と
5BFS最短性 の根拠
レベル の
この
6到達可能性 と停止性
7計算量
であり、
8使 い分 け
到達可能性 や連結成分 の列挙 には、DFSとBFSのどちらも使用 できる。無重 み最短距離 や最小手数 にはBFSを使用 する。再帰的 な構造 の調査 、帰 りがけの処理 、深 い候補 の優先 にはDFSが適 する。重 み付 き最短距離 にBFSをそのまま使用 してはならない。
9まとめ
data/exercise/information/algorithm/search/graph-traversal-and-unweighted-distance.exercise.n.md- DFSとBFSの
本質的 な差 は、候補 をスタックとキューのどちらで管理 するかにある。 訪問済 み記録 は、循環 による非停止 と候補 の重複 を防 ぐ。両者 は到達可能 な頂点 をちょうど一度 ずつ訪問 し、隣接 リストでは時間 で動作 する。- BFSは
層 を距離順 に処理 するため、無重 みグラフの最短距離 を求 める。
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.