1 / 13

Finding Medians

Explore divide and conquer solutions, complexity analysis, and constructive induction in finding medians in arrays. Learn selection algorithms for finding the s-th smallest element efficiently. Approximate the median with linear time complexity.

edithm
Download Presentation

Finding Medians

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. Finding Medians Lecture 09 CS 312

  2. Objectives • See another example of a divide and conquer solution. • See another example of complexity analysis of a divide and conquer algorithm. • See another example of a proof by constructive induction.

  3. Finding the Median •  (n log n) is easy. • Can we do better?

  4. Selection problem • Given an array of n elements, find the s-th smallest element. 3 1 4 1 5 9 6 7 3 2 9 3rd smallest is 2 6th smallest is 4 1 1 2 3 3 4 5 6 7 9 9

  5. Selection and median The selection problem can be used to solve the median problem The median problem can be used to solve the selection problem

  6. Selection Algorithm selection (T, 3): 3 1 4 1 5 9 6 7 3 2 9 pick p = 3 k l 1 2 1 3 3 5 9 6 7 4 9 3 <= 3 (s <= k) j := 3 1 2 1 function selection (T[1..n],s) i := 1 ; j := n repeat p := median (T[i..j]) pivotbis (T[i..j],p,k,l) if s <= k then j := k else if s >= l then i := l else return p

  7. Selection Algorithm function selection (T[1..n],s) i := 1 ; j := n repeat p := median (T[i..j]) pivotbis (T[i..j],p,k,l) if s <= k then j := k else if s >= l then i := l else return p There is one problem with this algorithm. What is it?

  8. Still have to find the median • use T[1], as in Quicksort • linear solution to selection on average • O(n2) in the worst case • approximate the median • still linear on average • but linear in worst-case too

  9. Approximating the median function pseudomed (T[1..n]) if n<= 5 then return adhocmed(T) z := floor (n/5) array Z[1..z] for i := 1 to z do Z[i] := adhocmed(T[5i-4..5i]) return selection (Z,ceil(z/2)) find the median of each 5-element subarray, find the median of each of those medians. about 3n/10 elements below and 7n/10 elements above

  10. Selection Algorithm function selection (T[1..n],s) i := 1 ; j := n repeat p := pseudomedian (T[i..j]) pivotbis (T[i..j],p,k,l) if s <= k then j := k else if s >= l then i := l ; else return p glossing over some details, we get... t(n) <= dn + t(floor(n/5)) + max{t(m)|m<= ((7n+12)/10)}

  11. Constructive Induction, again • Why is this a good candidate for constructive induction? c.n.t(n) <= c n For Fall 2002, don’t worry too much about constructive induction here. The basic idea is that we don’t know c, so we just guess and see if the induction goes through.

  12. Constructive Induction, again • Why is this a good candidate for constructive induction? • nothing easier works • can induct on n • need a value for c c.n.(n) <= c n

  13. Setting up the proof Basis case: For any n between 0 and the threshold n0, show that there exists a c such that t(n) <= cn Inductive step: Assume that t(m) <= cm for any integer m between 1 and n, then constrain c so that t(n) <= cn. The details of this proof are interesting, but not real important for this class.

More Related