nextupprevious
Next:7.2.2 Dynamic Programming AlgorithmUp:7.2 Shortest Paths ProblemPrevious:7.2 Shortest Paths Problem

7.2.1 Single Source Shortest Paths Problem: Dijkstra's Algorithm

REF.
E.W. Dijkstra. A note on two problems in connection with graphs. Numerische Mathematik, Volume 1, pp. 269-271, 1959.
Let
V = {1, 2,..., n} and source = 1 (7.1)
C[i, j] = $\displaystyle \mbox{Cost of the arc $(i, j)$\space if the arc $(i,j)$\space exists;otherwise $\infty$ }$  

{

        S= { 1 };

            for (i = 2; i < n; i++)

                D[i] = C[1,i];

            for (i=1; i < = n-1; i++)

    {

                choose a vertex w $ \in$ V-S such that D[w] is a minimum;

                S = S $ \cup$ {w };

                for each vertex v $ \in$ V-S

                     D[v] = min (D[v], D[w] + C[w, v])

    }

}


Theorem: Dijkstra's algorithm finds the shortest paths from a single source to all other nodes of a weighted digraph with positive weights.

Proof: Let V = 1, 2, ..., n and 1 be the source vertex. We use mathematical induction to show that

(a)
If a node i$ \neq$ 1) $ \in$S, then D[i] gives the length of the shortest path from the source to i.
(b)
if a node i$ \notin$S, then D[i] gives the length of the shortest special path from the source to i.
Basis: Initially S = 1 and hence (a) is vacuously true. For i$ \in$S, the only special path from the source is the direct edge if present from source to i and D is initialized accordingly. Hence (b) is also true.

Induction for condition (a)

Induction step for condition (b): Let $ \omega$$ \neq$v and $ \omega$$ \in$S. When v is added to S, these are two possibilities for the shortest special path from source to w:
1.
It remains as before
2.
It now passes through v ( and possibly other nodes in S)
In the first case, there is nothing to prove. In the second case, let y be the last node of S visited before arriving at w. The length of such a path is D[y] + C[y,w]. When the algorithm stops, all the nodes but one are in S and it is clear that the vector D[1], D[2], ..., D[n]) contains the lengths of the shortest paths from source to respective vertices.

Example: Consider the digraph in Figure 7.5.
 

Figure 7.5: A digraph example for Dijkstra's algorithm
\begin{figure}\centerline{\psfig{figure=figures/Fdijk2.ps}}\end{figure}

Initially:

S = {1}  D[2] = 10  D[3] = $\displaystyle \infty$ D[4] = 30  D[5] = 100

Iteration 1

Select w = 2, so that S = {1, 2}

D[3] = min($\displaystyle \infty$, D[2] + C[2, 3]) = 60 (7.2)
D[4] = min(30, D[2] + C[2, 4]) = 30 (7.3)
D[5] = min(100, D[2] + C[2, 5]) = 100  

Iteration 2

Select w = 4, so that S = {1, 2, 4}

D[3] = min(60, D[4] + C[4, 3]) = 50 (7.4)
D[5] = min(100, D[4] + C[4, 5]) = 90  

Iteration 3

Select w = 3, so that S = {1, 2, 4, 3}

D[5] = min(90, D[3] + C[3, 5]) = 60  

Iteration 4

Select w = 5, so that S = {1, 2, 4, 3, 5}

D[2] = 10 (7.5)
D[3] = 50 (7.6)
D[4] = 30 (7.7)
D[5] = 60  

Complexity of Dijkstra's Algorithm

With adjacency matrix representation, the running time is O(n2) By using an adjacency list representation and a partially ordered tree data structure for organizing the set V - S, the complexity can be shown to be

O(elog n)
where e is the number of edges and n is the number of vertices in the digraph.


nextupprevious
Next:7.2.2 Dynamic Programming AlgorithmUp:7.2 Shortest Paths ProblemPrevious:7.2 Shortest Paths Problem
eEL,CSA_Dept,IISc,Bangalore