(Latest Revision -- 03/05/2004)
CS 4440 Work Assignment:
Weeks Five to Seven
Assignments for weeks #5-7 of Theory of Algorithms
Reading to be finished by Friday, April 02, 2004:
- Finish chapter 7
- Read chapter 8
HOMEWORK
to be turned in at the start of class Monday, April 05,
2004
Problem #1: Write a program that implements the final O(n) version of the
algorithm "selection" of p238. "Final version" means you need to use function
"pseudomed of page 239 instead of function "median" that appears in the call
on page 238. Also you will need something for the function "pivotbis." There
is a very nice version of this function in the class web space. Its design is
inspired by a version of the algorithm that appears in the Carrano text book.
I will discuss it in class.
See also
my mnemonic work-up of this algorithm.
INPUT:
Write the program so it will input a file like this:
Number of numbers
number #1
number #2
number #3
number #4
.
.
.
number #N
and load the numbers into an array. Of course this should be a
pseudo-random list of numbers, certainly not sorted. Some of the
lists you test should be long lists. I can share with you a
program that will generate a long list of pseudo-random numbers,
if you like. Let me know if you are interested.
The program should also input from the user, executing a main
loop, getting a rank of an item to locate each time through the
loop. You can run the program and have it tell you several
values for items of various ranks.
TESTING:
Create some nice test inputs and run your program on them.
If you use the unix sort command (sort -n) to sort a copy of
each version of the input file (after taking out the first
number) then you can use the sorted versions of the file to
check the accuracy of your program.
After getting the output from the program, look at the sorted
files and see if the values the program found are really in the
positions of the sorted file corresponding to the ranks. For
example, if the program says that the 50th smallest element of
the array is 96593, then you should find 96593 in the 50th
position of the sorted list.
WHAT TO TURN IN:
e-mail me a copy of the source code for your program. Turn in a
hardcopy of test scripts plus a typewritten summary of your
findings. Don't print out the contents of long input files.
Problem #2. 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