markdown
ダイクストラ法の基本md 13801e5
lecture/information/algorithm/graph/dijkstra-algorithm-basics.lecture.n.md
Download PDF

ダイクストラほう基本きほん

date2026-07-14document_iddoc_68ebd8de946186fb80b167dcbafb01e5descriptionダイクストラ法を、非負重み、暫定距離、確定済み集合、緩和、不変条件、優先度付きキューの観点から説明する。prerequisites最短路の基本 / グラフの基本 / 計算量の基本 / ヒープと木構造type講義statusactiverelateddata/lecture/information/algorithm/graph/graph-algorithms-portal.lecture.n.md / data/lecture/information/algorithm/graph/shortest-path-basics.lecture.n.md / data/lecture/information/algorithm/data-structures/tree-and-heap.lecture.n.md
algorithmgraphundergraduatelecture

1導入どうにゅう

この講義こうぎでは、へんおもみが非負ひふであるグラフについて、ダイクストラほう単一始点最短距離たんいつしてんさいたんきょり計算けいさんする方法ほうほうと、その正当性せいとうせい説明せつめいする。暫定距離ざんていきょり確定済かくていず頂点ちょうてん緩和かんわ定義ていぎし、不変条件ふへんじょうけんとカット論法ろんぽうによって証明しょうめいする。

2問題設定もんだいせってい

有限ゆうげん有向ゆうこうまたは無向むこうグラフ G=(V,E)始点してん sVおも関数かんすう w:ER[PARSE ERROR: Undefined("Command(\"ge\")")]0かんがえる。みちながさを、そのへんおもみの総和そうわとする。s から v への最短距離さいたんきょりδ(s,v)あらわし、到達不能とうたつふのうなら δ(s,v)= とする。

data/lecture/information/algorithm/graph/shortest-path-basics.lecture.n.md

3用語ようご

  • 暫定距離ざんていきょり d[v] は、すでに発見はっけんした s から v へのみちのうち最小さいしょうながさである。未発見みはっけんなら とする。したがって、つねδ(s,v)[PARSE ERROR: Undefined("Command(\"le\")")]d[v]成立せいりつする。
  • 確定済かくていず集合しゅうごう S は、d[v]=δ(s,v) であることを証明済しょうめいずみの頂点ちょうてん集合しゅうごうである。この状態じょうたいv確定かくていした という。
  • 緩和かんわ とは、へん (u,v)たいして d[u]+w(u,v)<d[v] なら、d[v]d[u]+w(u,v)更新こうしんする操作そうさである。
  • 優先度付ゆうせんどつきキュー は、未確定みかくてい候補こうほから d最小さいしょう頂点ちょうてんすデータ構造こうぞうである。

4アルゴリズム

まず d[s]=0vsたいして d[v]=S=[PARSE ERROR: Undefined("Command(\"varnothing\")")] とする。優先度付ゆうせんどつきキューには (0,s)挿入そうにゅうする。その、キューがからになるまでつぎ反復はんぷくする。

  1. 距離値きょりち最小さいしょうくみ (x,u)す。
  2. uS または xd[u] なら、このふるくみ無視むしする。
  3. それ以外いがいでは uSくわえ、u から各辺かくへん (u,v) のうち vS であるものを緩和かんわする。d[v]減少げんしょうしたら、あたらしい (d[v],v) をキューに挿入そうにゅうする。

おな頂点ちょうてんふる距離値きょりちがキューにのこ実装じっそうでは、このような要素ようそstale entry とよぶ。2 の検査けんさにより、decrease-key 操作そうさをもたないヒープでもただしく実装じっそうできる。

5不変条件ふへんじょうけん

各反復かくはんぷく開始時かいしじに、つぎ成立せいりつする。

  1. uS なら d[u]=δ(s,u) である。
  2. vSd[v] は、s から v へのみちで、v 以外いがい頂点ちょうてんSぞくするものの最小長さいしょうちょうである。vs では、内部頂点ないぶちょうてんSぞくし、最後さいごへんS から vすす候補こうほ同値どうちである。そのようなみちがなければ d[v]= である。

初期状態しょきじょうたいでは、s へのながさ 0 のみちだけが候補こうほなので、この条件じょうけん成立せいりつする。確定かくていした u からへんをすべて緩和かんわすると、S内部ないぶ使用しようしてあらたに到達とうたつできる候補こうほ過不足かぶそくなく反映はんえいされる。したがって 2 は保存ほぞんされる。

6確定操作かくていそうさ正当性せいとうせい

S外部がいぶd最小さいしょう頂点ちょうてんu とする。u=s なら d[s]=0=δ(s,s) である。us始点してんから到達可能とうたつかのうであるとき、s から u への最短路さいたんろ Pひとえらぶ。PS から VS最初さいしょへん(y,z) とする。この境界きょうかい横断おうだんする議論ぎろんカット論法ろんぽう という。

Ps から y までの接頭部分せっとうぶぶんながさを c(P[s:y]) とする。yS なので、不変条件ふへんじょうけん(y,z)緩和かんわから

d[z][PARSE ERROR: Undefined("Command(\"le\")")]δ(s,y)+w(y,z)[PARSE ERROR: Undefined("Command(\"le\")")]c(P[s:y])+w(y,z).

おもみがすべて非負ひふであるため、Pz 以降いこうくわえてもながさは減少げんしょうせず、

c(P[s:y])+w(y,z)[PARSE ERROR: Undefined("Command(\"le\")")]c(P)=δ(s,u)

成立せいりつする。また u選択せんたくから d[u][PARSE ERROR: Undefined("Command(\"le\")")]d[z] であり、暫定距離ざんていきょり実在じつざいするみちながさなので δ(s,u)[PARSE ERROR: Undefined("Command(\"le\")")]d[u] である。したがって、

δ(s,u)[PARSE ERROR: Undefined("Command(\"le\")")]d[u][PARSE ERROR: Undefined("Command(\"le\")")]d[z][PARSE ERROR: Undefined("Command(\"le\")")]δ(s,u)

となり、d[u]=δ(s,u)る。ゆえに u確定かくていしても不変条件ふへんじょうけん 1 は保存ほぞんされる。

7到達不能とうたつふのう頂点ちょうてん

キューがからになったとき、s から到達可能とうたつかのう頂点ちょうてんはすべて確定済かくていずみである。到達不能とうたつふのう頂点ちょうてんはキューへ挿入そうにゅうされず、d[v]= のままのこる。無限大むげんだい候補こうほして確定かくていする必要ひつようはない。

8負辺ふへんゆるせない理由りゆう

suおもみを 2、sv を 5、vu を -4 とする。ダイクストラほうd[u]=2d[v]=5 よりさき確定かくていする。しかし、svuながさは 1 であり、しん最短距離さいたんきょりδ(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 sV be the source, and let w:ER[PARSE ERROR: Undefined("Command(\"ge\")")]0 be the weight function. The length of a path is the sum of its edge weights. Write δ(s,v) for the shortest-path distance from s to v, with δ(s,v)= 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 if no such path has been discovered. Consequently, δ(s,v)[PARSE ERROR: Undefined("Command(\"le\")")]d[v] always holds.
  • The settled set S consists of vertices for which d[v]=δ(s,v) has been proved. Such a vertex is called settled.
  • To relax an edge (u,v) is to assign d[v]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]= for vs, and S=[PARSE ERROR: Undefined("Command(\"varnothing\")")]. Insert (0,s) into a priority queue. Repeat the following steps until the queue is empty.

  1. Extract a pair (x,u) with minimum distance key.
  2. If uS or xd[u], discard this obsolete pair.
  3. Otherwise, add u to S and relax every outgoing edge (u,v) for which vS. 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.

  1. For every uS, d[u]=δ(s,u).
  2. For every vS, d[v] is the minimum length of an s-to-v path whose vertices other than v belong to S. When vs, 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]=.

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=δ(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 yS, the invariant and the relaxation of (y,z) imply

d[z][PARSE ERROR: Undefined("Command(\"le\")")]δ(s,y)+w(y,z)[PARSE ERROR: Undefined("Command(\"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)[PARSE ERROR: Undefined("Command(\"le\")")]c(P)=δ(s,u).

The choice of u gives d[u][PARSE ERROR: Undefined("Command(\"le\")")]d[z], while a tentative distance is the length of an actual path and therefore δ(s,u)[PARSE ERROR: Undefined("Command(\"le\")")]d[u]. Consequently,

δ(s,u)[PARSE ERROR: Undefined("Command(\"le\")")]d[u][PARSE ERROR: Undefined("Command(\"le\")")]d[z][PARSE ERROR: Undefined("Command(\"le\")")]δ(s,u),

which proves d[u]=δ(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]=. There is no need to extract or settle candidates whose keys are infinite.

8Why negative edges are forbidden

Consider edges su of weight 2, sv of weight 5, and vu of weight -4. Dijkstra's algorithm settles u with d[u]=2 before v with d[v]=5. However, the path svu has length 1, so the true distance is δ(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.
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
タブを全て閉じる