(Latest Revision -- 04/14/2002)

CS 4440 Work Assignment:
Weeks Nine to Ten
April 15-26


QUIZ: We need to have a quiz #2.

We will have the quiz on Friday April 26. It will cover chapters 7-8 and the assigned topics from chapter 9.


Readings:


HOMEWORK to be turned in by midnight 05/03/2002

Do a new version of the knapsack problem that was assigned for the last homework.

This time use a memory function and a top-down version of the algorithm so that no table entry is calculated unnecessarily. (Use the type of technique discussed in sections 8.7 and 8.8 of Brassard and Bratley).

For this exercise you are not required to implement virtual initialization. However, after you get the program working correctly without virtual initialization, it may interest you to try adding that capability.

The assignment is here, laid out as the previous assignment was, with the appropriate changes made. Be sure to read this carefully! The output of this new program will be quite different from the output of the last program assigned. Also the algorithm you will use this time will differ significantly from the one you used before.

Below I have given a header comment for a program, one copy of sample input with corresponding output, and two more sets of sample input. Your assignment is to write the program and turn in source code plus test script showing how the program performs on each of the three sample inputs here, plus any other inputs you may want to test.

Make sure that the format of the test script is such that you cat (display) the first input file, then run the program on the first input, then cat the second input file and run the program on the second input file, and then cat the third input file and run the prorgram on the third input file.

/* This program uses dynamic programming and a memory function to calculate the solution to 0/1 knapsack problems. (c.f. page 266 of Brassard and Bratley) You enter input like this:

   5
   1 1
   2 6
   5 18
   6 22
   7 28
   18
   16
   21
Here the first number is numObjs, the number of objects that are available to be placed in the knapsack.

   5
Next come numObjs pairs of numbers. These pairs describe the objects.

   1 1
   2 6
   5 18
   6 22
   7 28
The first number in the i-th pair is the weight of the i-th object. The second number in the i-th pair is the value (cost) of the i-th object.


For example here there are 5 objects and the sum of the weights is 1+2+5+6+7=21. Therefore the table of solutions has 5 rows and 22 columns corresponding to knapsack capacities of 0 to 21.

The program initializes all entries of the table to the value -1. Immediately after this initialization, the program prints the table to show how it looks in that form.

The last part of the input is an optional series of knapsack capacities, wtLim.

   18
   16
   21
The program will read each value of wtLim. For each value of wtLim, the program does the following things in the order shown:

  1. calculates all the entries of the table that are needed in order to find the optimal solution corresponding to the given weight limit (wtLim),
  2. prints a report. The report shows the table as it appears after the new entries necessary have been added to the table. The report also shows an (optimal) solution to the knapsack problem when the knapsack capacity is wtLim, and
  3. re-initializes the table by re-setting all the entries to -1. (When I test your program, this behaviour will help me determine whether your program is doing exactly the amount of work required for each value of wtLim -- no more and no less. It is, of course, not the most efficient thing to do.)
Your program must solve each problem in a top down fashion, using the table as a memory function, and never computing any table entry unnecessarily.

The report gives the serial number, weight, and value of each object that should be chosen to place in the knapsack, and it also gives the total weight and value of the chosen solution.

How does the program know which objects to use to comprise the solution? It uses the method of scanning backward through the table described in Brassard and Bratley.

In this program there can be at most 20 objects, with weights that add up to at most 100. It is easy to change these limits by giving certain constants different values.

*/ Sample Input File #1:
   5
   1 1
   2 6
   5 18
   6 22
   7 28
   18
   16
   21
Output For Sample Input File #1:
The initialized table: 

                   Total Weight ----->

  wt val   |   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21
____________________________________________________________________________________________________
   1   1   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1
   2   6   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1
   5  18   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1
   6  22   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1
   7  28   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1

//////////////////////////////////////////////////


Packing Sack with weight limit of........ 18

                   Total Weight ----->

  wt val   |   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21
____________________________________________________________________________________________________
   1   1   |  -1  -1  -1   1   1   1   1   1  -1   1   1   1   1   1  -1  -1   1  -1   1  -1  -1  -1
   2   6   |   0  -1  -1  -1  -1   7   7   7  -1  -1  -1   7   7   7  -1  -1  -1  -1   7  -1  -1  -1
   5  18   |  -1  -1  -1  -1  -1  18  -1  -1  -1  -1  -1  25  25  -1  -1  -1  -1  -1  25  -1  -1  -1
   6  22   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  40  -1  -1  -1  -1  -1  -1  47  -1  -1  -1
   7  28   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  68  -1  -1  -1

------------------------------------
    Object #      Weight       Value
           5           7          28
           4           6          22
           3           5          18
------------------------------------
     Totals:          18          68

//////////////////////////////////////////////////


Packing Sack with weight limit of........ 16

                   Total Weight ----->

  wt val   |   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21
____________________________________________________________________________________________________
   1   1   |  -1   1   1   1   1   1  -1   1   1   1   1   1  -1  -1   1  -1   1  -1  -1  -1  -1  -1
   2   6   |  -1  -1  -1   7   7   7  -1  -1  -1   7   7   7  -1  -1  -1  -1   7  -1  -1  -1  -1  -1
   5  18   |  -1  -1  -1   7  -1  -1  -1  -1  -1  25  25  -1  -1  -1  -1  -1  25  -1  -1  -1  -1  -1
   6  22   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  29  -1  -1  -1  -1  -1  -1  47  -1  -1  -1  -1  -1
   7  28   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  57  -1  -1  -1  -1  -1

------------------------------------
    Object #      Weight       Value
           5           7          28
           4           6          22
           2           2           6
           1           1           1
------------------------------------
     Totals:          16          57

//////////////////////////////////////////////////


Packing Sack with weight limit of........ 21

                   Total Weight ----->

  wt val   |   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21
____________________________________________________________________________________________________
   1   1   |  -1   1  -1   1  -1  -1   1   1   1   1   1  -1   1   1   1   1   1  -1  -1   1  -1   1
   2   6   |  -1  -1  -1   7  -1  -1  -1  -1   7   7   7  -1  -1  -1   7   7   7  -1  -1  -1  -1   7
   5  18   |  -1  -1  -1  -1  -1  -1  -1  -1  25  -1  -1  -1  -1  -1  25  25  -1  -1  -1  -1  -1  25
   6  22   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  47  -1  -1  -1  -1  -1  -1  47
   7  28   |  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  75

------------------------------------
    Object #      Weight       Value
           5           7          28
           4           6          22
           3           5          18
           2           2           6
           1           1           1
------------------------------------
     Totals:          21          75

Sample Input File #2:

4
2       10
4       10
6       12
9       18
0
1
2
4
6
8
16
20
21

Sample Input File #3:

3
2       1
3       2
4       5
0
1
2
3
4
5
6
7
8
9