1 / 65

Selection

Selection. Find the k th element. Randomized selection. Randomized-select (A, p, r,k) if p=r then return A[p] q ←randomized-partition(A,p,r) j ← q-p+1 if j=k then return A[q] else if k < j then return randomized-select (A,p,q-1,k)

jonny
Download Presentation

Selection

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. Selection Find the kth element

  2. Randomized selection Randomized-select (A, p, r,k) if p=r then return A[p] q←randomized-partition(A,p,r) j ← q-p+1 if j=k then return A[q] else if k < j then return randomized-select(A,p,q-1,k) else return randomized-select(A,q+1,r,k-j)

  3. Analyzing it in 2 ways • Writing a recurrence relation • Quicksort-like Recall: X(π) is a rv that counts the # of comparisons for a fixed input π We’ll write a recurrence on T(n) = maxπ,|π|=nE(X(π)) (The max is over all inputs πof size n)

  4. Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k

  5. Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k k

  6. Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k k+1

  7. Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k n-1

  8. Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k n-k+1

  9. Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k n-k+2

  10. Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k n-1

  11. Max expected running time holds for any input π of size n, so in particular Based on this we’ll prove that T(n) ≤ Dn

  12. Solve the recurrence Assume by induction that for any j < n: Substitute:

  13. Some algebra…

  14. More algebra.. If we choose D=8C we get

  15. An alternative analysis • Quicksort-like

  16. Expected # of comparisons Let z1,z2,.....,zn the elements in sorted order Let Xij = 1 if zi is compared to zj and 0 otherwise So,

  17. Case 1: i≤k<j i k j Compare zi and zjiff the first element picked from zi,zi+1,…,zj is either zi or zj  Pr{zicompared to zj} = 2/(j-i+1)

  18. Case 2: k<i<j k • i j Compare zi and zjiff the first element picked from zk,zi+1,…,zj is either zi or zj  Pr{zicompared to zj} = 2/(j-k+1)

  19. Case 3: i<j≤k i j k Compare zi and zjiff the first element picked from zi,zi+1,…,zkis either zi or zj  Pr{zicompared to zj} = 2/(k-i+1)

  20. i k j

  21. i k j

  22. i j i • k j i k j i • k j i • k j i • k j

  23. i k j In the first double sum we have at most m terms of the form 2/m  so it is O(n)

  24. k i j

  25. k i j

  26. k • i j k i j k • i j k • i j k • i j

  27. k i j Also in the second double sum we have at most m terms of the form 2/m  so it is O(n)

  28. k i j Also in the second double sum we have at most m terms of the form 2/m  so it is O(n) Similar analysis for the last sum

  29. Selection in linear worst case time Blum, Floyd, Pratt, Rivest, and Tarjan (1973)

  30. 5-tuples 6 2 9 5 1

  31. Sort the tuples 9 6 5 2 1

  32. Recursively find the median of the medians 9 6 5 2 1

  33. Recursively find the median of the medians 9 6 7 5 1 3 10 11 2 2 1

  34. Recursively find the median of the medians 9 6 7 5 1 3 10 11 2 2 1

  35. Partition around the median of the medians 5 Continue recursively with the side that contains the kth element

  36. Neither side can be large 5 ≤ ¾n ≤ ¾n

  37. The reason 9 6 3 1 5 7 2 11 10 2 1

  38. The reason 9 6 3 1 5 7 2 11 10 2 1 ≤

  39. Analysis T(n) = O(n)

  40. More fun with selection Weighted selection: • Each item j has weight w(j) • Given some target weight Z, find the item k such that ∑i<kw(i) < Z and ∑i<kw(i) + w(k) ≥ Z • Typical choice for Z is W/2 where W=∑iw(i)

  41. Weighted selection W=347, Z=173.5 m a b b d c g d e e f f c g • p • h • i • i • j • k o k • l • l • a • m • n • n • j • o • p • h 41 3 45 45 20 1 5 1 11 11 38 38 20 5 • 8 • 33 • 15 • 15 • 18 • 17 23 18 • 60 • 60 • 3 • 41 • 9 • 9 • 23 • 17 • 33 • 8 • 123 • 156 • 171 • 188 • 206 • 266 • 307 • 316 • 339 • 347 118 3 48 68 69 80

  42. Weighted selection W=347, Z=173.5 m b d g e f c • p • i • k o • l • a • n • j • h 41 45 1 5 11 38 20 • 8 • 15 • 18 23 • 60 • 3 • 9 • 17 • 33 • 123 • 156 • 171 • 188 • 206 • 266 • 307 • 316 • 339 • 347 118 3 48 68 69 80 a b c d e f g • h • i • j k • l • m • n • o • p 3 45 20 1 11 38 5 • 33 • 15 • 17 18 • 60 • 41 • 9 • 23 • 8

  43. Weighted selection • Find the median: m b d g e f c • p • i • k o • l • a • n • j • h 41 45 1 5 11 38 20 • 8 • 15 • 18 23 • 60 • 3 • 9 • 17 • 33

  44. Weighted selection • Sum up the smaller ones: m b d g e f c • p • i • k o • l • a • n • j • h 41 45 1 5 11 38 20 • 8 • 15 • 18 23 • 60 • 3 • 9 • 17 • 33 • 123 • 156 • 171 • 188 • 206 • 266 • 307 • 316 • 339 • 347 118 3 48 68 69 80 a b c d e f g • h • i • j k • l • m • n • o • p 3 45 20 1 11 38 5 • 33 • 15 • 17 18 • 60 • 41 • 9 • 23 • 8

  45. Weighted selection • Recur in the appropriate half: m b d g e f c • p • i • k o • l • a • n • j • h 41 45 1 5 11 38 20 • 8 • 15 • 18 23 • 60 • 3 • 9 • 17 • 33 • 123 • 156 • 171 • 188 • 206 • 266 • 307 • 316 • 339 • 347 118 3 48 68 69 80 a b c d e f g • h • i • j k • l • m • n • o • p 3 45 20 1 11 38 5 • 33 • 15 • 17 18 • 60 • 41 • 9 • 23 • 8

  46. Analysis • n + n/2 + n/4 …… = O(n) m b d g e f c • p • i • k o • l • a • n • j • h 41 45 1 5 11 38 20 • 8 • 15 • 18 23 • 60 • 3 • 9 • 17 • 33 • 123 • 156 • 171 • 188 • 206 • 266 • 307 • 316 • 339 • 347 118 3 48 68 69 80 a b c d e f g • h • i • j k • l • m • n • o • p 3 45 20 1 11 38 5 • 33 • 15 • 17 18 • 60 • 41 • 9 • 23 • 8

  47. Select the k-th smallest from X+Y

  48. Select the k-th smallest from X+Y Can produce the matrix and use our selection algorithm  O(n2)

More Related