(Latest Revision: 05/05/2003)
Pathways of the West: A Program That Computes Shortest Paths
Before working on this problem you need to know how to understand
the basics of programming graph algorithms. Read chapter
thirteen of "Walls and Mirrors" by Carranao and Prichard. You
also need to be familiar with the directions and examples here:
Write a program that inputs a source node, a list of nodes, and a
list of edges and outputs a table showing the shortest path from
the source to every other node.
The program reads from standard input. Inputs have a form like
The first item of input tells how many nodes there are. The
second item tells you the name of the source node. Then comes
the list of all the names of all the nodes of a connected graph.
The number of names of nodes agrees with the first item in the
input. For example, the sample starts with "65" and
"SanFranciscoCA" and then next comes a list of 65 city names.
(Note that SanFranciscoCA is one of the 65. The node list
contains the name of all the nodes.) The last part of the
output is a list of the weighted edges of the graph. Each
edge is represented by two names and a weight. The edge
represents a highway route between two destinations. The names
given are the destinations and the weight represents the mileage
between the destinations.
(In the sample input, the names of the cities and edges appear in
alphabetical order. That is just coincidental. There is no
requirement that the list of nodes or edges be in alphabetical
The program writes to standard output. First it writes a table
heading and then a table that contains in its columns: the name
of each destination city (all cities are destinations except the
source city), the length of the shortest path in the graph from
the source to the destination, and the name of the starting
point of the last edge in the shortest path.
For example, this
contains the line:
AstoriaOR 742 NewportOR
which means that the shortest path in the graph from
SanFranciscoCA to AstoriaOR has length 742, and that shortest
path goes to NewportOR just before ending up in AstoriaOR.
DISCUSSION OF PROCESSING:
You can use Dijkstra's algorithm to compute the shortest paths
and "back pointers." There is a description of the algorithm
You can test with the large sample I gave you, but you should
also generate a small sample input to test with as you develop
the program. I think this will make it easier to debug.
WHAT TO TURN IN:
You will turn in your assignment by e-mail. With regard to the
e-mail please follow these rules:
Send the following items to me by e-mail before midnight on the
- Always send me e-mail as plain text in the main message
body. Never send me attachments. If you want to send
e-mail from home just login remotely to one of the CS
suns and do the kind of e-mail command I showed you in
the hello-world exercise.
- I will tell you what subject line to use with each
message, and I need you to use exactly the
subject lines I give you. (I get hundreds of e-mail
messages at a time and your subject line allows me to
sort messages.) I will take points off if the subject
line is wrong.
A final version of the source code and a script showing your
test runs. Include all the source files (*.h files and *.cpp
files) that are required to compile the program into an
executable image -- everything I need so that I can compile and
test your program. Combine all the source files and the script
shell archive file
and e-mail me the archive file with the subject line:
Note that there are no spaces in the subject line given. It is important
that you do not insert any spaces. My e-mail address is: firstname.lastname@example.org.
For the due dates, see
the class schedule.
Historical Note: The algorithm for computing "single source, all
nodes, shortest paths" that we discussed was invented by Edsger
Dijkstra. Dijkstra did much of the important pioneering work in
the field of computer science. He passed away during the summer