Graphs and traversal
導入済み
7:45
データ構造とアルゴリズム
This course is about choosing a container by what it costs. The first unit is the yardstick: how cost grows with the size of the input. The next four are the…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| adjacency list/əˈdʒeɪsənsi lɪst/ | 隣接リスト |
| adjacency matrix/əˈdʒeɪsənsi ˈmeɪtrɪks/ | 隣接行列 |
| sparse/spɑːs/ | Describing a graph with few edges compared with the number of vertices, so an adjacency list suits it. |
| Breadth-first search/bredθ fɜːst sɜːtʃ/ | graph search that visits all vertices one edge away, then two edges away, and so on, using a queue |
| Depth-first search/depθ fɜːst sɜːtʃ/ | graph search that follows one path as deep as it can before backing up, using a stack or recursion |
| Dijkstra's algorithm/ˈdeɪkstrəz ˈælɡərɪθəm/ | finds shortest paths from one source in a graph with non-negative edge weights by always settling the nearest unsettled vertex |
| topological sort/ˌtɒpəˈlɒdʒɪkl sɔːt/ | an order of a directed acyclic graph's vertices in which every edge points forwards |
| Kahn's algorithm/kɑːnz ˈælɡərɪθəm/ | A topological sort that repeatedly outputs a node with no incoming edges and removes its outgoing edges. |
| negative cycle | |
| weighted shortest paths |
リストと行列
- グラフ $G = (V, E)$:この講座のグラフには$V = \{A \ldots H\}$($8$個のノード)と$10$本の無向辺があります。
- 隣接リストは各ノードの隣接ノードを保存します。$10$本の無向辺に対して、$20$個の項目があります。隣接行列は$8 \times 8$の格子で、$64$個のセルがあり、辺があるところに$1$が入ります。

Ten undirected edges need — adjacency-list entries
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Each undirected edge is stored twice, once per endpoint.
なぜO(V+E)なのか
- どちらの探索も各ノードを一度ずつ訪問済みとして記録し($V$の処理)、保存された各辺を一度ずつ調べます($E$の処理)。この講座のグラフでは$O(V + E)$です。行列では、辺があるかどうかにかかわらず、各行のすべての列を読むため、計算量は$O(V^2)$です。
- 疎なグラフ($E \ll V^2$)ではリストが有利です。グラフが密な場合や、1つのセルを調べる場合は行列が有利です。ノードには必ず訪問済みの印を付けましょう。そうしないと、実際のグラフに閉路があると探索が永遠に繰り返されます。
BFS on an adjacency matrix costs —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Every row reads all V columns, edge or not, so the list is better for sparse graphs.
2つの探索
- $A$から幅優先探索(BFS)を行い、隣接ノードをアルファベット順に調べます。各層は$A \,|\, B, C \,|\, D, E \,|\, F, G \,|\, H$なので、探索順は$A, B, C, D, E, F, G, H$です。
- キューが探索の最前線を到着順に保つため、ノードの層は$A$からそのノードまでの最小辺数を表します。
- $A$から深さ優先探索(DFS)を行います。$A \to B \to D$、次に$D$の次の未訪問の隣接ノード$C$、次に$C \to E \to F \to H$、最後に$H$の隣接ノード$G$を訪れます。
- 探索順は$A, B, D, C, E, F, H, G$です。行き止まりでは戻りながら、深い方へ先に進みます。BFSは最小辺数を求め、DFSは経路や閉路を示します。
BFS from A on the course graph reads —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The queue visits layer by layer: A, then B C, then D E, then F G, then H.
重み付きの辺:ダイクストラ法
- BFSが数えるのは辺の数であり、距離ではありません。重みを$S\text{–}A = 4$、$S\text{–}B = 1$、$B\text{–}A = 2$、$A\text{–}T = 1$、$B\text{–}T = 5$とします。ダイクストラ法は、距離が確定していないノードのうち、最も近いものを毎回確定します。最短経路は$S, B, A, T$で、長さは4です。
| 確定するノード | $d(S)$ | $d(B)$ | $d(A)$ | $d(T)$ |
|---|---|---|---|---|
| $S$ | 0 | 1 | 4 | ∞ |
| $B$ | 0 | 1 | 3 | 6 |
| $A$ | 0 | 1 | 3 | 4 |
| $T$ | 0 | 1 | 3 | 4 |
- 各行では、確定していない距離のうち最小のもの(太字)を確定し、新しい経路の方が短い場合は、その隣接ノードの距離を更新します。
- 距離を管理する最小ヒープを使うと、計算量は$O((V + E) \log V)$です。この貪欲な選択が正しく機能するのは、重みが非負の場合だけです。負の重みの辺があると、すでに確定したノードの距離が小さくなることがあります。その場合はベルマン・フォード法を使います。
In the step's graph, the edge $A\text{–}T$ becomes $3$. What is the shortest distance from $S$ から $T$?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
A is still 3 via B, so T = min(1 + 5, 3 + 3) = 6: a tie between S, B, T and S, B, A, T.
Dijkstra's algorithm stays correct when one edge has a negative weight.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A settled node could later be reached more cheaply through the negative edge; Bellman-Ford handles that case.
DAGを順序付ける:トポロジカルソート
- 前提条件を表す有向非巡回グラフ(DAG)のノードは、すべての辺が前方を向くように並べることができます。これをトポロジカルソートといいます。カーンのアルゴリズムは、入次数が0のノードを出力し、そのノードから出る辺を削除する操作を繰り返します。
- $A \to B$、$A \to C$、$B \to D$、$C \to D$の場合、$A$、次に$B$と$C$、次に$D$を出力します。ノードが残っているのに入次数が0のものがなければ、グラフには閉路があります。計算量は$O(V + E)$です。
Kahn's algorithm stops with nodes left and none of in-degree 0. This means:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Every node left waits on another: a cycle.
例題:ダイクストラ法がうまくいかない場合
- 有向辺は$S \to A = 2$、$S \to B = 4$、$B \to A = -3$です。ダイクストラ法は$A$の距離を2で確定し、次に$B$の距離を4で確定します。辺$B \to A$を通ると距離は1になりますが、$A$の距離はすでに確定しています。
- ベルマン・フォード法はすべての辺について$V - 1 = 2$回、距離を更新する操作を行い、$d(A) = \mathbf{1}$を求めます。さらにもう1回行って距離が更新されるものがあれば、負の閉路があります。計算量は$O(VE)$です。
$S \to A = 2$, $S \to B = 4$, $B \to A = -3$: true shortest distance to $A$?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Via $B$: $4 - 3 = 1$.
Dijkstra's algorithm is correct as long as no cycle has negative total weight.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
One negative edge is enough to break it, as here; Bellman–Ford handles negative edges.
失点しやすいポイント
- 訪問済みの印を付け忘れる。 閉路があると、BFSやDFSが永遠に繰り返されます。
- 重み付きグラフの最短経路にBFSを使う。 BFSが数えるのは辺の数です。重み付きグラフの例では、最適な経路$S, B, A, T$の重みの合計は$4$ですが、3本の辺を通ります。
- リストの項目数を辺の数として数える。 各無向辺は両端のノードのリストに入ります。辺は$10$本、項目は$20$個です。
まとめ
- 隣接リスト:必要な記憶領域は$O(V + E)$で、疎なグラフに最適です。隣接行列:セル数は$V^2$で、辺の有無を調べる計算量は$O(1)$です。
- BFSは最小辺数を求め、DFSは経路や閉路を示します。どちらの計算量も$O(V + E)$です。
- ダイクストラ法には非負の重みが必要で、ヒープを使った場合の計算量は$O((V + E) \log V)$です。
何が問題でしたか?この内容はサイトの運営チームだけが確認します。