(Latest Revision -- 04/25/00)
CS 4440 Work Assignment:
Weeks Nine to Ten
Assignments for weeks #9-10 of Theory of Algorithms
Readings:
Read the following parts of chapter 10:
section 10.1 Introduction (to Probabilistic Algorithms) pp. 328-329
section 10.2 Probabilistic Does Not Imply Uncertain pp. 329-330
section 10.5 Numerical Probabilistic Algorithms p. 333
section 10.5.2 Numerical Integration pp. 336-338
section 10.6 Monte Carlo Algorithms p. 341
section 10.6.1 Verifying Matrix Multiplication pp. 341-343
section 10.6.4 Amplification of Stochastic Advantage,
paragraphs 1 to 3, p. 350
section 10.7 Las Vegas Algorithms pp. 353-355
Read the following parts of chapter 11:
Intro p. 376
section 11.1 A Model for Parallel Computation pp. 376-379
section 11.2 Some Basic Techniques
section 11.2.1 Computing with a Complete Binary Tree pp. 379-380
section 11.2.1 Pointer Doubling pp. 380-383
section 11.3 Work and Efficiency pp. 383-386
section 11.4.1 Shortest Paths pp. 386-387
=================================================================
HOMEWORK to be turned in at the start of class 05/9/2000:
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. You do not have to
implement virtual initialization.
Here is the assignment, laid out as the previous
one was with the appropriate changes made. Be
sure to read this carefully! The output of the
new program and the algorithm will differ
significantly.
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.
(*
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.
The program reads the description of the
objects and initializes a table with R rows and
C columns. Here, R = numObjs and C = 1 + (the
sum of the weights of all the objects). The
entry in the table in the i-th row and j-th
column is reserved to hold, if needed, the max
value that can be packed into a knapsack with
weight capacity j if only objects numbered 1
through i are available.
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.
After initializing the table, 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 it 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.
The program solves each problem in a top down
fashion, using the table as a memory function,
and never computing any table entry
unnecessarily.
The program re-initializes the table before
starting each new problem with a new value of
wtLim. (This is an aid to seeing how the
algorithm works. It is, of course, not the
most efficient thing to do.)
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
-----------------------------------------------------------------
##############################
QUIZ: We need to have a quiz #2.
It will be Thursday, April 20. We should be
able to cover chapters 7-8 and perhaps some
topics from chapter 9.
##############################