solutionHW#3

# solutionHW#3 - CMPSC 465 SOLUTIONS TO ASSIGNMENT 3 This...

This preview shows pages 1–3. Sign up to view the full content.

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 textbook’s 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 ) = 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern