(Latest Revision: Nov 07, 2022)

CS 4440 Dijkstra's Algorithm Practice Problem


Some Background

Here is an an adjacency matrix that represents a directed graph, G1:
"from node"
numbers              "to node" numbers --> 
  
|            1       2       3       4       5
|  1         .      50      30     100      10
V  2         .       .       .       .       .
   3         .       5       .       .       .
   4         .      20      50       .       .
   5         .       .       .      10       .
Each number in the matrix represents an edge of G1. For example the "50" in row 4, column 3 represents an edge of G1 that starts at node 4 and goes to node 3.

Look here at how to perform Dijkstra's algorithm, using the adjacency matrix data above, to find the lengths of all the shortest paths from the source node (node #1) to the other nodes, as well as the paths themselves.

When you click on the link above, you see that tabular information just like this is created to solve the problem:
Part One of The Solution:
Step     v      C=V-S       BackPoint          Dist
                           1 2 3 4 5     1  2  3   4  5
Init    --    {2,3,4,5}   [1,1,1,1,1]   [0,50,30,100,10] 
 1       5     {2,3,4}    [1,1,1,5,1]   [0,50,30, 20,10]
 2       4      {2,3}     [1,4,1,5,1]   [0,40,30, 20,10]
 3       3       {2}      [1,3,1,5,1]   [0,35,30, 20,10]
The "Dist" information in the final row of the table tells us that the shortest paths from the start node to nodes 2, 3, 4, and 5, are of lengths 35, 30, 20, and 10. From the BackPoint table, we can also determine what all the actual shortest paths are from the start node to the other nodes. That works as follows.
Part Two of The Solution:

The back pointer of 2 is 3 and the back pointer of 3 is 1.  
Therefore the shortest path to 2 is 2 <== 3 <== 1. 

The back pointer of 3 is 1.  
Therefore the shortest path to 3 is 3 <== 1. 

The back pointer of 4 is 5, the back pointer of 5 is 1.
Therefore the shortest path to 4 is 4 <== 5 <== 1. 

The back pointer of 5 is 1.  
Therefore the shortest path to 5 is 5 <== 1. 
When you understand Dijkstra's Algorithm and the implementations we covered in class, you will be able to generate a tabular solution to the problem like the one above with relative ease, using only an adjacency matrix for your input.

There are some other presentations in the notes that explain the algorithm using "pictures." They are here and here, and there is pseudo-code for the algorithm here. (In the second link, the way the notation is done in the table is slightly different from how it's done above on this page, but the two are completely equivalent.)

The Assignment

Here is an adjacency matrix representing a different graph, G2.
"from"
node #	          "to" node #
        1     2     3     4     5     6     7
 1      .     .    15     .     .     .     9
 2      .     .     .     3    12     .     .
 3      .     .     .     9    21     .     .
 4      .     .     .     .    12     3     .
 5      .     .     .     .     .     .     .
 6      .     3     .     .     3     .     .
 7      .     6     3    15     .     .     .
Using the information about G2 above as an input to Dijkstra's algorithm, produce exactly the same kind of solution information for G2 as illustrated by parts one and two of the solution of the G1 example problem above.

Thus, you will execute Dijkstra's algorithm (by hand) on G2, you will find the lengths of all the shortest paths from node #1 in G2 to all the other nodes, and you will find the shortest paths themselves.

Note: The important thing here is that you get as close to possible to creating the output information correctly by using the adjacency matrix information. It's a way to show your understanding of the details of the algorithm.

I'm including below a diagram of G2. However, don't forget that your goal is to be able to execute the algorithm like a computer would - using only the adjacency matrix as input. It's quite possible that, on a quiz, you can get a problem like this, and there may not be a "picture" of the graph to go with it. You may be given only the adjacency matrix of a graph, or maybe an edge-list representation.


diagram of a directed graph



Directions For Submitting Solution: