next up previous contents
Next: Minimum Spanning Tree: Intro Up: Graphs: Introduction Previous: Breadth-First Search: Introduction   Contents

Shortest Path: Introduction [179]




\begin{picture}(728,413)(6,406)
\thicklines\put(480,640){\circle{28}}
\put(560,7...
...ut(640,535){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 3}}}
\end{picture}

What is the shortest path from the ROOT (1) to every other node?



SP: Pseudocode [180]

Dijkstra's algorithm is a ``greedy'' algorithm.

#define ROOT 1
NODE v, w;
SET V, S;             /* both V and S are SETS          */
int Dist[MAX][MAX];   /* distance from node v to w      */
int D[MAX];           /* current distance from the root */
int P[MAX];           /* backward path towards root     */

S = {ROOT}; 
for (all other nodes w in V-S) {
  D[w] = Dist[ROOT][w];
  P[w] = ROOT;
}
while (S is not yet V) {            /* how many times will this iterate? */
  choose node w in V-S such that D[w] is minimum;
  S = S union {w};
  for (each node v in V-S)          /* how big can this be?              */
    if (D[v] > D[w] + Dist[w][v]) {
      D[v] = D[w] + Dist[w][v];
      P[v] = w;
    }
}
ANALYSIS: O(      )



SP: Example Execution [181]

init S={ROOT}
v S D P
1 1 0 0
2 0 2 1
3 0 infty 1
4 0 1 1
5 0 infty 1
6 0 infty 1
7 0 infty 1
add w=4 to S
v S D P
1 1 0 0
2 0 2 1
3 0 3 4
4 1 1 1
5 0 3 4
6 0 9 4
7 0 5 4
add w=2 to S
v S D P
1 1 0 0
2 1 2 1
3 0 3 4
4 1 1 1
5 0 3 4
6 0 9 4
7 0 5 4
add w=3 to S
v S D P
1 1 0 0
2 1 2 1
3 1 3 4
4 1 1 1
5 0 3 4
6 0 8 4
7 0 5 4

add w=5 to S
v S D P
1 1 0 0
2 1 2 1
3 1 3 4
4 1 1 1
5 1 3 4
6 0 8 3
7 0 5 4
add w=7 to S
v S D P
1 1 0 0
2 1 2 1
3 1 3 4
4 1 1 1
5 1 3 4
6 0 6 7
7 1 5 4
add w=6 to S
v S D P
1 1 0 0
2 1 2 1
3 1 3 4
4 1 1 1
5 1 3 4
6 1 6 7
7 1 5 4



SP: sp [182]

% gcc -o sp sp.c                                       > tcc sp.c

Data File with Test Case is Cost Matrix (99=infty): sp.dat

7
99  2 99  1 99 99 99
99 99 99  3 10 99 99
 4 99 99 99 99  5 99
99 99  2 99  2  8  4  
99 99 99 99 99 99  6
99 99 99 99 99 99 99
99 99 99 99 99  1 99

% sp sp.dat
n: 7 steps: 42  total distance of shortest paths: 20

% sp sp.dat graph
  0  2  0  1  0  0  0
  0  0  0  0  0  0  0
  0  0  0  0  0  0  0
  0  0  2  0  2  0  4
  0  0  0  0  0  0  0
  0  0  0  0  0  0  0
  0  0  0  0  0  1  0



SP: sp [183]

% sp sp.dat step 
 u  v step  dist   u  v step  dist   u  v step  dist 
 ---------------------------------------------------
 1  2    2 +       4  3   10 -       4  7   21 -      
 1  3    3 +       4  5   12 +       4  3   21 +   6      
 1  3    3 -       4  5   12 -       4  5   26 +      
 1  2    4 -       4  6   13 +       3  6   27 +      
 1  4    4 +       4  6   13 -       3  6   27 -      
 1  5    5 +       4  7   14 +       4  7   28 +      
 1  5    5 -       4  7   14 -       4  7   28 -      
 1  6    6 +       1  2   14 +   3   4  5   28 +   9      
 1  6    6 -       4  3   17 +       3  6   34 +      
 1  7    7 +       4  5   19 +       3  6   35 -      
 1  7    7 -       4  5   19 -       4  7   35 +      
 1  4    7 +   1   4  6   20 +       4  7   35 +  14      
 1  2    9 +       4  6   20 -       7  6   41 +      
 4  3   10 +       4  7   21 +       7  6   42 +  20



SP: sp.c [184]

#define ROOT 1
int A[MAX][MAX], B[MAX][MAX];
void SP() {
int i, j, w, v, min, dist=0, S[MAX], P[MAX], D[MAX];
  
  S[ROOT] = 1; D[ROOT] = 0;  P[ROOT] = 0;
  for (i=2; i<=n; i++) { S[i] = 0; D[i] = A[ROOT][i]; P[i] = ROOT; }
  for (i=2; i<=n; i++) {
    min = INFINITY; 
    w = 0;
    for (v=1; v<=n; v++) 
      if ((S[v] == 0) && (D[v] < min)) {
         min = D[v]; 
         w = v;
      }
    S[w] = 1; 
    dist += min;
    B[P[w]][w] = A[P[w]][w]; 
    for (v=1; v<=n; v++) 
      if ((S[v] == 0) && (D[v] > D[w]+A[w][v])) {
        D[v] = D[w] + A[w][v]; 
        P[v] = w; 
      }
  }
}


next up previous contents
Next: Minimum Spanning Tree: Intro Up: Graphs: Introduction Previous: Breadth-First Search: Introduction   Contents
Ted Billard 2001-10-25