JMU
The Shortest Path Problem
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


A Brief Review
A Problem Set on a Graph/Network
Label Setting Algorithms
Label Setting Algorithms (cont.)
Label Setting Algorithms (cont.)

More Detailed Pseudo-Code

    When we start out, all of the nodes have temporary status;

    i = O;

    while (i != D) {

      for (All nodes j that are reachable from i) {

        if (L[i] + d(i,j) < L[j]) {

          L[j] = L[i] + d(i,j);
          P[j] = i;
        }
      }

      M = infinity;

      for (All nodes j with S[j]==temporary) {

        if (L[j]<M) {

          M = L[j];
          n = j;
        }
      }


      S[n] = permanent;

      i =  n;
    }
    
An Example

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,-
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 1,4 12,4 8,4
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,-
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 17,3
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,-
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,-
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,- 2
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,- 2
4 9,2
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,- 2
4 9,2 17,3 8,4 *,-
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,- 2
4 9,2 17,3 8,4 *,- 7
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,- 2
4 9,2 17,3 8,4 *,- 7
5 15,7 16,7
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,- 2
4 9,2 17,3 8,4 *,- 7
5 9,2 15,7 16,7
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,- 2
4 9,2 17,3 8,4 *,- 7
5 9,2 15,7 16,7 5
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,- 2
4 9,2 17,3 8,4 *,- 7
5 9,2 15,7 16,7 5
6 13,5
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,- 2
4 9,2 17,3 8,4 *,- 7
5 9,2 15,7 16,7 5
6 15,7 13,5
An Example (cont.)

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Perm
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 12,4 *,- 8,4 *,- 3
2 3,3 *,- 12,4 17,3 8,4 *,- 1
3 4,1 12,4 17,3 8,4 *,- 2
4 9,2 17,3 8,4 *,- 7
5 9,2 15,7 16,7 5
6 15,7 13,5 8
Label Correcting Algorithms
Label Correcting Algorithms (cont.)
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- {4}/{3,5,7}/{3,5,7}
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- {4}/{3,5,7}/{3,5,7}
2 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {7}/{6,8}/{3,5,6,8}
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- {4}/{3,5,7}/{3,5,7}
2 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {7}/{6,8}/{3,5,6,8}
3 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {8}/{}/{3,5,6}
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- {4}/{3,5,7}/{3,5,7}
2 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {7}/{6,8}/{3,5,6,8}
3 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {8}/{}/{3,5,6}
4 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {6}/{}/{3,5}
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- {4}/{3,5,7}/{3,5,7}
2 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {7}/{6,8}/{3,5,6,8}
3 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {8}/{}/{3,5,6}
4 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {6}/{}/{3,5}
5 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {5}/{}/{3}
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- {4}/{3,5,7}/{3,5,7}
2 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {7}/{6,8}/{3,5,6,8}
3 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {8}/{}/{3,5,6}
4 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {6}/{}/{3,5}
5 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {5}/{}/{3}
6 3,3 *,- 1,4 0,- 12,4 15,7 8,4 16,7 {3}/{1}/{1}
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- {4}/{3,5,7}/{3,5,7}
2 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {7}/{6,8}/{3,5,6,8}
3 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {8}/{}/{3,5,6}
4 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {6}/{}/{3,5}
5 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {5}/{}/{3}
6 3,3 *,- 1,4 0,- 12,4 15,7 8,4 16,7 {3}/{1}/{1}
7 3,3 4,1 1,4 0,- 12,4 15,7 8,4 16,7 {1}/{2}/{2}
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- {4}/{3,5,7}/{3,5,7}
2 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {7}/{6,8}/{3,5,6,8}
3 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {8}/{}/{3,5,6}
4 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {6}/{}/{3,5}
5 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {5}/{}/{3}
6 3,3 *,- 1,4 0,- 12,4 15,7 8,4 16,7 {3}/{1}/{1}
7 3,3 4,1 1,4 0,- 12,4 15,7 8,4 16,7 {1}/{2}/{2}
8 3,3 4,1 1,4 0,- 9,2 15,7 8,4 16,7 {2}/{5}/{5}
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- {4}/{3,5,7}/{3,5,7}
2 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {7}/{6,8}/{3,5,6,8}
3 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {8}/{}/{3,5,6}
4 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {6}/{}/{3,5}
5 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {5}/{}/{3}
6 3,3 *,- 1,4 0,- 12,4 15,7 8,4 16,7 {3}/{1}/{1}
7 3,3 4,1 1,4 0,- 12,4 15,7 8,4 16,7 {1}/{2}/{2}
8 3,3 4,1 1,4 0,- 9,2 15,7 8,4 16,7 {2}/{5}/{5}
9 3,3 4,1 1,4 0,- 9,2 15,7 8,4 13,5 {5}/{8}/{8}
An Example

Find the shortest path from 4 to 8 in the following network using last-in, first-out candidate selection.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Out/In/Candidates
0 *,- *,- *,- 0,- *,- *,- *,- *,- {}/{4}/{4}
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- {4}/{3,5,7}/{3,5,7}
2 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {7}/{6,8}/{3,5,6,8}
3 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {8}/{}/{3,5,6}
4 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {6}/{}/{3,5}
5 *,- *,- 1,4 0,- 12,4 15,7 8,4 16,7 {5}/{}/{3}
6 3,3 *,- 1,4 0,- 12,4 15,7 8,4 16,7 {3}/{1}/{1}
7 3,3 4,1 1,4 0,- 12,4 15,7 8,4 16,7 {1}/{2}/{2}
8 3,3 4,1 1,4 0,- 9,2 15,7 8,4 16,7 {2}/{5}/{5}
9 3,3 4,1 1,4 0,- 9,2 15,7 8,4 13,5 {5}/{8}/{8}
10 3,3 4,1 1,4 0,- 9,2 15,7 8,4 13,5 {8}/{}/{}
The Bellman/Ford-Fulkerson/Moore Algorithm(s)
An Example

Find the shortest path from 4 to 8 in the following network.

images/network_8node12arc.gif
Iter 1's Label 2's Label 3's Label 4's Label 5's Label 6's Label 7's Label 8's Label Changed
0 *,- *,- *,- 0,- *,- *,- *,- *,- 4
1 *,- *,- 1,4 0,- 12,4 *,- 8,4 *,- 3,5,7
2 3,3 *,- 1,4 0,- 12,4 15,7 8,4 16,7 1,6,8
3 3,3 4,1 1,4 0,- 12,4 15,7 8,4 16,7 2
4 3,3 4,1 1,4 0,- 9,2 15,7 8,4 16,7 5
5 3,3 4,1 1,4 0,- 9,2 15,7 8,4 13,5 8
Nerd Humor
http://imgs.xkcd.com/comics/pillow_talk.jpg
(Courtesy of xkcd)
Things to Think About