# hw3 - Duygu Karaolan 6909 03.11.2009 CS301 Algorithms...

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

Duygu Karaoğlan 03.11.2009 6909 1 CS301 – Algorithms Homework 3 Question 1) Computing 2 n (a) Design a recursive algorithm for computing 2 n (for n > 1) based on the following formula:2 n = 2 n−1 + 2 n−1 , where 2 0 is known to be 1. However, suppose that you are using a funny programming language where no local variables can be defined and only the addition operation is allowed. int pow2 (int n ) { if( n == 0) return 1; else return pow2 ( n -1) + pow2 ( n -1); } (b) Set up a recurrence relation for the number of additions made by the algorithm and solve it. T(0) = 0, T(1) = 1, T(2) = 3, T(3) = 7 ... T( n ) = 2T( n -1) + 1 = 2(T( n -2) + 1) + 1 = 2T( n -2) + 3 = 2(T( n -3) + 3) + 1 = 2T( n -3) + 7 = 2(T( n -4) + 7) + 1 = 2T( n -4) + 15 = 2(T( n -5) + 15) + 1 = 2T( n -5) + 31 ... = 2 n – 1 = O(2 n ) (c) Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. f(n) f(n-1) f(n-1) f(n-2) f(n-2) f(n-2) f(n-2) ... ... ... ... ... f(0) f(0) f(0) f(0) ... ... ... f(0) f(0) f(0) f(0) There are 2 n leaves of the tree represented above and f(0) = 1. Thus, the number of calls made by the algorithm is O(2 n ).

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

View Full Document
Duygu Karaoğlan 03.11.2009 6909 2 (d) How would you modify the algorithm if the programming language allowed multiplication as well? Give the formula on which you would base your algorithm and the recurrence relation for the number of operations (addition and/or multiplication, whatever you are using in your algorithm) and solve the recurrence. int pow2 (int n ) { if( n == 0) return 1; else return 2* pow2 ( n -1); } # of additions barb2right none # of multiplications barb2right T(0) = 0, T(1) = 1, T(2) = 2, T(3) = 3 ... T( n ) = T( n -1) + 1 = T( n -2) + 1 + 1 = T( n -2) + 2 = T( n -3) + 2 + 1 = T( n -3) + 3 = T( n -4) + 2 + 1 = T( n -4) + 4 ... = T( n - k ) + k Let n = k barb2right T( n ) = T(0) + n = n = O( n ) Question 2) Discuss why the stable version of counting sort is stable. (Your answer shouldn’t be as simple as “We wouldn’t be calling it the stable version of counting sort if it weren’t stable”.) In the stable version of counting sort we have an array that holds how many times a number occurs and we have another array that holds the last occurance of a number. The sorting is
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