1 / 12

Solutions to Midterm 1

Explore the intricacies of recurrence relations in algorithms and analyze the efficiency through detailed examples and calculations. Gain insights into key concepts like recursion tree height and work distribution levels.

Download Presentation

Solutions to Midterm 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Solutions to Midterm 1

  2. 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.

  3. 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)

  4. 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.

  5. 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)

  6. 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.

  7. 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)

  8. 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  

  9. 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 }

  10. 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)

  11. 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).

  12. 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)

More Related