

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


This exam must be handed in before 6:00 PM on Friday, December 16. Distance learners: Submit as an attachment to e–mail. This exam is open–book, open–notes, and not proctored. Do your own work. The first two questions are based on the following data for the Knapsack Problem. This is a list of ten items, in no particular order, with weights, values and “value density” given. Item Weight Value Density 1 16.00 192.00 12. 2 2.00 4.00 2. 3 3.00 9.00 3. 4 1.00 0.50 0. 5 5.00 24.00 4. 6 18.00 27.00 1. 7 4.00 18.00 4. 8 20.00 400.00 20. 9 30.00 27.00 0. 10 24.00 48.00 2.
CPSC 5115 Final Exam Due Friday, December 16, 2005 The next two questions are based on the following algorithm, written in a strange variant of Java, in which all arrays are one–based ; thus the statement long[] A = new long [10] creates an array of 10 elements, indexed 1 through 10. I still find 0–based arrays confusing when doing complexity analysis. Also note that this version uses procedures, not classes, and really is so different from any language that it cannot be processed by any compiler. Procedure Doofus ( long [] A, N) // Array A has N elements, indexed 1 to N. // if (N < 4) return ; // Have to stop the recursion somewhere. // A // First create two new arrays, each of size N /2. Assume this takes O(N) time. long N1 = N / 2 // Integer division long N2 = N – N long [] A1 = new long [N1] // Create an array of size N1, long [] A2 = new long [N2] // Create an array of size N // B // Copy values into these two new arrays. for ( long index = 1; index N1; index++) A1[index] = A[index] ; for ( long index = 1; index N2; index++) A1[index] = A[index + N1] ; // C // Now do the recursive calls. Doofus (A1, N1) ; // Here you should assume that both N1 and N2 are exactly Doofus (A2, N2) ; // equal to N / 2. I am just being picky in my coding. // D // Reassemble the original array A in a decidedly strange fashion. for (long J = 1; J N1; J++) { for (long K = 1; K N2; K++) // Again, assume that N1 = = N2 = = (N / 2) { L = J + K ; A[L] = A[L] + A1[J] * A2[K] ; } } ; // E // Split the big array again. for ( long index = 1; index N1; index++) A1[index] = A[index] ; for ( long index = 1; index N2; index++) A1[index] = A[index + N1] ; // F // Now do the recursive calls again. Doofus (A1, N1) ; // Here you should assume that both N1 and N2 are exactly Doofus (A2, N2) ; // equal to N / 2. I am just being picky in my coding. // G // Now the final assembly of the big array. for ( long index = 1; index N1; index++) A[index] = A1[index] ; for ( long index = 1; index N2; index++) A[index + N1] = A2[index] ; // H // done return ;