ダイクストラ法の基本
algorithmgraphundergraduatelecture
1導入
この講義では、辺の重みが非負であるグラフについて、ダイクストラ法が単一始点最短距離を計算する方法と、その正当性を説明する。暫定距離、確定済み頂点、緩和を定義し、不変条件とカット論法によって証明する。
2問題設定
有限の有向または無向グラフ G=(V,E)、始点 s\in V、重み関数 w:E\to\mathbb{R}_{\ge 0} を考える。道の長さを、その辺の重みの総和とする。s から v への最短距離を \delta(s,v) と表し、到達不能なら \delta(s,v)=\infty とする。
data/lecture/information/algorithm/graph/shortest-path-basics.lecture.n.md
3用語
- 暫定距離 d[v] は、すでに発見した s から v への道のうち最小の長さである。未発見なら \infty とする。したがって、常に \delta(s,v)\le d[v] が成立する。
- 確定済み集合 S は、d[v]=\delta(s,v) であることを証明済みの頂点の集合である。この状態を v が 確定した という。
- 緩和 とは、辺 (u,v) に対して d[u]+w(u,v)<d[v] なら、d[v]\leftarrow d[u]+w(u,v) と更新する操作である。
- 優先度付きキュー は、未確定の候補から d が最小の頂点を取り出すデータ構造である。
4アルゴリズム
まず d[s]=0、v\ne s に対して d[v]=\infty、S=\varnothing とする。優先度付きキューには (0,s) を挿入する。その後、キューが空になるまで次を反復する。
- 距離値が最小の組 (x,u) を取り出す。
- u\in S または x\ne d[u] なら、この古い組を無視する。
- それ以外では u を S に加え、u から出る各辺 (u,v) のうち v\notin S であるものを緩和する。d[v] が減少したら、新しい (d[v],v) をキューに挿入する。
同じ頂点の古い距離値がキューに残る実装では、このような要素を stale entry とよぶ。2 の検査により、decrease-key 操作をもたないヒープでも正しく実装できる。
5不変条件
各反復の開始時に、次が成立する。
- u\in S なら d[u]=\delta(s,u) である。
- v\notin S の d[v] は、s から v への道で、v 以外の頂点が S に属するものの最小長である。v\ne s では、内部頂点が S に属し、最後の辺が S から v へ進む候補と同値である。そのような道がなければ d[v]=\infty である。
初期状態では、s への長さ 0 の道だけが候補なので、この条件が成立する。確定した u から出る辺をすべて緩和すると、S を内部に使用して新たに到達できる候補が過不足なく反映される。したがって 2 は保存される。
6確定操作の正当性
S の外部で d が最小の頂点を u とする。u=s なら d[s]=0=\delta(s,s) である。u\ne s が始点から到達可能であるとき、s から u への最短路 P を一つ選ぶ。P が S から V\setminus S へ最初に出る辺を (y,z) とする。この境界を横断する議論を カット論法 という。
P の s から y までの接頭部分の長さを c(P[s:y]) とする。y\in S なので、不変条件と (y,z) の緩和から
d[z]\le \delta(s,y)+w(y,z)\le c(P[s:y])+w(y,z).
重みがすべて非負であるため、P の z 以降を加えても長さは減少せず、
c(P[s:y])+w(y,z)\le c(P)=\delta(s,u)
が成立する。また u の選択から d[u]\le d[z] であり、暫定距離は実在する道の長さなので \delta(s,u)\le d[u] である。したがって、
\delta(s,u)\le d[u]\le d[z]\le \delta(s,u)
となり、d[u]=\delta(s,u) を得る。ゆえに u を確定しても不変条件 1 は保存される。
7到達不能な頂点
キューが空になったとき、s から到達可能な頂点はすべて確定済みである。到達不能な頂点はキューへ挿入されず、d[v]=\infty のまま残る。無限大の候補を取り出して確定する必要はない。
8負辺を許せない理由
s\to u の重みを 2、s\to v を 5、v\to u を -4 とする。ダイクストラ法は d[u]=2 を d[v]=5 より先に確定する。しかし、s\to v\to u の長さは 1 であり、真の最短距離は \delta(s,u)=1 である。負辺によってカットを横断した後に道の長さが減少するため、証明の非負性を使用した不等式が破綻する。
9計算量
隣接リストと二分ヒープを使用すると、有向辺は高々一度緩和される。無向辺は両端の隣接リストに現れるため、各方向から高々一度ずつ調査される。成功した緩和ごとに高々 1 個の組を挿入する。したがって、時間計算量は
O((|V|+|E|)\log |V|)
であり、始点からすべての頂点へ到達可能なら O(|E|\log |V|) と表記できる。stale entry を保持する実装の補助空間は O(|V|+|E|) である。decrease-key を使用する実装では、キューの要素数を O(|V|) に抑えられる。
10要点
- ダイクストラ法の前提は、すべての辺重みが非負であることである。
- 最小の暫定距離をもつ頂点を確定できる根拠は、不変条件とカット論法にある。
- stale entry、到達不能な頂点、使用するヒープの操作を実装仕様として明示する。
Dijkstra's Algorithm
1Introduction
This lecture explains how Dijkstra's algorithm computes single-source shortest-path distances in a graph with nonnegative edge weights. It defines tentative distances, settled vertices, and relaxation, then proves correctness through a loop invariant and a cut argument.
2Problem setting
Let G=(V,E) be a finite directed or undirected graph, let s\in V be the source, and let w:E\to\mathbb{R}_{\ge 0} be the weight function. The length of a path is the sum of its edge weights. Write \delta(s,v) for the shortest-path distance from s to v, with \delta(s,v)=\infty when v is unreachable from s.
data/lecture/information/algorithm/graph/shortest-path-basics.lecture.n.md
3Terminology
- The tentative distance d[v] is the minimum length among the currently discovered paths from s to v. It is \infty if no such path has been discovered. Consequently, \delta(s,v)\le d[v] always holds.
- The settled set S consists of vertices for which d[v]=\delta(s,v) has been proved. Such a vertex is called settled.
- To relax an edge (u,v) is to assign d[v]\leftarrow d[u]+w(u,v) whenever d[u]+w(u,v)<d[v].
- A priority queue extracts a vertex with minimum d among unsettled candidates.
4Algorithm
Initialize d[s]=0, d[v]=\infty for v\ne s, and S=\varnothing. Insert (0,s) into a priority queue. Repeat the following steps until the queue is empty.
- Extract a pair (x,u) with minimum distance key.
- If u\in S or x\ne d[u], discard this obsolete pair.
- Otherwise, add u to S and relax every outgoing edge (u,v) for which v\notin S. Whenever d[v] decreases, insert the new pair (d[v],v).
An implementation that leaves old distance keys in the queue can contain several entries for one vertex. Such an obsolete pair is a stale entry. The test in step 2 permits a correct heap implementation without a decrease-key operation.
5Loop invariant
At the beginning of every iteration, the following statements hold.
- For every u\in S, d[u]=\delta(s,u).
- For every v\notin S, d[v] is the minimum length of an s-to-v path whose vertices other than v belong to S. When v\ne s, this is equivalent to requiring that its internal vertices lie in S and its final edge cross from S to v. If no such path exists, then d[v]=\infty.
Initially, only the length-zero path to s is a candidate, so the invariant holds. Relaxing every outgoing edge of a newly settled vertex records exactly the new candidate paths made available by using vertices of S internally. Hence statement 2 is preserved.
6Correctness of settling
Let u be an unsettled vertex with minimum d. If u=s, then d[s]=0=\delta(s,s). Otherwise, suppose that u is reachable, and choose a shortest path P from s to u. Let (y,z) be the first edge of P that leaves S. Reasoning across this boundary is the cut argument.
Let c(P[s:y]) denote the length of the prefix of P from s to y. Because y\in S, the invariant and the relaxation of (y,z) imply
d[z]\le \delta(s,y)+w(y,z)\le c(P[s:y])+w(y,z).
All edge weights are nonnegative, so adding the suffix of P after z cannot decrease its length. Thus
c(P[s:y])+w(y,z)\le c(P)=\delta(s,u).
The choice of u gives d[u]\le d[z], while a tentative distance is the length of an actual path and therefore \delta(s,u)\le d[u]. Consequently,
\delta(s,u)\le d[u]\le d[z]\le \delta(s,u),
which proves d[u]=\delta(s,u). Settling u therefore preserves statement 1 of the invariant.
7Unreachable vertices
When the queue becomes empty, every vertex reachable from s has been settled. An unreachable vertex is never inserted, so its value remains d[v]=\infty. There is no need to extract or settle candidates whose keys are infinite.
8Why negative edges are forbidden
Consider edges s\to u of weight 2, s\to v of weight 5, and v\to u of weight -4. Dijkstra's algorithm settles u with d[u]=2 before v with d[v]=5. However, the path s\to v\to u has length 1, so the true distance is \delta(s,u)=1. A negative edge can decrease a path length after the path crosses the cut, invalidating the inequality in the proof that depends on nonnegativity.
9Complexity
With adjacency lists and a binary heap, each directed edge is relaxed at most once. An undirected edge occurs in the adjacency lists of both endpoints and is examined at most once in each direction. Each successful relaxation inserts at most one pair. The running time is
O((|V|+|E|)\log |V|),
which is commonly written as O(|E|\log |V|) when every vertex is reachable from the source. An implementation that retains stale entries uses O(|V|+|E|) auxiliary space. An implementation with decrease-key can keep the queue size at O(|V|).
10Summary
- Dijkstra's algorithm requires every edge weight to be nonnegative.
- The loop invariant and cut argument justify settling a vertex with minimum tentative distance.
- Stale entries, unreachable vertices, and the supported heap operations must be stated as part of the implementation contract.