Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science CS 330: Algorithms Quick Select Gene Itkis.

Similar presentations


Presentation on theme: "Computer Science CS 330: Algorithms Quick Select Gene Itkis."— Presentation transcript:

1 Computer Science CS 330: Algorithms Quick Select Gene Itkis

2 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis2 QuickSelect: random divide & concur QSel(A[], F, L, r):  if F>L then return;  k  Partition(A[],F,L);  if k=r then return A[k]; QSelr  if k>r then return QSel(A[],F,k-1,r); QSelr-k  if k<r then return QSel(A[],k+1,L,r-k); -----------------------  Worst case:  T(n)=O(n)+ T(n-1)  T(n)=O(n 2 )  Best Case:  T(n)=O(n)+ T(n/2)  (or even on 1 st call) T(n)=O(n)  Average: ??? QSort(A[], F, L):  if F>L then return;  k  Partition(A[],F,L);  QSort  QSort(A[], F, k-1);  QSort  QSort(A[], k+1, L); -----------------------  Worst case:  T(n)=O(n)+ T(n-1)  T(n)=O(n 2 )  Best Case: 2 lg n  T(n)=O(n)+ 2T(n/2)  T(n)=O(n lg n)  Average: ???

3 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis3 QSort Analysis Expected Number of Compares  Average performance = Expected Number of Compares  Notation:  z i = i-th smallest element of A[1..n]  C i,j =C j,i = the cost of comparing z i and z j  P i,j = the probability of QSort comparing z i and z j  E[#compares]=  all i,j>i C i,j  P i,j =  all i,j>i P i,j  z i and z j are not compared if for some k: i<k<j, z k is chosen as a pivot before either z i or z j   P i,j =2/(j-i+1)

4 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis4 (  same as QSort!) QSel Analysis (  same as QSort!) AAverage performance = E EE Expected # of Compares NNotation: zz i = i-th smallest element of A[1..n] CC i,j =C j,i = the cost of comparing z i and z j PP i,j = the probability of QSel comparing z i and z j EE[#compares]=  all i,j>i C i,j  P i,j =  all i,j>i P i,j zz i and z j are not compared if for some k: i<k<j, z k is chosen as a pivot before either z i or z j NNNNOT as in QSort: z i and z j are a aa also not compared if for some k: r rr r<k<j, z k is chosen as a pivot before either zr or z j RRecall: zr is the element QSel is looking for

5 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis5 QSel Analysis  Thus, P i,j = 2/(max{|i-j|, |i-r|, |j-r|} +1)  Let D  j= r+D (-r<D<n-r)  i= r+D’ (|D’|<|D|)  Then P i,j < 2/D rzrrzr iziizi jzjjzj kzkkzk D D’ For QSort: P i,j < 2/d; d=|j-i| z i <z j?

6 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis6 QSort Analysis  Thus P i,j +1  E[#compares] =  all i,j>i P i,j =  all i,j>i 2/(j-i+1) i 2/(j-i)  Let j=i+d, where 0<d  n-i. Then 2/d  d=1..n 2/d 2ln nn  E[#compares] i 2/(j-i)=  all i,d 2/d = =  i=1..n  d=1..n-i 2/d <  i=1..n  d=1..n 2/d   2  i=1..n ln n  1.4 n lg n

7 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis7 QSel Analysis  Thus  E[#compares] D,D’ (|D’| i P i,j <  all D,D’ (|D’|<|D|) 2/D ≤  -r<D<n-r  D’= 1-|D|…|D|-1 2/D <  -r<D<n-r 2D 2/D =  -r<D<n-r 4 = 4 n 2 nd  might be “over-counting”: e.g. r=5, D=10, then D’ =-4…9, not -9…9 P i,j 2/d i=1..n  d=1..n 2/d2ln n1.4n lg n E[#compares] =  all i,j>i P i,j =  all i,d 2/d <  i=1..n  d=1..n 2/d  2  i=1..n ln n  1.4n lg n

8 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis8 QSel Analysis  Thus, P i,j = 2/(max{|i-j|, |i-r|, |j-r|} +1)  Let D  j= r+D (-r<D<n-r)  i= r+D’ (|D’|<|D|)  Then P i,j < 2/D rzrrzr iziizi jzjjzj kzkkzk D D’ For QSort: P i,j < 2/d; d=|j-i| z i <z j?

9 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis9 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(n 2 ); Best and Average: O(n lg n)  QSel  Worst case: O(n 2 ); Best and Average: O(n)  Randomized algorithms are COOL & USEFUL !

10 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis10 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?

11 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis11 Hiring Problem  Best case:  Best candidate comes first  Cost: $1k  Probability:  1/N  Worst case:  Best candidate comes last  Not enough – e.g. if 2 nd best comes first, cost=$2k  Worst case: every candidate is hired =>  Candidates come in increasing quality order  Cost: $Nk  Probability:  1/(N!)  Average: ???

12 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis12 Expected Number of Hires  QSort/Qsel – expected # of compares here: expected # of hires E(#hires)  Let P i = probability that i-th candidate is hired  E(#hires) =  all i $1k×P i = $1k×  all i P i =  P i = Prob[i-th hired] = Prob[i-th is best of 1..i]  P i = 1/i P i 1/i $0.7k lg n  E(#hires) = $1k×  all i P i = = $1k×  i=1..N 1/i   $1k× ln n  $0.7k lg n


Download ppt "Computer Science CS 330: Algorithms Quick Select Gene Itkis."

Similar presentations


Ads by Google