You are on page 1of 2

CSc 345 Analysis of Discrete Structures Fall 2007 (McCann) http://www.cs.arizona.

edu/classes/cs345/fall07/

Program #5: The Knapsack Problem


Due Date: November 27 th , 2007, at the beginning of class Overview: The knapsack problem is a classic example, frequently used to demonstrate solutions from a variety of algorithm types. Not only are there many ways of solving the problem, there are many ways to state the problem. For this assignment, well use a version of the knapsack problem known as the subset sum problem: Given a set of integers S = s1 , s2 , . . . , sn and another integer C, is there a subset of S such that the sum of its elements equals C? Naturally, if the answer is yes, wed also like to know at least one of the subsets that sums to C. Assignment: Write a complete, welldocumented Java program that, given S and C, solves the subset sum problem using two separate techniques. If no subset of S sums to C, both techniques are to report that fact. In addition, you are to measure the execution time of each technique. The two techniques are: Technique #1: Exhaustive search. The idea: Plan to generate every possible subset of S, computing the sums of their elements as you go, but stopping when a solution is found. How do you compute all possible subsets of a set? Thats for you to gure out. A little research should uncover suitable algorithms. As always, we require that you write your own implementations of any algorithms you use. Technique #2: Dynamic Programming. As you might recall, the idea in dynamic programming solutions is that a table of solutions is created, often starting with simple subproblem solutions and building to the solution to the given problem. We can apply this technique to the subset sum problem to produce a reasonably ecient solution. Heres how: Create a table V [1..n, 0..C], where n = |S| and C is the desired sum. Each location in the table is computed using this magic expression: V [i, j] = max(V [i 1, j], V [i 1, j si ] + si ). V [0, j] = 0 when j 0, and V [i, j] = when j < 0. A solution (that is, a subset of S that sums to C) is found if V [n, C] = C. That expression not only lls the table, it also hints at how to determine the subset that produces the sum. Again, we wont tell you how to determine this; we expect that you can gure it out for yourself. We will, however, give you an example problem to get you started. Let C = 13 and S = {1, 2, 5, 6, 9}. The resulting table is: si (1) (2) (5) (6) (9) V 1 2 3 4 5 0 0 0 0 0 0 1 1 1 1 1 1 2 1 2 2 2 2 3 1 3 3 3 3 4 1 3 3 3 3 5 1 3 5 5 5 6 1 3 6 6 6 7 1 3 7 7 7 8 1 3 8 8 8 9 1 3 8 9 9 10 1 3 8 9 10 11 1 3 8 11 11 12 1 3 8 12 12 13 1 3 8 13 13

As V [5, 13] = 13, it is possible to nd a subset of S that sums to C. As this is a small problem size, we can easily nd the subset by inspection: {2, 5, 6}. As mentioned above, youll need to employ the tables content to nd a more ecient algorithmic way to nd the subset.

(Continued on the back...)

Data: Your program is to accept the name of a data le from the command line. The format of a data le is straightforward. The le contains one integer per line. The rst line provides C. Lines 2 through n + 1 contain the elements of S, in no particular order. As S is intended to be a set, your program will need to check it for duplicates. If any are found, the program is to terminate with a meaningful error message. Remember that the empty set is a set. We wont be supplying any sample data les for this assignment, as we expect you can create them on your own with little trouble. Output: The initial output of your program is to be the value of C and the content of S: Subset Sum Problem: C = 13; S = {1,2,5,6,9}.

Each of your two solutions (dynamic programming and exhaustive search) is to display one of two messages. If a solution is found, display: SUCCESS was achieved using Dynamic Programming in # minutes and ##.## seconds! The sum of {2, 5, 6} equals 13. Of course, the algorithm name and execution time will vary, and you are to use as many digits as is necessary to display the quantity of minutes. When a solution cannot be found, display: FAILURE was suffered by Dynamic Programming after # minutes and ##.## seconds. No subset of S sums to 13. Separate the three output sections from one another single blank lines. Very important: Construct your program to compute and display the dynamic programming solution before it computes and displays the exhaustive search solution! This will allow the TAs to verify the correctness of your dynamic programming solution on large problems without having to wait for the exhaustive search. Hand In: On the due date, turn in a printed listing of your welldocumented program statements. As usual, you are required to submit your completed program le(s) to the class submission directory, using the turnin command. The turnin location for this assignment is cs345p5. Name your main program source le Prog5.java so that we dont have to guess which le to compile/translate. Other Requirements, Hints, and Miscellaneous Babbling: The knapsack problem is a member of the NPComplete class of problems, which means that it has no known ecient solution. Well talk a little bit about such problems later in the course. As usual, we plan to test your code with a variety of data les. Thus, so should you. The diculty level of this assignment does not reach that of previous programs. This is intentional, given the holiday between now and the due date, and the time of the semester. Even so, an early start is recommended. This is the last program of the semester. Savor it, cherish it, for soon it will be but a misty memory . . .

You might also like