CSCI 311 - Program 3
Graphs - Dijkstra's
Shortest Path Algorithm
Dr. Melody
Stapleton, Instructor
Bonnie Varghese -
Teaching Assistant
Due Date:
Bonnie will provide
the final input file(s) to run your program against.
******************************************************************************
Your assignment is
to program the Dijkstra's shortest path algorithm in
a fashion similar to that shown on page 470 of Kruse's Chapter 12 notes on
graphs, but with some required modifications. The code on page 470 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 459 in Figure c). You will then need to modify the code on page 470
to work with the "mixed" implementation. You will also need to
modify the algorithm on page 470 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 459, Figure 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 470 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 470, 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.
Here is a complete
e.g. of an input file and the corresponding output that should accompany this
file:
Input:
7
0 1 15
0 2 20
1 3 10
1 4 25
2 3 15
2 5 20
3 4 20
3 5 15
3 6 30
4 6 10
5 6 20
0
1
3
4
5
6
Output:
Graph:
vertex[0]->1,15->2,20->NULL
vertex[1]->3,10->4,25->NULL
vertex[2]->3,15->5,20->NULL
vertex[3]->4,20->5,15->6,30->NULL
vertex[4]->6,10->NULL
vertex[5]->6,20->NULL
vertex[6]->NULL
source: 0
The distance from 0
to 1 is 15
The shortest path
from 0 to 1 is with hops:
0 to 1 of weight 15
The distance from 0
to 2 is 20
The shortest path
from 0 to 2 is with hops:
0 to 2 of weight 20
The distance from 0
to 3 is 25
The shortest path
from 0 to 3 is with hops:
0 to 1 of weight 15
1 to 3 of weight 10
The distance from 0
to 4 is 40
The shortest path
from 0 to 4 is with hops:
0 to 1 of weight 15
1 to 4 of weight 25
The distance from 0
to 5 is 40
The shortest path
from 0 to 5 is with hops:
0 to 2 of weight 20
2 to 5 of weight 20
The distance from 0
to 6 is 50
The shortest path
from 0 to 6 is with hops:
0 to 1 of weight 15
1 to 4 of weight 25
4 to 6 of weight 10
source: 1
No path from 1 to 0
No path from 1 to 2
The distance from 1
to 3 is 10
The shortest path
from 1 to 3 is with hops:
1 to 3 of weight 10
The distance from 1
to 4 is 25
The shortest path
from 1 to 4 is with hops:
1 to 4 of weight 25
The distance from 1
to 5 is 25
The shortest path
from 1 to 5 is with hops:
1 to 3 of weight 10
3 to 5 of weight 15
The distance from 1
to 6 is 35
The shortest path
from 1 to 6 is with hops:
1 to 4 of weight 25
4 to 6 of weight 10
source: 3
No path from 3 to 0
No path from 3 to 1
No path from 3 to 2
The distance from 3
to 4 is 20
The shortest path
from 3 to 4 is with hops:
3 to 4 of weight 20
The distance from 3
to 5 is 15
The shortest path
from 3 to 5 is with hops:
3 to 5 of weight 15
The distance from 3
to 6 is 30
The shortest path
from 3 to 6 is with hops:
3 to 6 of weight 30
source: 4
No path from 4 to 0
No path from 4 to 1
No path from 4 to 2
No path from 4 to 3
No path from 4 to 5
The distance from 4
to 6 is 10
The shortest path
from 4 to 6 is with hops:
4 to 6 of weight 10
source: 5
No path from 5 to 0
No path from 5 to 1
No path from 5 to 2
No path from 5 to 3
No path from 5 to 4
The distance from 5
to 6 is 20
The shortest path
from 5 to 6 is with hops:
5 to 6 of weight 20
source: 6
No path from 6 to 0
No path from 6 to 1
No path from 6 to 2
No path from 6 to 3
No path from 6 to 4
No path from 6 to 5