This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CMPSC 465 SOLUTIONS TO ASSIGNMENT 3 Spring 2008 This assignment is worth a total of 100 points. 1. [15 points] Exercise 4.1.1 on page 128, under the assumption that n is a power of 2. Grading: a. (8 points) The algorithm is required to return the position of the maximum, not its value, and it has to return a correct answer in the base case as well as in the recursive case. (As explained in the instructions, mimicking the textbooks code for Mergesort on page 128 is not an acceptable solution.) b. (1 point) Since the answer to (b) depends on the answer to (a), the answer to (b) may be different from the answer below. c. (5 points) According to the instructions, you are expected to use one of the substitution methods [forward or backward substitution] demonstrated in Appendix B. It is not sufficient to merely guess the answer . . . d. (1 point) a. ALGORITHM Max ( A [ l..r ]) //Returns index of maximum element in A [ l..r ] if r = l return r m b ( l + r ) / 2 c i Max ( A [ l..m ]) j Max ( A [ m + 1 ..r ]) if A [ i ] < A [ j ] i j return i b. This algorithm will return the index of the leftmost occurrence of the maximum value, since, when locations i and j contain equal values, it chooses location i . c. Recurrence: C ( n ) = 2 C ( n/ 2) + 1 for n > 1, and C (1) = 0. Solution: C (2 ) = 0 C (2 1 ) = 2 0 + 1 = 1 C (2 2 ) = 2 1 + 1 C (2 3 ) = 2 2 1 + 2 1 + 1 C (2 4 ) = 2 3 1 + 2 2 1 + 2 1 + 1 . . . C (2 k ) = 2 k- 1 1 + 2 k- 2 1 + + 2 1 + 1 C (2 k ) = 2 k- 1 so C ( n ) = n- 1 for n = 2 k . d. Since the brute-force algorithm makes exactly n- 1 data comparisons, the divide-and-conquer algorithm makes the same number of comparisons as the brute-force algorithm. 1 2. [25 points] Exercise 4.1.2 on page 128, under the assumption that n is a power of 2. Grading: a. (15 points) As indicated by the hint on page 508, in this exercise, unlike the previous one, the divide-and- conquer technique will reduce the number of comparisons if implemented well. That is why the instructions require your algorithm to make just one comparison when n = 2, which is why we need to make n = 2 one of the base cases. If, instead, we were to handle this case recursively, we would be making two comparisons.of the base cases....
View Full Document
- Spring '08