Copyright RIT 2009
$Id: writeup.xml,v 1.5 2009/05/05 22:36:11 vcss233 Exp $
You are to work on this lab completely on your own.
In this lab you will implement Dijkstra's shortest path algorithm. You will use data for New York's cities and roads to construct a graph that can be used to determine the neighbors of any city and find the shortest path between two cities.

New York State Road Map
Review your lecture notes on graphs and shortest paths, and read the relevant text sections.
Fetch the jar file by clicking here (http://www.cs.rit.edu/~vcss233/pub/lab09/Binaries/labDijkstra.jar) .
[Eclipse] You can insert the java and text files into an
eclipse workspace and also configure the build
path so it recognizes the ArrayDiGraph.class
from the jar file.
To do that, right-click on workspace,
then select Build Path->Configure Build Path->Libraries->Add External Jar).
In this activity you will write the data storage classes City and Road. These classes are used to represent the cities and roads that form a graph.
These classes
are used by the Mappy class.
Mappy constructs
a directed graph, DiGraph, which
is implemented in the class file ArrayDiGraph.
This graph is used to
represent a geographical map of cities and roads.
The graph is composed of the city name for the vertex key,
a City for the vertex data, and a Road
for the edge data:
|
The Mappy class contains all the code necessary to read
the input data and construct the graph. You can use the supplied
data file of New York cities/roads,
ny.txt
(./Auxiliary/ny.txt)
when
running the program:
|
The data in this file contains 4 pieces of information per line: the source city name, the destination city name, the distance between the cities (in miles), and the major road that connects them.
When the program runs, it provides a command line interface to display information.
The list of available commands can be found by entering the help command:
|
Once you have written the City and Road classes,
you should verify they work by writing main methods for
each of the classes and fully testing all the methods.
It is important that you read the javadocs for these classes
so that you know exactly how to define them.
When finished, the cities and roads commands
should work correctly:
|
When you feel your code is working correctly and is properly documented, submit your work using the following command:
|
Now that the graph is constructed correctly, your next task is to implement the
neighbors command. It should display information about the
neighbors of a city that is supplied by the user, i.e.:
|
You will implement your solution in the
neighbors routine of the
Mappy class.
Notice that
the neighbors are printed out in alphabetical order. Since the graph
does not guarantee the order of the neighbors, you should use a sorted
collection to store the neighbor city names before displaying the
neighbor and road information.
When you feel your code is working correctly and is properly documented, submit your work using the following command:
|
In the final activity you will implement the travel
command. It should display information about the shortest path between
two cities, as in this example:
|
You will implement your solution in the
travel routine of the
Mappy class.
Your program will be able to compute the shortest path between any two supplied cities. Use the following algorithm for calculating the shortest path between a source and destination city:
|
The graphs below show a trace of the algorithm as it selects each least-cost vertex from the initial collection of 'vertices'. The vertices begin as a list of all the vertices. As the algorithm removes each vertex (when it becomes the working vertex), the graph shows it double-circled. After removal from the 'vertices', a vertex contains its finalized cost and predecessor information. The algorithm will change only those vertices that remain in the 'vertices' collection.

Dijkstra's Shortest Path Algorithm
When the algorithm finishes, the graph will contain all the necesary information to determine the shortest path. Each destination city stores the total distance from the start city. To get the path, you should iterate from the destination city to the source city, using the predecessor of each city along the way. You can use a list to store each city as you traverse the graph, inserting each predecessor at the front of the list. When finished, you can traverse through the list from start to end and print the city pairs along with the road information.
If the user wants to travel from the same source and destination city, the algorithm will still work, but you should display a different message for the route:
|
This algorithm also supports the case where the graph is not fully connected and there is no path from the source to destination. If a vertex in the graph is unconnected, then the vertex will have the initial, maximum cost value when pulled out of the 'vertices'. You can tell that a vertex was unreachable if it has a maximum cost and a null predecessor. (By contrast, the start vertex has 0 cost and a null predecessor.)
When you feel your code is working correctly and is properly documented, submit your work using the following command:
|
Grade Breakdown: