13 Randomized Algorithms
13 Randomized Algorithms
R ANDOMIZED A LGORITHMS
contention resolution
global min cut
linearity of expectation
max 3-satisfiability
universal hashing
Chernoff bounds
load balancing
Lecture slides by Kevin Wayne
Copyright 2005 Pearson-Addison Wesley
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
Randomization
Algorithmic design patterns.
Greedy.
Divide-and-conquer.
Dynamic programming.
Network flow.
Randomization.
in practice, access to a pseudo-random number generator
Randomization. Allow fair coin flip in unit time.
Why randomize? Can lead to simplest, fastest, or only known algorithm for
a particular problem.
Ex. Symmetry breaking protocols, graph algorithms, quicksort, hashing,
load balancing, Monte Carlo integration, cryptography.
P1
P2
.
.
.
Pn
4
= 1/n (1 1/n)
n 1.
between 1/e and 1/2
1 $en %
en
Choose t = e n c ln n:
c ln n
Choose t = e n:
( )
1
e
(1
1 en
en
1
e
= nc
Pr [ F [t] ]
n
"n
%
t
1
= Pr$ F [i, t ] ' Pr[ F [i, t]] n ( 1 en )
# i=1
&
i=1
union bound
Choosing t = 2 en c ln n yields
previous slide
Pr[F[t]] n n-2 = 1 / n.
n
"n %
Pr $ Ei ' Pr[ Ei ]
# i=1 &
i=1
Replace every edge (u, v) with two antiparallel edges (u, v) and (v, u).
Pick some vertex s and compute min s- v cut separating s from each
other vertex v V.
Contraction algorithm
Contraction algorithm. [Karger 1995]
contract u-v
e
f
10
Contraction algorithm
Contraction algorithm. [Karger 1995]
11
Contraction algorithm
Claim. The contraction algorithm returns a min cut with prob 2 / n2.
Pf. Consider a global min-cut (A*, B*) of G.
2 / n.
B*
A*
F*
12
Contraction algorithm
Claim. The contraction algorithm returns a min cut with prob 2 / n2.
Pf. Consider a global min-cut (A*, B*) of G.
2 ! 1 2 1 2
(1 2n ) (1 n1
) ( 4 ) ( 3)
n 2
n3
( n ) ( n 1 ) ! ( 24 ) ( 13 )
2
n(n1)
2
n2
13
Contraction algorithm
Amplification. To amplify the probability of success, run the contraction
algorithm many times.
with independent random choices,
# 2&
%1 2 (
$ n '
n 2 ln n
)# 2 & 12 n 2 , 2ln n
= +%1 2 ( .
e1
+*$ n ' .-
( )
2ln n
1
n2
(1 1/x)x 1/e
14
trial 1
trial 2
trial 3
trial 4
trial 5
(finds min cut)
trial 6
...
Early iterations are less risky than later ones: probability of contracting
an edge in min cut hits 50% when n / 2 nodes remain.
16
Expectation
Expectation. Given a discrete random variables X, its expectation E[X]
is defined by:
E[X ] = j Pr[X = j]
j=0
Waiting for a first success. Coin is heads with probability p and tails with
j=0
j=0
E[X ] = j Pr[X = j] = j (1 p)
j 1 tails
j1
p
p 1 p
1
j
p =
2 =
j (1 p) =
1 p j=0
1 p p
p
1 head
18
j=0
j=0
19
Guessing cards
Game. Shuffle a deck of n cards; turn them over one at a time;
try to guess each card.
Memoryless guessing. No psychic abilities; can't even remember what's
been turned over already. Guess a card from full deck uniformly at random.
Claim. The expected number of correct guesses is 1.
Pf. [ surprisingly effortless using linearity of expectation ]
20
Guessing cards
Game. Shuffle a deck of n cards; turn them over one at a time;
try to guess each card.
Guessing with memory. Guess a card uniformly at random from cards
not yet seen.
Claim. The expected number of correct guesses is (log n).
Pf.
21
Coupon collector
Coupon collector. Each box of cereal contains a coupon. There are n
different types of coupons. Assuming all boxes are equally likely to contain
each coupon, how many boxes before you have 1 coupon of each type?
Claim. The expected number of steps is (n log n).
Pf.
n1
n 1
n
E[X ] = E[X j ] =
= n = n H (n)
j=0
j=0 n j
i=1 i
prob of success = (n j) / n
22
Maximum 3-satisfiability
exactly 3 distinct literals per clause
C1
C2
C3
C4
C5
=
=
=
=
=
x2
x2
x1
x1
x1
x3
x3
x2
x2
x2
x4
x4
x4
x3
x4
24
E[Z ] =
linearity of expectation
E[Z j ]
j=1
k
Pr[clause C j is satisfied]
j=1
7k
8
25
26
= E[Z ] =
j pj
j 0
j pj +
j < 7k /8
j pj
j 7k /8
1) p
( 7k
j + k pj
8
8
j < 7k /8
j 7k /8
( 87 k 18 ) 1 + k p
Rearranging terms yields p 1 / (8k).
27
28
Maximum satisfiability
Extensions.
29
stop algorithm
after a certain point
Remark. Can always convert a Las Vegas algorithm into Monte Carlo,
but no known method (in general) to convert the other way.
30
RP and ZPP
RP. [Monte Carlo] Decision problems solvable with one-sided error in polytime.
can decrease probability of false negative
to 2-100 by 100 independent repetitions
One-sided error.
31
create():
insert(u):
delete(u):
lookup(u):
33
Hashing
Hash function. h : U { 0, 1, , n 1 }.
Hashing. Create an array H of size n. When processing element u,
access array element H[h(u)].
birthday paradox
H[1]
H[2]
jocularly
seriously
null
H[3]
suburban
H[n]
browsing
untravelled
considerating
34
35
Bro server:
36
Hashing performance
Ideal hash function. Maps m elements uniformly at random to m hash slots.
37
Universal hashing
Universal family of hash functions. [Carter-Wegman 1980s]
Pr h H [ h(u) = h(v) ] 1/ n
chosen uniformly at random
Ex. U = { a, b, c, d, e, f }, n = 2.
a
h1(x)
h2(x)
H = {h1, h2}
Pr h H [h(a) = h(b)] = 1/2
not universal
Pr h H [h(a) = h(c)] = 1
Pr h H [h(a) = h(d)] = 0
...
h1(x)
H = {h1, h2 , h3 , h4}
h2(x)
h3(x)
h4(x)
universal
Pr h H [h(a) = h(f)] = 0
...
38
1
n
= | S | 1n 1
universal
(assumes u S)
#r
&
ha (x) = % ai xi ( mod p
$ i=1
'
Hash function family. H = { ha : a A }.
40
ai (xi yi ) mod p
i j
!
##"##
$
m
= 1 / p 1 / n.
41
This implies = .
Bonus fact. Can replace "at most one" with "exactly one" in above fact.
Pf idea. Euclid's algorithm.
42
Now
et(1+) E[e tX ]
Markov's inequality: Pr[X > a] E[X] / a
independence
44
for any 0, 1+ e
Combining everything:
Pr[X > (1+ )]
t(1+)
i E[e
previous slide
t Xi
] e
t(1+)
inequality above
i e
pi (e t 1)
t(1+)
(e t 1)
i pi = E[X]
45
Pf idea. Similar.
Remark. Not quite symmetric since only makes sense to consider < 1.
46
Load Balancing
Load balancing. System in which m jobs arrive in a stream and need to be
processed immediately on m identical processors. Find an assignment that
balances the workload across processors.
Centralized controller. Assign jobs in round-robin manner. Each processor
receives at most m / n jobs.
Decentralized controller. Assign jobs to processors uniformly at random.
How likely is it that some processor is assigned "too many" jobs?
48
Load balancing
Analysis.
Union bound
49
Union bound
1 ]
2
<e
12 (
1 2 (16n ln n)
2
1
= 2
n
1 2/n.
twice the average with probability
50