Graph optimisation
導入済み
5:37
アルゴリズム設計と解析
This course teaches you to price an algorithm's design before you trust it with real data. The first two units cover analysis tools and divide and conquer. The…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| cut/kʌt/ | a split of a graph's vertices into two groups |
| minimum spanning tree/ˈmɪnɪməm ˈspænɪŋ triː/ | a set of edges that connects all vertices with the smallest total weight and no cycle |
| cut property/kʌt ˈprɒpəti/ | the lightest edge crossing any cut belongs to some minimum spanning tree |
| Kruskal's algorithm/ˈkrʌsklz ˈælɡərɪθəm/ | builds a minimum spanning tree by adding the lightest edges that do not form a cycle |
| cycle/ˈsaɪkl/ | |
| Prim's algorithm/prɪmz ˈælɡərɪθəm/ | grows a minimum spanning tree from one vertex, always adding the lightest edge leaving the tree |
| union–find/ˈjuːnɪən faɪnd/ | a structure that tracks which items are in the same set and joins sets quickly |
| topological order/ˌtɒpəˈlɒdʒɪkl ˈɔːdə/ | an order of a directed acyclic graph's vertices in which every edge points forwards |
カットを横切る最軽量の辺
- カットは頂点を2つのグループに分けます。カットを横切る辺のうち、重みが最小の辺は、ある最小全域木(MST)に含まれます。カットが、すでに選んだ辺を横切らないように分けているとき、この辺を構築途中の全域森に加えても安全です。
- このカット性質は、クラスカル法(辺を重みの順に並べ、閉路を作らない限り各辺を採用する)と、下で説明するプリム法の根拠です。ダイクストラ法で頂点を確定するときも、「最も近いものを選んでも安全」という似た考え方を使いますが、対象は木の重みではなく距離です。

The light edge crossing a cut is —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Tree + light edge - cycle other edge keeps or lowers the weight.
この講座のグラフでクラスカル法を使う
- 重みの順に並べると、C–B 1、A–C 2、D–E 2、E–F 3、A–B 4、B–D 5、D–F 6、C–D 8、C–E 10です。C–B、A–C、D–E、E–F、B–Dを採用し、残りはそれぞれ閉路を作るので採用しません。
- 最小全域木の重みは$1+2+2+3+5 = 13$です。証明は次のとおりです。任意の木にカットを横切る最軽量の辺を加え、できた閉路からカットを横切るもう1本の辺を取り除くと、重みは変わらないか小さくなります。
Kruskal on the course graph yields MST weight —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
CB 1 + AC 2 + DE 2 + EF 3 + BD 5 = 13.
負の重みの辺:木では問題なく、ダイクストラ法では致命的
- ダイクストラ法は一度確定した頂点を再び調べません。A→B (2)、A→C (5)、C→B (−4)では、Cより先にBの距離を2で確定してしまい、長さ1の経路A→C→Bを見逃します。頂点を安全に確定できるのは、後から調べる辺によって経路が短くならない場合だけです。そのためには、辺の重みが負でない必要があります。
- 負の重みの辺を含む最短経路には、ベルマン–フォード法を使います。すべての辺を更新する操作を$V - 1$回繰り返し、計算量は$\Theta(VE)$で、負の閉路も検出できます。最小全域木は事情が異なります。クラスカル法とプリム法は重みを比較するだけなので、負の重みがあっても使えます。
Kruskal's algorithm still finds a minimum spanning tree when some edge weights are negative.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Kruskal only sorts and compares weights, and the cut property holds for any weights. It is Dijkstra's shortest-path settling that breaks with negative edges.
With directed edges A→B 2, A→C 5, C→B −4, Dijkstra from A settles B at —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
B (2) is nearer than C (5), so B is settled first, at 2. The path A→C→B of length 1 is found too late; use Bellman-Ford.
Dijkstra's algorithm finds correct shortest paths whenever the graph has no negative cycle, even if some edges are negative.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Dijkstra settles a node for good, so a negative edge found later cannot correct it: on A→B (2), A→C (5), C→B (−4) it settles B at $2$ and misses the path of length $1$ through C. Bellman-Ford handles negative edges.
プリム法と高速な閉路判定
- プリム法は開始頂点から1本の木を成長させ、常にその木から外へ出る辺のうち最軽量のものを加えます。この講座のグラフでAから始めると、A–C $2$、C–B $1$、B–D $5$、D–E $2$、E–F $3$の順に選びます。合計は$13$で、カット性質が保証するとおり、クラスカル法と同じ木になります。
- クラスカル法の閉路判定には素集合データ構造を使います。各連結成分には根があり、辺の両端の根が同じ場合はその辺を採用しません。ランクによる併合と経路圧縮を使うと、各操作にかかる時間はほぼ一定になるため、クラスカル法の計算量は辺の並べ替えで決まり、$O(E \log E)$になります。
How many edges does a minimum spanning tree of a connected 12-vertex graph have?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Every spanning tree on V vertices has V - 1 edges.
Prim starts at A on the course graph (A–B 4, A–C 2, ...). Which edge does it add first?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Only edges leaving the tree {A} count, and A–C is the lightest of them.
まずトポロジカル順序を求める
- 有向非巡回グラフのトポロジカル順序では、すべての頂点を、その頂点から辺が向かう先の頂点より前に並べます。履修の前提条件やビルドの手順などに使えます。カーンのアルゴリズムは、入ってくる辺がない頂点を繰り返し取り除きます。計算量は$\Theta(V + E)$です。
- 頂点が残っているのに、入ってくる辺がない頂点が1つもなければ、グラフに閉路があり、トポロジカル順序は存在しません。
A directed graph containing a cycle has a topological order.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Every vertex on the cycle would have to come before itself.
例題:有向非巡回グラフの最短経路
- 辺はA→B 3、A→C 6、B→C −2、B→D 4、C→D 1です。トポロジカル順序A、B、C、Dに従って距離を更新すると、B = 3、C = $\min(6, 3 - 2) = 1$、D = $\min(3 + 4, 1 + 1) = \mathbf{2}$になります。
- 1回の走査で済み、計算量は$\Theta(V + E)$です。各頂点の距離は、その頂点から出る辺を使う前に確定するので、負の重みの辺があっても問題ありません。一般のグラフですべての頂点対の最短経路を求めるフロイド–ワーシャル法の計算量は$\Theta(V^3)$で、頂点が100個の場合は$10^6$回の更新が必要です。
DAG A→B 3, A→C 6, B→C −2, B→D 4, C→D 1: shortest distance A to D?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
A→B→C→D: $3 - 2 + 1 = 2$.
Floyd–Warshall on 100 vertices: how many distance updates ($V^3$)?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
$100^3 = 10^6$.
点を落としやすいところ
- クラスカル法の閉路を目で確認する。 素集合データ構造を使いましょう。両端がすでに同じ集合にある辺は閉路を作るので、採用しません。
- 最小全域木を最短経路木として使う。 最小全域木が最小にするのは重みの合計であり、2つの頂点間の距離ではありません。
- プリム法の開始頂点によって重みの合計が変わると考える。 どの頂点から始めても、得られる木の重みの合計は同じです。この講座のグラフでは$13$です。
まとめ
- カット性質:任意のカットを横切る最軽量の辺は、安全に選べます。
- この講座の最小全域木:C–B、A–C、D–E、E–F、B–D。重みは$13$です。
- 重み付きグラフに関する2つの異なる問題:
| 最短経路 | 最小全域木 | |
|---|---|---|
| 求めるもの | 1つの始点からの最も重みが小さい経路 | すべての頂点をつなぐ、重みの合計が最小の辺の集合 |
| アルゴリズム | ダイクストラ法、ベルマン–フォード法 | クラスカル法、プリム法 |
| 負の重み | ダイクストラ法は使えない。ベルマン–フォード法を使う | 問題なし |
何が問題でしたか?この内容はサイトの運営チームだけが確認します。