650 likes | 746 Views
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)
E N D
Selection Find the kth 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) else return randomized-select(A,q+1,r,k-j)
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)
Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k
Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k k
Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k k+1
Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k n-1
Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k n-k+1
Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k n-k+2
Expected running time With probability 1/n, the pivot falls at position j, for j=1,2,…,n k n-1
Max expected running time holds for any input π of size n, so in particular Based on this we’ll prove that T(n) ≤ Dn
Solve the recurrence Assume by induction that for any j < n: Substitute:
More algebra.. If we choose D=8C we get
An alternative analysis • Quicksort-like
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,
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)
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)
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)
i k j
i k j
i j i • k j i k j i • k j i • k j i • k j
i k j In the first double sum we have at most m terms of the form 2/m so it is O(n)
k i j
k i j
k • i j k i j k • i j k • i j k • i j
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)
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
Selection in linear worst case time Blum, Floyd, Pratt, Rivest, and Tarjan (1973)
5-tuples 6 2 9 5 1
Sort the tuples 9 6 5 2 1
Recursively find the median of the medians 9 6 7 5 1 3 10 11 2 2 1
Recursively find the median of the medians 9 6 7 5 1 3 10 11 2 2 1
Partition around the median of the medians 5 Continue recursively with the side that contains the kth element
Neither side can be large 5 ≤ ¾n ≤ ¾n
≥ The reason 9 6 3 1 5 7 2 11 10 2 1
The reason 9 6 3 1 5 7 2 11 10 2 1 ≤
Analysis T(n) = O(n)
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)
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
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
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
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
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
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
Select the k-th smallest from X+Y Can produce the matrix and use our selection algorithm O(n2)