Data Stuctues and Algoithm Analysis (CSC317) Randomized algoithms (at 2)
Hiing oblem - eview c Cost to inteview (low i ) Cost to fie/hie (exensive ) n Total numbe candidates m Total numbe hied c h O(c i n + c h m) Deends on ode of candidates! Constant no matte ode of candidates
Randomized hiing oblem(n) 1. andomly emute the list of candidates 2. Hie-Assistant(n) Hie-Assistant(n) 1. best = 0 //least qualified candidate 2. fo i = 1 to n 3. inteview candidate i 4. if candidate i bette than best 5. best = i 6. hie candidate i
Randomized hiing oblem(n) Instead of wost case We would like to know the cost on aveage of hiing new alicants X andom vaiable equal to numbe times new eson hied X = i X i
Randomized hiing oblem(n) X andom vaiable equal to numbe times new eson hied X = Whee i X i X i X i = I{i} ae indicato andom vaiables: 1 if candidate i hied 0 if candidate i not hied Fo notation, we can do the I symbol: X = 1 if candidate i hied i 0 if candidate i not hied
Randomized hiing oblem(n) X andom vaiable equal to numbe times new eson hied X = E X i X i n i=1 [ ] = E X i = n i=1 E [ X ] i Lineaity exectations
Randomized hiing oblem(n) X andom vaiable equal to numbe times new eson hied X = E X i X i n i=1 [ ] = E X i E[ X ] i = P( i) =? = n i=1 E [ X ] i Pob candidate i hied
Randomized hiing oblem(n) * X andom vaiable equal to numbe times new eson hied X = E X n i=1 X i n i=1 [ ] = E X i = n i=1 E [ X ] i E[ X ] i = P( i) = 1 i Pob candidate i hied E[ X] = n 1 =? i i=1 Put back in *
Randomized hiing oblem(n) X andom vaiable equal to numbe times new eson hied X = n i=1 E[ X] = X i n 1 = 1 i 1 + 1 2 + 1 3 +... + 1 n = i=1 ln(n) + O(1) Hamonic seies
Randomized hiing oblem(n) X andom vaiable equal to numbe times new eson hied X = n i=1 E[ X] = X i n 1 = 1 i 1 + 1 2 + 1 3 +... + 1 n = i=1 ln(n) + O(1) Vaious ways to ove: One easy way to see: Hamonic seies
Randomized hiing oblem(n) n 1 = 1 i 1 + 1 2 + 1 3 +... + 1 n = i=1 ln(n) + O(1) Hamonic seies Vaious ways to see: k 1 > n n=1 k+1 1 dx = ln(k +1) x 1
Randomized hiing oblem(n) Cost of hiing new candidate C h So on aveage cost C h ln(n) What about wost case?
Randomized hiing oblem(n) Cost of hiing new candidate C h So on aveage cost C h ln(n) Comae to wost case C h n
Quicksot Inut: n numbes Outut: soted numbes, e.g., in inceasing ode O(n logn) on aveage Sots in lace, unlike Mege sot Elegant
Quicksot Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,)
Quicksot Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) We can see it is a divide and conque, but what is secial?
Patition 1. Pick ivot element in aay A (fo now, we ll choose last element) 2. Reaange A so that elements befoe ivot ae smalle, and elements afte ivot ae lage Pivot Examle: A = [8 6 1 3 7 5 2 4]
Patition Examle: A = [8 6 1 3 7 5 2 4] Afte Patition: A = [1 3 2 4 5 7 6 8] < ivot > ivot Pivot in its ight sot
Patition Animation: htt://www.cs.miami.edu/~but/leaning/csc517.101/wokbook/atition.html
Patition Patition(A,,) 1. x=a[ ] 2. i=-1 3. fo j = to -1 4. if A[ j ] <= x 5. i = I + 1;; 6. swa A[ i ] with A[ j ] 7. swa A[ i+1 ] with A[ ] 8. etun i+1 O(?)
Patition Patition(A,,) 1. x=a[ ] ivot 2. i=-1 3. fo j = to -1 4. if A[ j ] <= x 5. i = i + 1 6. swa A[ i ] with A[ j ] 7. swa A[ i+1 ] with A[ ] 8. etun i+1 O(n) Why?
Patition Patition(A,,) 1. x=a[ ] ivot 2. i=-1 3. fo j = to -1 4. if A[ j ] <= x 5. i = i + 1 6. swa A[ i ] with A[ j ] 7. swa A[ i+1 ] with A[ ] 8. etun i+1 O(n) Why? Fo each j, at most one swa
Patition Reviewing evious mateial Loo invaiant?
Patition i j Loo invaiant? i kees tack of ivot location j kees tack of next element to chk Befoe the fo loo at given j 1. All elements in A[..i] <= ivot 2. All elements in A[i+1..j-1] > ivot 3. A[] = ivot
Patition i j Loo invaiant? i kees tack of ivot location j kees tack of next element to chk i 1 4 6 8 0 3 9 5 j i j A[]: ivot A[j.. 1]: not yet examined A[i+1.. j 1]: known to be > ivot A[.. i]: known to be ivot
Quicksot Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) When will Patition do a bad job?
Quicksot Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) When will Patition do a bad job? When the atition is fo zeo elements vesus n-1 T(n) = T(n 1) + Θ(n) = Θ(n 2 )
Quicksot Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) When will Patition do a good job?
Quicksot q Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) When will Patition do a good job? When the atition is oughly equal in tems of numbes smalle than and geate than ivot T(n) = 2T(n / 2) + Θ(n) = Θ(nlogn) (as in Mege sot)
Quicksot Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) How do we choose the ivot oint?? So that good ON AVERAGE?
Quicksot Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) KEY APPROACH: Choose ivot RANDOMLY
Quicksot Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) Why might a andom choice be good enough? We saw that equal slit good;; What about a 3 to 1, o 9 to 1 slit?
Quicksot q Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) What about a 3 to 1, o 9 to 1 slit?
Quicksot q Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) What about a 9 to 1 slit? T(n) = T(9n /10) + T(n /10) + Θ(n) =?
Quicksot n cn log 10 n 1 10 n 9 10 n 1 n 9 n 9 n 81 n 100 100 100 100 cn cn log 10=9 n 1 81 1000 n 729 1000 n cn cn What about a 9 to 1 slit? 1 cn O.nlg n/ T(n) = T(9n /10) + T(n /10) + Θ(n) = Θ(nlogn)
Quicksot Quicksot(A,, ) 1. If < 2. q = Patition(A,,) 3. Quicksot(A,,q-1) 4. Quicksot(A,q+1,) Why might a andom choice be good enough? Even a 9 to 1, o 3 to 1 slit, is O(n log n) Might not be so had to get on aveage??
Quicksot on aveage un time We ll ove that aveage un time with andom ivots fo any inut aay is O(n log n) Randomness is in choosing ivot Aveage as good as best case! Next class