 # Solutions to Midterm 1 Question 1 Recurrence Relation

• Slides: 12 Solutions to Midterm 1 Question 1 • Recurrence Relation T(n) = 4 T(n/2) + n 2, n 2; T(1) = 1 (a) Height of the recursion tree: Assume n = 2 k height: k = log n. If 2 k-1 < n ≤ 2 k, k - 1< log n ≤ k. Therefore, height is log 2 n. Question 1 (contd) • Recurrence Relation T(n) = 4 T(n/2) + n 2, n 2; T(1) = 1 (b) Amount of work done: top level (level 0) = 1. n 2 bottom level (level k) = 4 k. T(1) = n 2. T(1) (c) How many subproblems at level i? 4 i (d) What is the closed form ? (n 2 log n) Question 1 (contd) • Recurrence Relation T(n) = 2 T(n/2) + n 2, n 2; T(1) = 1 (a) Height of the recursion tree: Assume n = 2 k height: k = log n. If 2 k-1 < n ≤ 2 k, k - 1< log n ≤ k. Therefore, height is log 2 n. Question 1 (contd) • Recurrence Relation T(n) = 2 T(n/2) + n 2, n 2; T(1) = 1 (b) Amount of work done: top level (level 0) = 1. n 2 bottom level (level k) = 2 k. T(1) = n. T(1) (c) How many subproblems at level i? 2 i (d) What is the closed form ? (n 2) Question 1(contd) • Recurrence Relation T(n) = T(3 n/5) + log n, n 2; T(1) = 1 (a) Height of the recursion tree: Assume n = 5/3 k height: k = log 5/3 n. If 5/3 k-1 < n ≤ 5/3 k, k - 1< log 5/3 n ≤ k. Therefore, height is log 5/3 n. Question 1 (contd) • Recurrence Relation T(n) = T(3 n/5) + log n, n 2; T(1) = 1 (b) Amount of work done: top level (level 0) = 1. logn bottom level (level k) = 1. T(1) (c) How many subproblems at level i? 1 (d) What is the closed form ? O(log 2 n) Question 2 (a) log n O( n) : true: logn/ n 0 as n (b) nlog n O( n) : false: nlogn/ n as n (c) nlog n O(n) : true: nlogn/n 0 as n (d) nlog n o(n) : false: nlogn/n as n (e) 3 n (n!) : false: 3 n/n! 0 as n Question 3. We are given a set of n points on a line. We are interested in eliminating the smallest n 0. 25 and the largest n 0. 25 points from the set. (a) Randomized Quicksort: Eliminate (1, n, n 0. 25) Eliminate (l, r, nleft, nright) { Select a random pivot x q partition(l, r, x) if (q-l+1) nleft { mark the points l. . q; Eliminate(q+1, r, nleft-(q-l+1), nright)} else if (r-q+1) nright { mark the points q. . r; Eliminate(l, q-1, nleft, nright -(r-q+1 t)} else {Eliminate(l, q-1, nleft, 0); Eliminate(q+1, r, 0, nright)} Return the unmarked points } Question 3(contd. ) (a) Randomized Quicksort: Eliminate (1, n, n 0. 25) Eliminate (l, r, nleft, nright) { Select a random pivot x q partition(l, r, x) if (q-l+1) nleft { mark the points l. . q; Eliminate(q+1, r, nleft-(q-l+1), nright)} else if (r-q+1) nright { mark the points q. . r; Eliminate(l, q-1, nleft, nright -(r-q+1 t)} else {Eliminate(l, q-1, nleft, 0); Eliminate(q+1, r, 0, nright)} Return the unmarked points } Running time (expected): O(nlog n) (loose) (n) Question 3(contd. ) (b) Max-min heap: Build-Max-Min-heap(A) for i = 1 to n 0. 25 do Extract-Min(A); for i = 1 to n 0. 25 do Extract-Max(A); Report the element present in the heap. Running time: O(n) (to build the heap) +2. n 0. 25. O(log n). Question 4 Suppose that in the analysis of Randomized Quicksort, we consider a pivot good if at least 20 percent of the numbers being sorted are greater than the pivot element and at least 20 percent of the numbers are less than the pivot. We call a pivot bad if it is not good. (a) Observe that every pivot operation fixes one element after the partition operation. Therefore, there will be n pivot operations, whether the pivot is good or not. When the pivot element occupies the middle element (good pivot) we still need n pivot operations and all of them are good. (b) Since Randomized quicksort algorithm has O(nlogn) expected running time, the expected height of the recursion tree should be O(logn). Therefore, the stack height is O(log n) (expected)