(Latest Revision -- 04/11/00)
04/11/00 -- Added Homework Assignment
CS 4440 Work Assignment:
Weeks Seven to Eight
Assignments for weeks #7-8 of Theory of Algorithms
Readings:
chapter 8
chapter 9, with special attention to:
section 9.1 Intro to Graphs & Games pp. 285-291
section 9.6 Backtracking pp. 305-306
section 9.6.1 The Knapsack Problem (3) pp. 306-308
section 9.7 Branch and Bound p. 312
section 9.7.1 The Assignment Problem pp. 312-315
section 9.7.3 General Considerations pp. 316-317
=================================================================
HOMEWORK to be turned in by midnight 4/21/2000.
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 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.
/*
Header comment for program
This program uses dynamic programming to
calculate the solution to a 0/1 knapsack
problem. (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. The program reads this description of
the objects and then computes the table of
solutions to the 0/1 knapsack problems for all
knapsack capacities up to a maximum equal to
the sum of the weights of all the objects in
the data set.
For example here the sum of the weights is
1+2+5+6+7=21. Therefore the table of solutions
is filled out with 22 columns, corresponding to
knapsack capacities of 0 to 21. The program
prints out the description of the objects and
the table.
Finally, there comes an optional series of
knapsack capacities, wtLim.
18
16
21
The program will read each value of wtLim. For
each value it prints a report, stating the
(optimal) solution to the knapsack problem when
the knapsack capacity is wtLim.
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
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 | 0 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 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
5 18 | 0 1 6 7 7 18 19 24 25 25 25 25 25 25 25 25 25 25 25 25 25 25
6 22 | 0 1 6 7 7 18 22 24 28 29 29 40 41 46 47 47 47 47 47 47 47 47
7 28 | 0 1 6 7 7 18 22 28 29 34 35 40 46 50 52 56 57 57 68 69 74 75
Packing Sack with weight limit of........ 18
------------------------------------
Object # Weight Value
5 7 28
4 6 22
3 5 18
------------------------------------
Totals: 18 68
Packing Sack with weight limit of........ 16
------------------------------------
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
------------------------------------
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