cs 330: algorithms quick select
DESCRIPTION
CS 330: Algorithms Quick Select. Gene Itkis. QuickSelect: random divide & concur. Q Sort (A[], F, L): if F>L then return; k Partition (A [],F,L); Q Sort (A[], F, k-1); Q Sort (A[], k+1, L); ----------------------- Worst case: T(n)= O(n) + T(n-1) T(n)=O(n 2 ) Best Case: - PowerPoint PPT PresentationTRANSCRIPT
Computer Science
CS 330: AlgorithmsQuick Select
Gene Itkis
Computer Science
CS-330: Algorithms, Fall 2008 Gene Itkis 2
QuickSelect: random divide & concur
QQSelSel(A[], F, L, r):(A[], F, L, r): if F>L then return; k Partition(A[],F,L); if k=r then return A[k]; if k>r then return
QQSelSel(A[],F,k-1,rr); if k<r then return
QQSelSel(A[],k+1,L,r-kr-k);----------------------- Worst case:Worst case:
T(n)=O(n)+ T(n-1)
T(n)=O(n2) Best Case:Best Case:
T(n)=O(n)+ T(n/2) (or even on 1st call)
T(n)=O(n) Average: Average: ??????
QQSortSort(A[], F, L):(A[], F, L): if F>L then return; k Partition(A[],F,L);
QQSortSort(A[], F, k-1);
QQSortSort(A[], k+1, L);----------------------- Worst case:Worst case:
T(n)=O(n)+ T(n-1)
T(n)=O(n2) Best Case:Best Case:
T(n)=O(n)+ 22T(n/2)
T(n)=O(n lg nlg n) Average: Average: ??????
Computer Science
CS-330: Algorithms, Fall 2008 Gene Itkis 3
QSort Analysis
Average performance = Expected Number of ComparesExpected Number of Compares
Notation: zi = i-th smallest element of A[1..n]
Ci,j=Cj,i = the cost of comparing zi and zj
Pi,j = the probability of QSort comparing zi and zj
E[#compares]= all i,j>i Ci,j Pi,j = all i,j>i Pi,j
zi and zj are not compared if for some k: i<k<j, zk is chosen as a pivot before either zi or zj
Pi,j=2/(j-i+1)
Computer Science
CS-330: Algorithms, Fall 2008 Gene Itkis 4
QSel Analysis ((same as same as QSort!)QSort!)
Average performance = Expected # of ComparesExpected # of Compares Notation:
zi = i-th smallest element of A[1..n]
Ci,j=Cj,i = the cost of comparing zi and zj
Pi,j = the probability of QSel comparing zi and zj
E[#compares]= all i,j>i Ci,j Pi,j = all i,j>i Pi,j
zi and zj are not compared if for some k: i<k<j, zk is chosen as a pivot before either zi or zj
NOT as in QSortNOT as in QSort: zi and zj are alsoalso not compared if for some k: rr<k<j, zk is chosen as a pivot before either zrr or zj
Recall: zrr is the element QSel is looking for
Computer Science
CS-330: Algorithms, Fall 2008 Gene Itkis 5
QSel Analysis
Thus, Pi,j = 2/(max{|i-j|, |i-r|, |j-r|} +1)
Let j= r+DD (-r<D<n-r)
i= r+D’ (|D’|<|D|)
Then
Pi,j < 2/D
zrr ziizjjzkk
D
D’
For QSort:
Pi,j < 2/d; d=|j-i|
zi<zj
??
Computer Science
CS-330: Algorithms, Fall 2008 Gene Itkis 6
QSort Analysis
Thus E[#compares] = all i,j>i PPi,ji,j = all i,j>i 2/(j-i+1+1)
< all i,j>i 2/(j-i)
Let j=i+d, where 0<d n-i. Then
E[#compares] < all i,j>i 2/(j-i)= all i,d 2/d2/d =
= i=1..n d=1..n-i 2/d < i=1..n d=1..n d=1..n 2/d2/d
22i=1..n ln nln n 1.4 nn lg n
Computer Science
CS-330: Algorithms, Fall 2008 Gene Itkis 7
QSel Analysis
Thus
E[#compares]E[#compares] = all i,j>i Pi,j
< all D,D’ (|D’|<|D|)D,D’ (|D’|<|D|) 2/DD
≤ -r<-r<DD<n-r<n-r D’= 1-|D|…|D|-1D’= 1-|D|…|D|-1 2/D2/D
< -r<-r<DD<n-r<n-r 2D 2/D2/D
= -r<-r<DD<n-r<n-r 4
= 4 nn
2nd might be “over-counting”: e.g. r=5, D=10, then D’=-4…9, not -9…9
E[#compares] = all i,j>i PPi,ji,j =all i,d 2/2/dd <
i=1..ni=1..nd=1..nd=1..n2/d2/d 22i=1..nln nln n 1.4n lg n1.4n lg n
Computer Science
CS-330: Algorithms, Fall 2008 Gene Itkis 9
Conclusion
Can find r-th smallest element in linear time: O(n) E.g. median (useful for hw and tests ;-) No need to sort – works faster without sorting
Murphy is not always right Sometimes best case is more common than
worst: QSort
Worst case: O(n2); Best and Average: O(n lg n)
QSel Worst case: O(n2); Best and Average: O(n)
Randomized algorithms are COOL & USEFUL!
Computer Science
CS-330: Algorithms, Fall 2008 Gene Itkis 10
Hiring Problem
Another example of the above principles Problem
N candidates come for a job (at large intervals) You are the big boss You want the best, but do not want to wait
Your hiring strategy – greedy: Get the best you’ve seen so far (i.e. better than the current
choice) Assume: easy to compare candidates/workers
Each “fire current & hire new” procedure costs $1K How much you will spend eventually?
Computer Science
CS-330: Algorithms, Fall 2008 Gene Itkis 11
Hiring Problem
Best case: Best candidate comes first Cost: $1k Probability:
1/N
Worst case: Best candidate comes last
Not enough – e.g. if 2nd best comes first, cost=$2k Worst case: every candidate is hired =>
Candidates come in increasing quality order Cost: $Nk Probability:
1/(N!)
Average: ???Average: ???
Computer Science
CS-330: Algorithms, Fall 2008 Gene Itkis 12
Expected Number of Hires
QSort/Qsel – expected # of compareshere: expected # of hires E(#hires)
Let Pi= probability that i-th candidate is hired
E(#hires) = all i $1k×Pi = $1k×all i Pi=
Pi= Prob[i-th hired] = Prob[i-th is best of 1..i]
PPii = 1/i = 1/i
E(#hires) = $1k×all i PPi i = = $1k×i=1..N 1/i 1/i $1k× ln n $0.7k lg n$0.7k lg n