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

Saint Louis University

Computer Science 314

Michael Goldwasser

Fall 2012

Dept. of Math & Computer Science

Homework Assignment 07

Shortest Path Algorithms


Topic: Shortest Path Algorithms
Related Reading: Ch. 23, 24
Due: Monday, 5 November 2012, 11:59pm

This will be a programming project.

For this assignment, you may work in pairs.

Please make sure you adhere to the policies on academic integrity in this regard.


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.

Algorithmic Advice

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.

Testing Your Program

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:

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,, or 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.

Grading Standards

As with other homeworks, we will grade this out of 100 points. 70 of those 100 points will be based purely on the correctness of your program on the judge's data. However, rather than an "all or nothing" approach, we will award points proportionally to how well it performs on the individual test. (Although this assumes that your program makes a sincere effort; for example, if you just say "IMPOSSIBLE" to everyone test, we will not consider those a sincere effort).

The final 30 points will be based upon a subjective evaluation of your source code, including its organization, legibility, and documentation.

Michael Goldwasser © 2012
CSCI 314, Fall 2012
Last modified: Monday, 29 October 2012
Assignments | Course Home | Questionnaire | Schedule & Lecture Notes | Submit