markdown
DFSとBFSの基本md fc4dc8c
lecture/information/algorithm/search/dfs-and-bfs-basics.lecture.n.md
Download PDF

DFSとBFSの基本きほん

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

1導入どうにゅう

この講義こうぎでは、ふか優先探索ゆうせんたんさく幅優先探索はばゆうせんたんさく候補頂点こうほちょうてん管理方法かんりほうほうから定義ていぎし、訪問済ほうもんず集合しゅうごう必要ひつよう理由りゆう到達可能性とうたつかのうせいかんする正当性せいとうせい計算量けいさんりょう、BFSが無重むおも最短距離さいたんきょりもとめる理由りゆう説明せつめいする。

線形探索せんけいたんさく二分探索にぶんたんさく配列はいれつから目標値もくひょうち探索たんさくした。これにたいし、この講義こうぎではグラフの頂点ちょうてんへん沿って列挙れっきょする探索たんさくあつかう。

グラフの頂点ちょうてんへん隣接りんせつみち距離きょり前提ぜんていとする。未確認みかくにん場合ばあいは、さきにグラフの基本きほん参照さんしょうする。

data/lecture/information/graph/graph-basics.lecture.n.md data/lecture/information/algorithm/data-structures/stack-and-queue-basics.lecture.n.md

2共通きょうつう探索たんさく枠組わくぐ

グラフ G=(V,E)始点してん sVかんがえる。探索たんさくでは、発見済はっけんずみだが隣接頂点りんせつちょうてん調査ちょうさえていない頂点ちょうてん候補こうほとして保持ほじする。

  1. 訪問済ほうもんず集合しゅうごうS={s} とし、候補こうほs追加ついかする。
  2. 候補こうほからでないあいだ頂点ちょうてん vひとす。
  3. v各隣接頂点かくりんせつちょうてん uN(v) について、uS なら uS候補こうほ追加ついかする。

頂点ちょうてん候補こうほ追加ついかすると同時どうじ訪問済ほうもんずみにすることが重要じゅうようである。した時点じてんまで記録きろくおくらせると、おな頂点ちょうてん複数ふくすうへんから重複ちょうふくして追加ついかされる。

3DFS

ふか優先探索ゆうせんたんさく(depth-first search; DFS)は候補こうほをスタックで管理かんりし、最後さいご追加ついかした候補こうほ優先ゆうせんする。再帰版さいきばんでは、ある頂点ちょうてん未探索みたんさく隣接頂点りんせつちょうてんひとえらんで再帰呼出さいきよびだしを完了かんりょうしてからつぎえらぶため、ひとつのみち延長えんちょうしてからもどる。

再帰さいきによる実装じっそうでは、関数呼出かんすうよびだしのスタックが候補こうほスタックの役割やくわりになう。再帰版さいきばんでも、循環じゅんかんふくむグラフで停止ていしするには訪問済ほうもんず集合しゅうごう必要ひつようである。

4BFS

幅優先探索はばゆうせんたんさく(breadth-first search; BFS)は候補こうほをキューで管理かんりする。さき追加ついかした頂点ちょうてんさきす。

BFSで頂点ちょうてん uはじめて発見はっけんしたとき、そのおやv として

[u]=[v]+1

記録きろくする。初期値しょきち[s]=0 である。まず 探索たんさくのレベルとび、最短距離さいたんきょりとの一致いっちつぎ証明しょうめいする。

5BFS最短性さいたんせい根拠こんきょ

おやじゅんにたどると、レベル [u]頂点ちょうてん u にはなが[u]みち存在そんざいする。したがってしん最短距離さいたんきょりδ(s,u) とすれば、δ(s,u)[u] である。

レベル k頂点ちょうてんから追加ついかされる頂点ちょうてんのレベルは k+1 であり、それまでにキューへ追加ついかされた頂点ちょうてんよりまえにはかれない。したがってFIFO順序じゅんじょにより、キューからはレベルの非減少順ひげんしょうじゅん頂点ちょうてんされる。

逆向ぎゃくむきの不等式ふとうしきを、しん距離きょりかんする帰納法きのうほうしめす。δ(s,u)=0 なら u=s であり [u]=0 である。δ(s,u)=r>0 とし、最短さいたんみちu直前ちょくぜんにある頂点ちょうてんw とする。δ(s,w)=r-1 なので、帰納法きのうほう仮定かていにより [w]=r-1 である。w はレベル r頂点ちょうてんよりまえ処理しょりされ、そのとき u未発見みはっけんなら [u]=r として発見はっけんされる。すでに発見済はっけんずみなら、そのレベルは r 以下いかである。ゆえに [u]r=δ(s,u) である。両方りょうほう不等式ふとうしきから [u]=δ(s,u)成立せいりつする。

この主張しゅちょう各辺かくへん費用ひようひとしい無重むおもみグラフにかぎられる。へんごとにことなる非負ひふおもみがある場合ばあいは、Dijkstraほうなどべつ方法ほうほう必要ひつようである。

6到達可能性とうたつかのうせい停止性ていしせい

探索中たんさくちゅう訪問済ほうもんずみとなる頂点ちょうてんは、既知きちみち一辺いっぺんだけ延長えんちょうして発見はっけんされるため、かなら始点してんから到達可能とうたつかのうである。ぎゃくに、ながrみち到達とうたつできる頂点ちょうてん発見はっけんされることを rかんする帰納法きのうほうしめす。r=0始点してんである。r>0 では、直前ちょくぜん頂点ちょうてん帰納法きのうほう仮定かていにより発見はっけんされ、有限個ゆうげんこ候補こうほ順次処理じゅんじしょりされるため、やがてされてつぎ頂点ちょうてん発見はっけんする。したがって、DFSとBFSはいずれも到達可能とうたつかのう頂点ちょうてんをちょうど一度いちどずつ訪問ほうもんする。

有限ゆうげんグラフでは各頂点かくちょうてん候補こうほ高々一度たかだかいちどしか追加ついかしないので、探索たんさくかなら停止ていしする。

7計算量けいさんりょう

隣接りんせつリストをもちいる場合ばあい各頂点かくちょうてん一度処理いちどしょりされ、無向むこうグラフの各辺かくへん両端りょうたんから高々一度たかだかいちどずつ調しらべられる。したがってDFSとBFSの時間計算量じかんけいさんりょう

O(|V|+|E|)

であり、訪問済ほうもんず集合しゅうごう候補こうほ必要ひつよう追加領域ついかりょういきO(|V|) である。

8使つか

  • 到達可能性とうたつかのうせい連結成分れんけつせいぶん列挙れっきょには、DFSとBFSのどちらも使用しようできる。
  • 無重むおも最短距離さいたんきょり最小手数さいしょうてすうにはBFSを使用しようする。
  • 再帰的さいきてき構造こうぞう調査ちょうさかえりがけの処理しょりふか候補こうほ優先ゆうせんにはDFSがてきする。
  • おも最短距離さいたんきょりにBFSをそのまま使用しようしてはならない。

9まとめ

data/exercise/information/algorithm/search/graph-traversal-and-unweighted-distance.exercise.n.md
  • DFSとBFSの本質的ほんしつてきは、候補こうほをスタックとキューのどちらで管理かんりするかにある。
  • 訪問済ほうもんず記録きろくは、循環じゅんかんによる非停止ひていし候補こうほ重複ちょうふくふせぐ。
  • 両者りょうしゃ到達可能とうたつかのう頂点ちょうてんをちょうど一度いちどずつ訪問ほうもんし、隣接りんせつリストでは O(|V|+|E|) 時間じかん動作どうさする。
  • 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.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
タブを全て閉じる