For this assignment, you will use your graph representation from HW4 to implement Dijkstra's algorithm for finding shortest paths. You do not have to implement Dijkstra'a algorithm exactly as specificied in class, with the exact runtime specified in class, see the description below for more information.
As with HW4, you may use anything in the Java standard collections (or anything else in the standard library) for any part of this assignment. Take a look at the Java API as you are thinking about your solutions. At the very least, look at the Collection and List interfaces to see what operations are allowable on them and what classes implement those interfaces.
Copy your files from HW4 into a new directory:
You will also need to add a new method to your MyGraph.java
implementation from HW4.
You will need to add and implement the additional method
public Path shortestPath(Vertex a, Vertex b)
.
You can copy and paste the method header and comments from
the skeleton code HW5_MyGraph.java
.
The name of this file is HW5_MyGraph.java, only so you don't accidentally overwrite your MyGraph.java from
HW4 when downloading this file. You only need this file to copy and paste the method header for shortestPath.
You should still be implementing a graph called MyGraph and turn in a file at the end called MyGraph.java
You do not need
to re-implement all of the other methods, you can use your MyGraph implementation from HW4.
However, you
may modify any of the code you submitted for HW4 in your HW5 MyGraph file.
In this the assignment, you will use your graph from HW4 to compute shortest paths. The MyGraph class has a method shortestPath you should implement to return the lowest-cost path from its first argument to its second argument. Return a Path object as follows:
Because you know the graph contains no negative-weight edges, Dijkstra's algorithm is what you should implement. Additional implementation notes:
The program in FindPaths.java is mostly provided to you. When the program begins execution, it reads two data files and creates a representation of the graph. It then prints out the graph's vertices and edges, which can be helpful for debugging to help ensure that the graph has been read and stored properly. Once the graph has been built, the program loops repeatedly and allows the user to ask shortest-path questions by entering two vertex names. The part you need to add is to take these vertex names, call shortestPath, and print out the result. Your output should be as follows:
The FindPaths code expects two input files in a particular format. The names of the files are passed as command-line arguments. The provided files vertex.txt and edge.txt have the right format to serve as one (small) example data set where the vertices are 3-letter airport codes. Here is the file format:
Note data files represent directed graphs, so if there is an edge from A to B there may or may not be an edge from B to A. Moreover, if there is an edge from A to B and an edge from B to A, the edges may or may not have the same weight.
Style: For this assignment we will be grading similarly, but a little more strictly for things like style and efficiency than we did on HW3. Make sure you are commenting your code, looking for redundancy, structural mistakes, formatting of your code, efficiency of your algorithms, and preserving the abstraction of your classes through encapsulated fields, private methods when appropriate, copying and immutability, and appropriate level of commenting on public functionality.
Feel free to add additional public functionality that you think would be useful for a client. There might be some redundant code between your public methods; it might help to make some private helper methods to clean up your code.
Map<Vertex, Set<Vertex>> graph;
Where the keys of the outer map
represent the source vertices in the graph. The values of the map represent the adjacent neighbors
on the out edges for each vertex. For example, the graph above pictured for #1 in the topological sort
problem might be respresented in the graph structure (with no particular ordering) as:
graph -> [
A -> [B, D],
B -> [E, D],
C -> [],
D -> [E],
E -> [C],
F -> [],
G -> [F]
]
reverse(Map<Vertex, Set<Vertex>> graph)
which returns a new Map where each edge (u, v) has the reverse direction (v, u).
nextAdjacentVertices(Map<Vertex, Set<Vertex>> graph,
Vertex source)
that returns a Set<Vertex>
of vertices representing all of the
vertices that are path length 2 away from the source vertex (all the vertices that are adjacent to the
neighbors of the source).
You should submit a zip file to the Canvas dropbox with the name cse373_17wi_hw5.zip
.
This should not have a nested folder inside the zip.
When you unzip the turned in zip file, it should only have the expected files.
The expected files are all the provided code files and any additional files you created:
MyGraph.java
-- with the additional shortestPath methodEdge.java
-- with additions if you added to it Vertex.java
-- with additions if you added to it FindPaths.java
-- with your additions