Assignments | Course Home | Questionnaire | Schedule & Lecture Notes | Submit

## Homework Assignment 07

### Overview

Topic: Shortest Path Algorithms
Due: Monday, 5 November 2012, 11:59pm

This will be a programming project.

For this assignment, you may work in pairs.

### Overview

For this homework, we will consider a recent programming programming contest problem (click the link for the problem statement).

Your goal is to develop source code that solves this problem. You may use C++, Java, or Python as your programming language. To be successful, your program must produce precisely the correct output for each test set, as described in the problem statement.

The original description of the geometry and the various witnesses is just window dressing for what should boil down to a series of difference constraints of the form A < B + ε where A represents the firing time of one person, B the firing time of another, and arbitrary constant ε (which may be negative).

Once you have the various different constraints, the original data is no longer needed. Section 24.4 of CLRS discusses an approach for analyzing a set of difference constraints using a shortest-path computation on a graph model that is constructed from the constraints. In that section of the book, the goal is simply to find a consistent set of values for the variables, assuming one exists. The solution is to introduce one artificial source vertex into the graph and then to compute shortest path distances to all other vertices from that source; if a negative-weight cycle is detected, the set of constraints are inconsistent.

This contest problem goes a step further. When it is possible to satisfy the constraints, you must further determine whether there is a unique ordering of firing times that satisfies a constraint, or whether there are two or more such orderings. In order to solve this problem, we suggest that you instead compute the all-pairs shortest path distances in the associated graph. (There is need for introducing an artifical source vertex.)

Our proposed algorithm is as follows:

1. Convert the original witness information into different constraints based on the relative distances.
2. Construct a graph representation G based on the difference constraints, with constraint A < B + ε modeled as an edge directed from B to A with weight ε.
3. Compute all-pairs shortest path distances for the graph G using the Floyd-Warshall algorithm.
4. We next determine the firing order as follows:
1. If there exist any vertex v of G with shortest-path distance δ(v,v) < 0, the original set of difference constraints in infeasible.
2. Otherwise, the contraints are solvable, and we must determine a proper ordering or detect that the order is not well-defined. To do this, we consider a new directed, unweighted graph G' that has a directed edge (u,v) precisely if δ(v,u) < 0 in G. This is because a negative-weight path from v to u in graph G represents a transitive series of inequalities requiring that u < v in terms of the original firing times.

There is a one-to-one correspondance between valid orderings of the original firing times and topological orderings in the graph G'. To compute a topological order while also checking if it is unique, we recommend an alternative algorithm described in Exercise 22.4-5.

We have an automated system that will execute your program on the judge's official data sets and compare your results to the expected output. Your program must adhere to the following conventions:

• Your source code must be in a single file, appropriately named as
• G.cpp,
• G.java, or
• G.py
depending on your language choice. For those using Python, the assumption is that you are using Python2 (if you would prefer to use Python3, let me know and I'll adjust the system).

• Your program must read all input from a file named G.in, without any direct user interaction

• Your program must output all results to the standard console (e.g., cout in C++, System.out in Java, or with print in Python).

When you are ready to test your program, run the following command on turing:

```/Public/goldwasser/314/judge <sourcecode>
```
(where sourcecode is appropriately G.cpp, G.java, or G.py). You are welcome to run this test as many times as you'd like.

### Judge's Data

Although in a real programming contest, students are only shown the limited input and output examples in the printed material, we will provide you with the complete set of judge's tests that were used on the contest (and which are being used by our automated judging utility). You are free to use these examples in order to trace problems that are arising in your program.

In fact, the above judging utility simply runs your program, stores the output, and compares the actual output to the expected output.

### Formal Submissions

Although you are welcome to use the online judging utility to see if your program is correct, you must formally submit your source code through the course web page. If you are working as a pair, only one of you needs to submit the program, but make sure that comments at the beginning of the source code credit both authors.