markdown
Dijkstra's Algorithmmd 13801e5
lecture/information/algorithm/graph/dijkstra-algorithm-basics.lecture.n.md
Download PDF

Dijkstra's Algorithm

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

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
タブを全て閉じる