/*
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:
- calculates all the entries of the table that are needed in order to find
the optimal solution corresponding to the given weight limit (wtLim),
- 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
- 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.
*/
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