# 12 10 pts a binary min heap contains the keys 1 2 2

• Notes
• 14

This preview shows page 6 - 9 out of 14 pages.

12. (10 pts) A binary min-heap contains the keys 1 , 2 , . . . , 2 13 - 1 , 2 13 . What is the smallest key that can be on a leaf node ? 13. For each statement below, decide whether it is true or false. In each case attach a very brief expla- nation of your answer. (a) If f ( n ) 0 . 001 g ( n ) for all n < 1000 then f ( n ) is O ( g ( n ), true or false? (b) Let A be an algorithm that, for each n > 0, takes less than 1000 steps for all inputs of size n except for three of these inputs, on which it takes an average of n steps. Then, the worst-case running time of A is O ( n ), true or false? (c) Suppose we decide to change our JAVA model of computation by counting, in addition , also 1 step for each argument of each method call. In this new model, a program has the same asymptotic complexity than in our JAVA model of computation, true or false? (d) All the Fibonacci numbers are smaller than 1 , 000 , 000 , 000 , 000 , 000 , 000, true or false? 14. In this problem you NOT allowed to use any of the theorems about Big-Oh stated in the lecture slides, the textbook, or the lab writeups. Your proof should rely only on the definition of Big-Oh. Prove that n +1 n is O ( n ). 15. Consider the algorithm INPUT: array of integers A of length n k n %3 if k = 0 then return 6

Subscribe to view the full document.

else for i 0 to n - 1 do if k = 1 then for j 0 to n - 1 do A [ i ] A [ i ] + A [ j ] end for end if end for return end if (a) On which inputs is the algorithm running in linear time? (b) Analyze the algorithm and give big-Oh characterization of its worst-case running time. 16. Provide counterexamples for each of the following two false statements. You only need to give the counterexample, you don’t need to prove that it works. (As usual in this kind of problem, f ( n ) and g ( n ) map nonnegative reals to strictly positive reals.) (a) For any f ( n ), if f ( n ) is O ( n ) then 2 f ( n ) is O (3 n ). (b) For any f ( n ) and g ( n ), if f ( n ) is O ( g ( n )) then ( f ( n )) n is O (( g ( n )) n ). 17. static char[] too(char[] c) { char[] d = new char[c.length]; for (int k = 0; k < c.length; k++) d[k]=c[k]*c[k]; return d; } static int foo(char[] a) { for (int i = a.length-1; i > 0; i--) a[i]=0; return a.length; } static void bar(char[] b) { for (int j = 1; j < foo(too(b)); j=2*j) b[j]=0; } Analyze the worst-case running time of bar(b) as a function of n = b.length and give a Big-Oh bound. 18. Consider an algorithm sort ( A ) that takes as input an array A of integers. The algorithm works by calling aux (0 , n, A ) where n is the length of A and where aux ( lo, hi, A ) is a recursive algorithm. The arguments lo and hi of aux delimit the portion of the array A that aux sorts, namely A [ lo ] , A [ lo + 1] , . . . , A [ hi - 1]. aux ( lo, hi, A ) works as follows: 1. If hi - lo is 0 or 1, return. Otherwise go to the next step. 2. If hi - lo is 2, put A [ lo ] and A [ hi - 1] in order (swap if needed) then return. Otherwise go to the next step. 7
3. Divide the array portion between lo and hi into three (approximately) equal parts. Call insertion- sort to order the middle third, then recursively call aux for the lower third, and finally recursively call aux for the upper third. (a) Let T ( n ) be the worst-case running time for sort on an array of length n . Write a recurrence relation for T ( n ). On the right side of the recurrence relation do not include the terms of the form c n p where p 0, except for the term of highest degree . Just give the relation, no explanations are required.

Subscribe to view the full document.

{[ snackBarMessage ]}

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
###### "Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

### 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