CSCI 311 - Fall 2006 - Program 3
Graphs - Dijkstra's Shortest Path Algorithm
Dr. Melody Stapleton, Instructor
Shaun McCluskey, Teaching Assistant
Due Date:
Shaun will provide the final input file(s) to run your program against by Friday, December 1.
******************************************************************************
Your assignment is to program the Dijkstra's shortest path algorithm in a fashion similar to that shown on page 587 of Kruse's Chapter 12 notes on graphs, but with some required modifications. The code on page 587 works with an adjacency table implementation of a digraph. You are to implement a digraph using the "mixed" implementation scheme that is depicted on page 576 in Figure 12.5 c). You will then need to modify the code on page 587 to work with the "mixed" implementation. You will also need to modify the algorithm on page 587 so that, given a source vertex, the code not only finds the shortest distance from that vertex to any other vertex in the graph (as the code does now), but it also stores the actual shortest path from that source vertex to any other vertex in the graph.
You are to create a class "digraph" that will have a number of methods. At the minimum it will have a constructor, destructor and a method to "read" in the relevant values to build the internal "mixed" representation of the graph. Your input file format is specified below. You will also need to build a method that "prints" the graph, and prints it in a way that looks like the picture on page 576 in Figure 12.5 c). I.e., you will print across a row, first the "from vertex" of an edge and then next to it print the list of "to" (or adjacent) vertices for that vertex. Your first row will be for the "from" vertex 0, second row for the "from" vertex 1, etc.
You should modify the name of the code on page 587 to be function set_path_and_distance. Add to this code the ability to store the actual shortest paths, as mentioned above. You will also need to change the code in 587, so that after it runs it prints out the shortest distances from the source to each of the other vertices in the graph, and it also prints out the list of vertices on that shortest path before the set_path_and_distance ends.
Your "read' method for the digraph class needs to process files in the following format:
*******************************************************************
8 // number of vertices in the graph (values from 0 to 7)
0 1 5 // specifies there is an edge from 0 to 1 of weight 5 in the graph
1 2 10 // specifies there is an edge from 1 to 2 of weight 10 in the graph
etc.
// blank line
3 // source vertex
// blank line
5 // source vertex
// blank line
7 // source vertex
<end-of-file marker>
*******************************************************************
As soon as you have read in and processed all the edges of the graph, you need to then call your "print" method to print out the graph as described above.
Next, process the requests (there may be more than one) to find the shortest distance and path between the source vertex in the input file and all other vertices. If there is no path from the source to a given vertex i , then simply print: "There is no path from <source> to <vertex i>". If there is a path print:
The distance from <source> to <vertex i> is <distance>
The shortest path is from <source> to <vertex i> is with hops:
<source> to <vertex j> of weight m,
<vertex k> to <vertex l> of weight n
etc,
or more specifically, for e.g. -
The distance from 0 to 6 is 24
The shortest path from 0 to 6 is with hops:
0 to 2 of weight 3
2 to 4 of weight 7
4 to 3 of weight 10
3 to 6 of weight 4 // Note how all these add up to 24!!
Once you have completed executing the method "set_path_and_distance" for this source vertex, test to see if you have another source to process or whether you are at the end of the input file.
You will have at least 2 such weighted digraph files to process with your program.