Saint Louis University |
Computer Science 3100
|
Dept. of Computer Science |
Topics: Applications of Network Flow
Related Reading: Chapter 26 of CLRS
Due:
10:00am, Friday, December 2, 2016
You must adhere to the policies on academic integrity, paying particular attention to the limits on collaboration.
For this homework, you are to develop an implementation for a programming contest problem found at open.kattis.com/problems/catvsdog. You may program in any language that is accepted by the kattis site (currently C, C# C++, Go, Haskell, Java, JavaScript, Objective-C, PHP, Prolog, Python 2, Python 3, Ruby). Note that your solution must not only pass the automated tests on the kattis site, but also produce some extraneous trace information that demonstrates how the optimal solution may be achieved.
We discussed this problem during a class meeting, but for convenience, we provide here a very brief summary of how to model this problem and its relationship to network flow.
The problem can be modeled as an unweighted, undirected graph in which there is a vertex for every viewer of the program, and with an undirected edge between viewers u and v if the desired outcomes for those two viewers are incomptible (i.e., it is impossible to make both of them happy).
To maximize the number of happy viewers, we focus on minimizing the number of unhappy viewers (after which it becomes easy to calculate the remaining number of happy viewers).
To have a set of happy viewers, we must select at least one endpoint of each edge to represent an unhappier viewer. In terms of our graph, this unhappy set must form a vertex cover of the graph. And to minimize the number of unhappy viewers, we wish to calculate a minimum vertex cover.
The problem statement's condition that all viewers are either cat lovers or dog lovers, means that our model graph is bipartite (the only conflicts that exist are between a cat lover and a dog lover). For bipartite graph's, König's Theorem states that the size of a minimum vertex cover must equal the size of a maximal matching in the graph.
The maximum matching in a bipartite graph can be computed by using network flow on a modified graph. We create a single source with edges having capacity 1 to each catlover vertex. Then we direct the conflict edges from the catlover to the doglover and with capacity 1. Finally, we create a new sink vertex and then have edges with capacity 1 from each doglover vertex to the sink.
The size of the maximum matching on this graph will equal the size of the minimum vertex cover. Furthermore, we can constructively identify a minimum vertex cover as follows. If we consider the residual flow network for a maximum flow, we identify our vertex cover set to be the union of those catlover vertices that are not reachable from s together with those doglover vertices that are reachable from s.
To be successful on this assignment, you must develop a solution that is "accepted" by open.kattis.com. You will need to register for an account in order to submit solutions, but registration is free.
To be accepted by that software, your program must read its input from the standard input stream in whatever language you use (e.g., cin in C++, or System.in in Java), with the input format being precisely what is described in the problem statement. It must also output precisely the expected results on the standard output stream (e.g., cout in C++ or System.out in Java), and it must not output any other extraneous information on the standard output stream. You may, however, output any extraneous information on the standard error stream (e.g., cerr in C++ or System.err in Java), and in fact as a requirement for this project, we will expect your program to produce such additional information as described below.
While the problem statement gives one sample input and its associated output, there are many more hidden test cases that kattis will use to evaluate your software. Please see the kattis website for a more complete explanation of the judging process and the possible outcomes that are reported. The challenge to the contest format is that if you're program fails for one of the given reasons, the system does not disclose any information about the test input that caused the failure.
Because we know it can be very difficult to determine the reason for failure when you don't have many reference examples, we will be kind and provide a few additional test cases with known output, so that you might have something to trace when doing your software development.
additional cases: input, output
Although the official contest problem only demands that your program output the maximum number of happy viewers, we ask more of your own program. Your software must demonstrate the feasibility of the reported solution but describing which cats and dogs will remain on the show, and as a result, which viewers will remain happy.
Note Well: in order for your software to be accepted by kattis, it is imperative that the only information sent to standard output be the number of happy viewers. All other information that we are requesting must be sent to the standard error stream.
As an example of a format you might adopt, here is a plausible result for the original examples given in the problem description:
1 <== sent to standard output Keeping D1 Happy person: +D1,-C1 3 <== sent to standard output Keeping C1 Happy person: +C1,-D1 Happy person: +C1,-D1 Happy person: +C1,-D2
Please note that there may not be a unique optimal strategy. For example on the first case above, we could just as easily kept cat C1 and made the first viewer happy. So your strategy might not agree with what I report, but the number of happy people should agree. For additional examples, here is the trace output corresponding to the additional test cases we provided in the proceeding section.
While no one reads your source code during a programming contest, we are using this as a course project and will be doing a code review. For this reason, you are expected to:
Ensure that your source code is legible and well documented with internal comments.
Provide a brief overview of your implementation, discussing any unusual design decisions, and disclosing any known problems with the correctness of your code.
You are required to submit two files
The homework is worth up to 100 points, as follows: