Skip to content

Shortest Path Algorithm

Definition

For nonnegative edge lengths, the course's shortest path algorithm sets d(s)=0d(s)=0 and U={s}U=\{s\}. Repeatedly choose an edge u−vu-v with u∈Uu\in U, v∉Uv\notin U minimizing

d(u)+ℓuv.d(u)+\ell_{uv}.

Set d(v)d(v) equal to this value, record predecessor uu, and add vv to UU. Stop when the destination is added, or continue to obtain distances to all vertices.


Interpretation

Vertices enter the settled set UU in order of their shortest distance from the origin ss. A label d(v)d(v) is final as soon as vv enters UU, and the recorded predecessors recover the actual route after the distance has been computed.

A weighted network from A to F with each vertex labelled by its shortest distance from A, from d(A) = 0 to d(F) = 13, and the shortest-path tree highlighted.

The labels give the shortest distance from AA to each vertex; the vertices are settled in the order A,C,B,D,E,FA,C,B,D,E,F, with d=0,2,3,8,10,13d=0,2,3,8,10,13. The label of BB is 33, via CC, not 44 via the direct edge: once d(C)=2d(C)=2 is known, the edge C−BC-B offers 2+1=32+1=3. The red edges are the recorded predecessor edges, and following them back from FF gives the route A−C−B−D−E−FA-C-B-D-E-F of the shortest path problem.

The settled order is correct because lengths are nonnegative. Any other route to the newly chosen vertex vv must leave UU through some edge, so it already costs at least the smallest candidate value d(v)d(v), and further nonnegative edges cannot make it shorter. With a negative length this breaks down: if A−BA-B has length 2, A−CA-C length 3 and C−BC-B length −2-2, the algorithm settles BB with d(B)=2d(B)=2, yet the route A−C−BA-C-B has length 1.

The Dijkstra-Prim method for minimum spanning trees grows its set UU in the same way but compares only the edge length ℓuv\ell_{uv}, not d(u)+ℓuvd(u)+\ell_{uv}. In the figure both rules happen to select the same edges, but in general a shortest-path tree and a minimum spanning tree differ.


Key Results

  • Compare complete distances from the origin, not just individual edge lengths.
  • Ties can be broken arbitrarily when one shortest path is sufficient.
  • Backtrack from the destination through predecessors and reverse the sequence.

Examples

The figure settles vertices in the order A,C,B,D,E,FA,C,B,D,E,F. The recorded predecessor edges form a shortest-path tree rooted at AA.

  • In the course's worked example with origin SS and destination TT (a different network from the figure), the labels are d(B)=2d(B)=2, d(A)=3d(A)=3, d(D)=6d(D)=6, d(C)=7d(C)=7, d(E)=8d(E)=8, d(T)=12d(T)=12.
  • The predecessor chain T,E,D,A,B,ST,E,D,A,B,S recovers the route of length 12.

Prerequisites

Sources

Aliases: Dijkstra's shortest path algorithm Symbols: element of

Last updated: 27 October 2026 11:52