# B without solving the recurrence relation prove that

• Notes
• 14

This preview shows pages 8–11. Sign up to view the full content.

(b) Without solving the recurrence relation , prove that the algorithm runs in O ( n 2 ) time. Hint: use strong induction. 19. For each statement below, decide whether it is true or false. In each case attach a very brief expla- nation of your answer. (a) In any tree, for any node x , the sum between the depth of x and the height of x equals the height of the entire tree, true or false? (b) By implementing lists with doubly linked nodes instead of singly linked nodes we can find the element at any given index in constant time, true or false? 20. Show the result of inserting 10,12,1,14,6,5,8,15,3,9,7 in this order in an empty binary min-heap. Circle the final result! 21. Order the following functions by asymptotic growth rate indicating when two or more are big-Theta of each other: n 2 n, 2 2 n - 3 , n 3 , 4 n log n + 2 n, 3 n +3 , 1 + 2 2 + · · · + n 2 , log n n , log n 2 log n 22. Give a big-Oh characterization for the running time of the code fragments belwo, with a short explanation: (a) for( int i = 0; i < n; i++ ) for( int j = i; j <= n; j++ ) for( int k = i; k <= j; k++ ) sum++; (b) static int foo(int[] a) { int s=a[0]; for(int i=2;i<=n;i=i+2) s=s+a[i]; return s; } (c) for( int p = 0; p < n*n; p++ ) for( int q = 0; q < p; q++ ) sum++; 8

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

23. Assume that the concatenation of strings s1 + s2 takes O ( k ) time where k is the size of the longest of s 1 and s 2 . Everything else (constructors, substring), as usual, takes constant time. Analyze the code below and give a big-Oh characterization of its running time. public static String reverse1(String s) { int N = s.length(); String rev = ""; for (int i = 0; i < N; i++) rev = s.charAt(i) + rev; return rev; } 24. 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 n is not O ( n ). 25. For each of the following statements, if true prove using only the definition of big-Oh, if false give a counterexample. (As usual, the functions involved take strictly positive values.) (a) For any f ( n ), g ( n ) is O ( n f ( n )), where g ( n ) = n i =1 f ( i ). (b) For any f ( n ) , g ( n ), if log f ( n ) is O (log g ( n )) then f ( n ) is O ( g ( n )). 26. Suppose 0 < f ( n ) < g ( n ) for all n 1000. For the following statement, if true , give a proof, and if false , give a counterexample. You can assume any of the theorems about Big-Oh stated in the lecture notes. n · f ( n ) + g ( n ) is O ( g ( n ) · log n ) 27. (20 pts) Assume that the concatenation of strings s1 + s2 takes O ( k ) time where k is the size of the longest of s 1 and s 2 . Assume also that the method call s.substring(i, j) takes O ( j - i ) time. Everything else without method calls, as usual, takes constant time. Find a Big-Oh bound for the worst-case running time of the method below by writing the recurrence relation and solving it for N = 2 m . Write the result in terms of N (not m ). public static String reverse2(String s) { int N = s.length(); if (N <= 1) return s; String left = s.substring(0, N/2); String right = s.substring(N/2, N); String revright = reverse2(right); String revleft = reverse2(left); return (revright+revleft); } 28. Consider the following: 9
static int foo(char[] a) { for (int i = a.length-1; i > 0; i=i/2) if (a[i] == ’@’) return 2; return 3; } static void bar(char[] b) { for (int j = 0; j < b.length; j=j+foo(b)) b[j]=0; } Analyze the worst-case running time of bar(b) as a function of n = b.length

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

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