120 likes | 335 Views
Solutions to Midterm 1. Question 1. Recurrence Relation T(n) = 4T(n/2) + n 2, n 2 ; T(1) = 1 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
E N D
Question 1 • Recurrence Relation T(n) = 4T(n/2) + n2, n 2; T(1) = 1 • Height of the recursion tree: Assume n = 2k height: k = log n. If 2k-1 < n ≤ 2k, k - 1< log n ≤ k. Therefore, height is log2n.
Question 1 (contd) • Recurrence Relation T(n) = 4T(n/2) + n2, n 2; T(1) = 1 (b) Amount of work done: top level (level 0) = 1 . n2 bottom level (level k) = 4k . T(1) = n2 . T(1) (c) How many subproblems at level i? 4i (d) What is the closed form ? (n2 log n)
Question 1 (contd) • Recurrence Relation T(n) = 2T(n/2) + n2, n 2; T(1) = 1 • Height of the recursion tree: Assume n = 2k height: k = log n. If 2k-1 < n ≤ 2k, k - 1< log n ≤ k. Therefore, height is log2n.
Question 1 (contd) • Recurrence Relation T(n) = 2T(n/2) + n2, n 2; T(1) = 1 (b) Amount of work done: top level (level 0) = 1 . n2 bottom level (level k) = 2k . T(1) = n . T(1) (c) How many subproblems at level i? 2i (d) What is the closed form ? (n2)
Question 1(contd) • Recurrence Relation T(n) = T(3n/5) + log n, n 2; T(1) = 1 • Height of the recursion tree: Assume n = 5/3k height: k = log5/3 n. If 5/3k-1 < n ≤ 5/3k, k - 1< log5/3 n ≤ k. Therefore, height is log5/3n.
Question 1 (contd) • Recurrence Relation T(n) = T(3n/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) = 1 . T(1) (c) How many subproblems at level i? 1 (d) What is the closed form ? O(log2 n)
Question 2 • 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) 3n (n!) : false: 3n/n! 0 as n
Question 3. We are given a set of n points on a line. We are interested in eliminating the smallest n0.25 and the largest n0.25 points from the set. (a) Randomized Quicksort: Eliminate(1,n,n0.25,n0.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+1t)} 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,n0.25,n0.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+1t)} 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 n0.25do Extract-Min(A); for i = 1 to n0.25do Extract-Max(A); Report the element present in the heap. Running time: O(n) (to build the heap) +2. n0.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. • 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. • 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)