Lab #5: Graph Searching


Due: Wednesday, August 6th, 2007 at 11:59PM


Zip file with source files, documentation, &c.
Browsable version of the files above

In this homework, you'll be implementing a general graph class, and then implementing two separate algorithms for finding shortest paths in graphs: Dijkstra's algorithm and the Bellman-Ford algorithm. The code that you write will be general enough to express graphs representing many different kinds of things; for example, you will be able to make graphs representing roads and intersections, and use the graph search algorithms to find the quickest way to get from one place to another.

Goals

The Graph Class (30 points)

The core of this assignment is the GeneralGraph class, which implements the Graph interface. The graphs we are dealing with are directed graphs; that is, each edge has a source and a destination (think about one-way streets, for example). Our graphs will not allow any self-loops (that is, no edge in the graph should have the same starting and ending vertex), nor will they allow multiple edges from the same source to the same destination. (Having an edge from vertex A to vertex B and another from B to A is fine; you just can't have two from A to B).

In previous assignments, such as the queue, hashtable, and priority queue, there was not much choice in how to implement the interface we gave you. This assignment is a bit more open-ended; we give you the interface for the graph, but there are many possibly ways of implementing it. You should look over the Graph interface and understand it well before starting on your graph implementation.

Ideally, your implementation should be:

Of course, there are tradeoffs, and you should think about what representation will do best overall.

Since you have already written your own implementations of many of the more interesting data structures, there are no restrictions on which classes in the Java standard library you may use.

Dijkstra's Algorithm (30 points)

Your first task, once your graph class is working well, is to implement Dijkstra's shortest-path algorithm. The algorithm should stop as soon as it has found the shortest path from the start vertex to the end vertex, and it should return the list of edges in the path (from that list, the vertices in the path as well as the cost of the path can both be found pretty easily).

Of course, Dijkstra's algorithm does not work in all cases on graphs with edges of negative weight; as you've seen, Dijkstra's algorithm can fail on such graphs, and your implementation should throw an IllegalArgumentException if it is given a graph with a negative-weight edge. However, our graph class allows such edges, so we will also implement a less efficient algorithm that succeeds in such cases: the Bellman-Ford algorithm.

Your implementation should run in time O((|V|+|E|)\log(|V|)), including the cost of the operations inside the graph class.

Note: You CANNOT use Java's Priority Queue for this assignment. You must write your own!

The Bellman-Ford Algorithm (20 points)

The interface for the Bellman-Ford algorithm is exactly the same as with Dijkstra's algorithm; it should behave the same way, except that it should give correct results on graphs with negative-weight edges as long as there are no negative-weight cycles. Your implementation should throw an IllegalArgumentException if the graph it is given contains a negative-weight cycle.

Your implementation should run in time O(|V|*|E|).

Hints

Coding Style: 10 points

It should go without saying that your submission must have good style. This is needed so that the TAs can understand your code and give you partial credit. It's also a good habit.

Theory Questions: 10 points

In the file theory.txt there are some theory questions. Please answer the questions in this file and submit it along with your .java files.