Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

Dijkstra's Algorithm

 

Dijkstra's algorithm  is a greedy algorithm for solving the single-source, shortest-path problem on an edge-weighted graph in which all the weights are non-negative. It finds the shortest paths from some initial vertex, say tex2html_wrap_inline71118, to all the other vertices one-by-one. The essential feature of Dijkstra's algorithm is the order in which the paths are determined: The paths are discovered in the order of their weighted lengths, starting with the shortest, proceeding to the longest.

For each vertex v, Dijkstra's algorithm keeps track of three pieces of information, tex2html_wrap_inline71254, tex2html_wrap_inline71256, and tex2html_wrap_inline71258:

tex2html_wrap_inline71254
The bool-valued flag tex2html_wrap_inline71254 indicates that the shortest path to vertex v is known. Initially, tex2html_wrap_inline71266 for all tex2html_wrap_inline71268.
tex2html_wrap_inline71256
The quantity tex2html_wrap_inline71256 is the length of the shortest known path from tex2html_wrap_inline71118 to v. When the algorithm begins, no shortest paths are known. The distance tex2html_wrap_inline71256 is a tentative distance. During the course of the algorithm candidate paths are examined and the tentative distances are modified.

Initially, tex2html_wrap_inline71280 for all tex2html_wrap_inline71268 such that tex2html_wrap_inline71284, while tex2html_wrap_inline71286.

tex2html_wrap_inline71258
The predecessor of vertex v on the shortest path from tex2html_wrap_inline71118 to v. That is, the shortest path from tex2html_wrap_inline71118 to v has the form tex2html_wrap_inline71300.

Initially, tex2html_wrap_inline71258 is unknown for all tex2html_wrap_inline71268.

Dijkstra's algorithm proceeds in phases. The following steps are performed in each pass:

  1. From the set of vertices for with tex2html_wrap_inline71266, select the vertex v having the smallest tentative distance tex2html_wrap_inline71256.
  2. Set tex2html_wrap_inline71312.
  3. For each vertex w adjacent to v for which tex2html_wrap_inline71318, test whether the tentative distance tex2html_wrap_inline71320 is greater than tex2html_wrap_inline71322. If it is, set tex2html_wrap_inline71324 and set tex2html_wrap_inline71326.
In each pass exactly one vertex has its tex2html_wrap_inline71254 set to true. The algorithm terminates after tex2html_wrap_inline70678 passes are completed at which time all the shortest paths are known.

Table gif illustrates the operation of Dijkstra's algorithm as it finds the shortest paths starting from vertex b in graph tex2html_wrap_inline71202 shown in Figure gif.

 

 

passes

vertex

initially 1 2 3 4 5 6
a tex2html_wrap_inline68188 3 b tex2html_wrap_inline71342 3 b tex2html_wrap_inline71342 3 b tex2html_wrap_inline71342 3 b tex2html_wrap_inline71342 3 b tex2html_wrap_inline71342 3 b
b 0 -- tex2html_wrap_inline71342 0 -- tex2html_wrap_inline71342 0 -- tex2html_wrap_inline71342 0 -- tex2html_wrap_inline71342 0 -- tex2html_wrap_inline71342 0 -- tex2html_wrap_inline71342 0 --
c tex2html_wrap_inline68188 5 b 4 a tex2html_wrap_inline71342 4 a tex2html_wrap_inline71342 4 a tex2html_wrap_inline71342 4 a tex2html_wrap_inline71342 4 a
d tex2html_wrap_inline68188 tex2html_wrap_inline68188 tex2html_wrap_inline68188 6 c tex2html_wrap_inline71342 6 c tex2html_wrap_inline71342 6 c tex2html_wrap_inline71342 6 c
e tex2html_wrap_inline68188 tex2html_wrap_inline68188 tex2html_wrap_inline68188 8 c 8 c tex2html_wrap_inline71342 8 c tex2html_wrap_inline71342 8 c
f tex2html_wrap_inline68188 tex2html_wrap_inline68188 tex2html_wrap_inline68188 tex2html_wrap_inline68188 11 d 9 e tex2html_wrap_inline713429e
Table: Operation of Dijkstra's algorithm.

Initially all the tentative distances are tex2html_wrap_inline68188, except for vertex b which has tentative distance zero. Therefore, vertex b is selected in the first pass. The mark tex2html_wrap_inline71342 beside an entry in Table gif indicates that the shortest path is known ( tex2html_wrap_inline71470).

Next we follow the edges emanating from vertex b, tex2html_wrap_inline71474 and tex2html_wrap_inline71476, and update the distances accordingly. The new tentative distances for a becomes 3 and the new tentative distance for c is 5. In both cases, the next-to-last vertex on the shortest path is vertex b.

In the second pass, vertex a is selected and its entry is marked with tex2html_wrap_inline71342 indicating the shortest path is known. There is one edge emanating from a, tex2html_wrap_inline71494. The distance to c via a is 4. Since this is less than the tentative distance to c, vertex c is given the new tentative distance 4 and its predecessor on the shortest-path is set to a. The algorithm continues in this fashion for a total of tex2html_wrap_inline70254 passes until all the shortest paths have been found.

The shortest-path information contained in the right-most column of Table gif can be represented in the form of a vertex-weighted graph as shown in Figure gif.

   figure51318
Figure: The shortest-path graph for tex2html_wrap_inline71202.

This graph contains the same set of vertices as the problem graph tex2html_wrap_inline71202. Each vertex v is labeled with the length tex2html_wrap_inline71256 of the shortest path from b to v. Each vertex (except b) has a single emanating edge that connects the vertex to the next-to-last vertex on the shortest-path. By following the edges in this graph from any vertex v to vertex b, we can construct the shortest path from b to v in reverse.


next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.