CIS
finsolgg

# A analyze this code and write a recurrence relation

• Notes
• 11

This preview shows pages 5–7. Sign up to view the full content.

(a) Analyze this code and write a recurrence relation for the running time T ( n ) of foo where n = c.length Explain your analysis. (b) Solve the recurrence relation and thus give a Big-Oh char- acterization of the running time of foo . static void foo(char[] c) { int n= c.length; if (n<2) return; char[] A = new char[n/2]; for (int i = 0; i < n/2; i++) A[i] = c[i]; foo(A); char[] B = new char[n/2]; for (int j = 0; j < n/2; j++) B[j] = c[n-1-j]; foo(B); for (int k = 0; k < n/2; k++) { c[k] = A[k]; c[n-1-k] = B[k]; } } Answer: (a) Let T ( n ) be the running time of foo(c) where n is c.length . For n = 1 the running time is constant, T (1) = c 1 . Otherwise, the array creation statements take constant time and the bodies of the three loops also take constant time. Each of the loops iterates n/ 2 times therefore each of the loops takes time O ( n ). In addition we have two recursive calls to foo each on an array of size n/ 2. This leads to the recurrence relation T ( n ) = 2 T ( n/ 2) + c 2 n + c 3 where c 2 n + c 3 accounts for the parts that are O ( n ). Here c 1 , c 2 , c 3 are some constants (as usual their exact value is ignored). Moreover, as we have justified in class, solutions that have c 3 = 0 are OK also. Answer: (b) One of several ways to solve the recurrence relation: define S ( m ) = T (2 m ), therefore S (0) = T (1) = c 1 and take also c 3 = 0 as we have explained and rewrite the recurrence relation as S ( m ) = 2 S ( m - 1) + c 2 2 m 5

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

Now write S ( m ) = 2 S ( m - 1) + c 2 2 m S ( m - 1) = 2 S ( m - 2) + c 2 2 m - 1 · · · · · · · · · S (1) = 2 S (0) + c 2 2 1 Now multiply the second equation with 2, the third with 2 2 , . . . , the m ’th (and last one with 2 m - 1 ) add all left and right sides of the equations, cancel the same terms and you get; S ( m ) = 2 m c 1 + c 2 m 2 m Since m = log n we get T ( n ) = S (log n ) = n c 1 + c 2 n log n . Therefore T ( n ) is O ( n log n ). Grading guidelines: (a) 15 points total; 5 points for correct and explicit analysis of for loops; 5 points for correct and explicit analysis of recursive calls; 3 points for correctly stating that the complexity of array creation is O (1); 2 points for correct recurrence relation. (b) 10 points total; 5 points for correctly solving the recurrence; but you lose 2 if no base case stated; finally 5 points for correct final answer O ( nlogn ). 8. (40 pts) For each statement below, decide whether it is true or false. In each case attach a very brief explanation of your answer. (a) In a binary min-heap both the smallest and the largest key are on leaves, true or false? Answer: FALSE. The largest key must be on a leaf but the smallest key is on the root. (b) In a binary search tree both the smallest and the largest key are on leaves, true or false? Answer: FALSE. Neither has to be on a leaf. The smallest key must be a on a node without left child but the node may have a right child. The largest key must be a on a node without right child but the node may have a left child. (c) The worst-case complexity of inserting n keys in an (empty to begin with) hash-table with open addressing and linear probing is O ( n 2 ), true or false?
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