L03 Randomized Algorithms
L03 Randomized Algorithms
Lecture 3
Prof. Piotr Indyk
Menu
• Tricky part: suppose you need to announce to the adversary the algorithm
you use to choose a cup on each round
• If your algorithm always chooses left, the adversary will always put the
coin on the right, so your total payoff = $0
• Similarly, if you use any other deterministic algorithm
• If you choose a uniformly random cup on every round, then no matter
what the adversary does you are guaranteed expected payoff = $T/2 !
Randomized Algorithms
• Algorithm:
– Choose a random binary vector x=(x1,…,xn),
such that Pr[xi=1]=Pr[xi=0]=½ , i=1…n
– Output YES if ABx=Cx and NO otherwise
• Does it run in O(n2) time ?
– YES, because ABx = A(Bx)
Correctness Analysis
d ? c
x ≠ x
D C
S1
– we have: (d-c)x = Σj≠i(dj-cj)xj + (di-ci)xi
d ? c
x ≠ x
D C
How to be super-lucky?
Lucky-Case Execution
T (100
1
n ) T (100
9
n ) T (100
9
n )T (100
81
n)
Lucky-Case Analysis
cn cn
1
10
cn 9
10
cn cn
log10/9n
1
100
cn 9
100
cn 9
100
cn 81
100
cn cn
…
…
Q(1)
Q(1)
Lucky-Case Analysis
cn cn
1
10
cn 9
cn cn
log10n 10
log10/9n
1
100
cn 9
100
cn 9
100
cn 81
100
cn cn
…
…
Q(1)
Q(1)
cn log10n £ T(n) £ cn log10/9n + O(n)
Lucky-Case Execution
PARANOIDPARTITION
• Repeat:
• choose the pivot to be a random element of the array
• perform PARTITION
until resulting split is “lucky,” ie no worse than 1/10: 9/10
PARANOIDQUICKSORT
Same as QUICKSORT but uses PARANOIDPARTITION
Analysis
• Let T(n) be an upper bound on the expected
running time on any array of n elements
• Consider any input of size n
• The time needed to sort the input is bounded
from the above by a sum of
• The time needed to sort the left subarray
• The time needed to sort the right subarray
• The number of tried partitions until we
get a lucky split, times cn
Expectations
• Therefore:
𝑇 𝑛 ≤ max 𝑇 𝑖 +𝑇 𝑛−𝑖 + 𝐸 #𝑡𝑟𝑖𝑎𝑙𝑠 ⋅ 𝑐𝑛
! &!
$%$
"# "#
where 𝐸 #𝑡𝑟𝑖𝑎𝑙𝑠 is the expected number of random
partitions we need to do to get a lucky split
• We will show that E[#trials] is £ 10/8
• Therefore:
𝑇 𝑛 ≤ max 𝑇 𝑖 +𝑇 𝑛−𝑖 + 10/8 ⋅ 𝑐𝑛
! &!
$%$
"# "#
≤ Θ(𝑛 log 𝑛)
Time until Lucky partition
• Therefore:
𝑇 𝑛 ≤ max 𝑇 𝑖 +𝑇 𝑛−𝑖 + 𝐸 #𝑡𝑟𝑖𝑎𝑙𝑠 ⋅ 𝑐𝑛
! &!
$%$
"# "#
where 𝐸 #𝑡𝑟𝑖𝑎𝑙𝑠 is the expected number of random
partitions we need to do to get a lucky split
• We will show that E[#trials] is £ 10/8
• Therefore:
𝑇 𝑛 ≤ max 𝑇 𝑖 +𝑇 𝑛−𝑖 + 10/8 ⋅ 𝑐𝑛
! &!
$%$
"# "#
≤ Θ(𝑛 log 𝑛)
Quicksort in practice
Linearity of expectation.
Calculating expectation
é n -1 ù
E[T (n)] = E ê å X k (T (k ) + T (n - k - 1) + Q(n) )ú
ëk =0 û
n -1
= å E[ X k (T (k ) + T (n - k - 1) + Q(n) )]
k =0
n -1
= å E[ X k ] × E[T (k ) + T (n - k - 1) + Q(n)]
k =0
£ 2a æç 1 n 2 lg n - 1 n 2 ö÷ + Q(n)
n è2 8 ø
Use fact.
Substitution method
n -1
E [T (n)] £ 2 å ak lg k + Q(n)
n k =2
£ 2a æç 1 n 2 lg n - 1 n 2 ö÷ + Q(n)
n è2 8 ø
= an lg n - æç an - Q(n) ö÷
è 4 ø
Express as desired – residual.
Substitution method
n -1
E [T (n)] £ 2 å ak lg k + Q(n)
n k =2
= 2a æç 1 n 2 lg n - 1 n 2 ö÷ + Q(n)
n è2 8 ø
= an lg n - æç an - Q(n) ö÷
è 4 ø
£ an lg n ,
if a is chosen large enough so that
an/4 dominates the Q(n).
Acks
• Previous version of these slides © Charles Leiserson, Piotr
Indyk
Partitioning subroutine
PARTITION(A, p, r, i)
x ¬ A[ i]
exchange A[ p] « A[i]
i¬p
for j ¬ p + 1 to r
if A[ j] £ x then
i¬i+1
exchange A[i] « A[ j]
exchange A[ p] « A[i]
return i
Loop Invariant: x £x ³x ?
p i j r