# Homework Assignment 5

Graphs

15-211 Summer 2011

For this assignment you will be implementing a general graph class and associated algorithms. This is the most extensive assignment. As before, you may work in pairs.

## Objectives

- Implement a general graph class.
- Implement two different algorithms for the shortest path
- Implement the Union-Find algorithm.
- Implement a minimum spanning tree algorithm.

## Theory Questions: 20 points

In the file theory5.pdf there are some short theory questions. Please answer the questions in this file and turn it in class on Friday, June. 17.

## Starter Code

Download the starter code for assignment (zip file). Once you finish implementation, FTP java files to /afs/andrew.cmu.edu/course/15/211/handin

## What to Submit

You FTP the following java files

- Graph.java
- Dijkstra.java
- BellmanFord.java
- Kruskal.java
- UnionFind.java

Do not submit anything else like zip files, folders, desktops, class files. Make sure you do not use packages in your java files as well. Failure to do so will result in a 20 point penalty.

## The Graph Class: 20 points

The core of this assignment is the `Graph` class. You are to implement a directed weighted graph. Our graphs do not allow any self-loops, nor they allow multiple edges (in one direction) between vertices.
There are many possibly ways of implementing this class Our graph is a Map of vertices to a Set of incident edges.
The Graph class takes two generic types. V is a type for vertices and E is a type for edges that extends the Edge class.
See the `GraphInterface` class for methods to implement.

## The Dijkstra algorithm: 20 points

Your first task, once your graph class is working well, is to implement Dijkstra's shortest-path algorithm.
Dijkstra's algorithm can fail on graphs with edges of negative weight, therefore implementation should throw an `IllegalArgumentException` in such cases.

Your implementation should run in *O((|V|+|E|) log(|V|))* time. Note, you can use Java's Priority Queue for this assignment, or you can reuse it from your previous assignment. See lecture notes for the algorithm.

## The Bellman-Ford algorithm: 15 points

Your implementation should give the same result as Dijkstra's algorithm on graphs with positive-weight edges. Furthernore, it should find a shortest path on graphs with negative-weight edges as long as there are no negative-weight cycles. You throw an `IllegalArgumentException` in the latter case.

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

## The Kruskal algorithm: 15 points

You are to implement Kruskal's minimum spanning tree algorithm. Your implementation should run in *O(|V|+|E|*log|E|)* time, To achieve this runtime, you should implement the Union-Find algorithm..

## The Union-Find algorithm: 10 points

In your code, you are required to implement the *union by rank* heuristic. This heuristic is used when applying the union operation to two trees. You are also required to implement *path compression*. This heuristic means that when we perform a find operation on element x, we first find the root of the tree for x's equivalence class and then we set the parent of all nodes on the path from x to the root to be the root.

Victor S. Adamchik, CMU, 2011