Download as ps, pdf, or txt
Download as ps, pdf, or txt
You are on page 1of 1

AMS301 Finite Mathemati

al Stru tures A
Dijkstra's Shortest Path Algorithm

My presentation of Dijkstra's shortest path algorithm di ers from that of the textbook (Tu ker). Thus, I detail the
algorithm below. The algorithm an be thought of as propagating a \signal" from the root (sour e) vertex, and keeping
tra k at every other vertex of when the signal arrives for the rst time, given that it takes a ertain length of time for
the signal to travel along ea h edge.

Input
The input to the algorithm is a graph G = (V ; E ). We an assume, without loss of generality, that the verti es are
labeled with integers | V = f1; 2; : : : ; ng | and that our goal is to onstru t a tree of shortest paths rooted at vertex
1. Edges are labeled with \lengths" (\weights", \ osts") that are nonnegative; edge (i; j ) has length ij  0. (If there
is no edge (i; j ), then ij = +1.)

Output
The algorithm will onstru t a tree of shortest paths rooted at the vertex 1. In parti ular, ea h vertex i will have two
pie es of data asso iated with it: (1) a label ui that will be equal to the length of a shortest path from vertex 1 to vertex
i, and (2) a pointer predi that gives the vertex that is the prede essor to i in a shortest path from 1 to i. (In other

words, predi is the parent of i in the shortest path tree onstru ted. By following the pred pointers, one an tra e out
any shortest path from 1 to i.)

Algorithm
The algorithm keeps tra k of a partitioning of the vertex set V into two types of verti es: those that are \permanently
labeled" (the set P  V ), and those that are \temporarily labeled" (the set T  V ). To be \permanently labeled"
means that the value of ui is not going to hange ever again, sin e it is in fa t equal to the length of a shortest path
from 1 to i. To be \temporarily labeled" means that the value of ui may hange again (it may go down), so we do not
yet know if it equals the length of a shortest path from 1 to i. At the beginning of the algorithm, only the root (vertex
1) is permanently labeled. At the end of the algorithm, all verti es are permanently labeled.
(0) Initialization.

=0 u1

uj = 1j ; j = 2; 3; : : : ; n

predj = 1; j = 2; 3; : : : ; n; pred1 = NIL

P = f1g; T = f2; 3; : : : ; ng

(1) Assign a Permanent Label. Find k 2 T with the smallest label: uk = minj 2T uj . (Ties may be broken
arbitrarily, or by some tie-breaking rule, su h as pi king the lowest numbered vertex whenever there is a hoi e.)

T =T fg
k ; P =P [f g
k

If T = ;, STOP.
(2) Update Temporary Labels. For ea h j 2 T , do the following: If uk + kj < u j, then update uj :

u j u k + kj ; predj k

(Here, k is the vertex that was just assigned a permanent label in step (1).)
(Otherwise, do not hange uj or predj .)
Go to Step (1).
Theorem 1 For a graph with n verti es and e edges, having nonnegative edge lengths, Dijkstra's algorithm an be
implemented to run in time O e ( + n log n) (i.e., the running time is at most a onstant times e + n log n).
Proof. The proof of this laim requires some more advan ed knowledge of data stru tures. It is not hard, however, to
implement the algorithm to run in time O(n2 ) or in time O(e log n). u
t

You might also like