








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









2
Algorithmic design patterns.
Greedy.
Divide-and-conquer.
Dynamic programming.
Network flow.
Randomization.
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.
in practice, access to a pseudo-random number generator
4
Contention resolution. Given n processes P
1
n
, each competing for
access to a shared database. If two or more processes access the
database simultaneously, all processes are locked out. Devise protocol
to ensure all processes get through on a regular basis.
Restriction. Processes can't communicate.
Challenge. Need symmetry-breaking paradigm.
P
1
P
2
P
n
.
.
.
5
Protocol. Each process requests access to the database at time t with
probability p = 1/n.
Claim. Let S[i, t] = event that process i succeeds in accessing the
database at time t. Then 1/(e ⋅ n) ≤ Pr[S(i, t)] ≤ 1/(2n).
Pf. By independence, Pr[S(i, t)] = p (1-p)
n-
.
Setting p = 1/n, we have Pr[S(i, t)] = 1/n (1 - 1/n)
n-
. ▪
Useful facts from calculus. As n increases from 2, the function:
(1 - 1/n)
n-
converges monotonically from 1/4 up to 1/e
(1 - 1/n)
n-
converges monotonically from 1/2 down to 1/e.
process i requests access none of remaining n-1 processes request access
value that maximizes Pr[S(i, t)] between 1/e and 1/
6
Claim. The probability that process i fails to access the database in
en rounds is at most 1/e. After e⋅n(c ln n) rounds, the probability is at
most n
-c
.
Pf. Let F[i, t] = event that process i fails to access database in rounds
1 through t. By independence and previous claim, we have
Pr[F(i, t)] ≤ (1 - 1/(en))
t
.
Choose t = e ⋅ n:
Choose t = e ⋅ n c ln n:
Pr[ F ( i , t )] " 1 #
1
en
( )
en $ %
1
en
( )
en
1
e
Pr[ F ( i , t )] "
1
e
( )
c ln n
= n
7
Claim. The probability that all processes succeed within 2e ⋅ n ln n
rounds is at least 1 - 1/n.
Pf. Let F[t] = event that at least one of the n processes fails to access
database in any of the rounds 1 through t.
Choosing t = 2 en c ln n yields Pr[F[t]] ≤ n · n
= 1/n. ▪
Union bound. Given events E
1
n
Pr E
i
i = 1
n
( Pr[ E
i
i = 1
n
Pr F [ t ] [ ]
= Pr F [ i , t ]
i = 1
n
( Pr[ F [ i , t ]]
i = 1
n
) ( n 1 *
1
en
( )
t
union bound previous slide
13
Amplification. To amplify the probability of success, run the
contraction algorithm many times.
Claim. If we repeat the contraction algorithm n
2
ln n times with
independent random choices, the probability of failing to find the
global min-cut is at most 1/n
2
.
Pf. By independence, the probability of failure is at most
n
2
n
2
ln n
n
2
1
2
n
2
2 ln n
/ e
" 1
( )
2 ln n
n
2
(1 - 1/x)
x
≤ 1/e
14
Remark. Overall running time is slow since we perform Θ(n
2
log n)
iterations and each takes Ω(m) time.
Improvement. [Karger-Stein 1996] O(n
2
log
3
n).
Early iterations are less risky than later ones: probability of
contracting an edge in min cut hits 50% when n / √ 2 nodes remain.
Run contraction algorithm until n / √ 2 nodes remain.
Run contraction algorithm twice on resulting graph, and return best of
two cuts.
Extensions. Naturally generalizes to handle positive weights.
Best known. [Karger 2000] O(m log
3
n).
faster than best known max flow algorithm or
deterministic global min cut algorithm
16
Expectation. Given a discrete random variables X, its expectation E[X]
is defined by:
Waiting for a first success. Coin is heads with probability p and tails
with probability 1-p. How many independent flips X until first heads?
E [ X ] = j Pr[ X = j ]
j = 0
"
E [ X ] = j " Pr[ X = j ]
j = 0
= j ( 1 % p )
j % 1
p
j = 0
p
1 % p
j ( 1 % p )
j
j = 0
p
1 % p
1 % p
p
2
p
j-1 tails 1 head
17
Useful property. If X is a 0/1 random variable, E[X] = Pr[X = 1].
Pf.
Linearity of expectation. Given two random variables X and Y defined
over the same probability space, E[X + Y] = E[X] + E[Y].
Decouples a complex calculation into simpler pieces.
E [ X ] = j " Pr[ X = j ]
j = 0
= j " Pr[ X = j ]
j = 0
1
= Pr[ X = 1 ]
not necessarily independent
18
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)
Let X
i
= 1 if i
th
prediction is correct and 0 otherwise.
Let X = number of correct guesses = X
1
n
i
] = Pr[X
i
= 1] = 1/n.
1
n
] = 1/n + … + 1/n = 1. ▪
linearity of expectation
19
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.
Let X
i
= 1 if i
th
prediction is correct and 0 otherwise.
Let X = number of correct guesses = X
1
n
i
] = Pr[X
i
= 1] = 1 / (n - i - 1).
1
n
] = 1/n + … + 1/2 + 1/1 = H(n). ▪
ln(n+1) < H(n) < 1 + ln n linearity of expectation
20
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.
Phase j = time between j and j+1 distinct coupons.
Let X
j
= number of steps you spend in phase j.
Let X = number of steps in total = X
0
1
n-
j
j = 0
n " 1
n
n " j
j = 0
n " 1
= n
i
i = 1
n
= n H ( n )
prob of success = (n-j)/n
⇒ expected waiting time = n/(n-j)
25
Q. Can we turn this idea into a 7/8-approximation algorithm? In
general, a random variable can almost always be below its mean.
Lemma. The probability that a random assignment satisfies ≥ 7k/
clauses is at least 1/(8k).
Pf. Let p
j
be probability that exactly j clauses are satisfied; let p be
probability that ≥ 7k/8 clauses are satisfied.
Rearranging terms yields p ≥ 1 / (8k). ▪
7
8
k = E [ Z ] = j p
j
j " 0
= j p
j
j
j " 7 k / 8
j < 7 k / 8
7 k
8
1
8
) p
j
j
j " 7 k / 8
j < 7 k / 8
7
8
k %
1
8
) & 1 + k p
26
Johnson's algorithm. Repeatedly generate random truth assignments
until one of them satisfies ≥ 7k/8 clauses.
Theorem. Johnson's algorithm is a 7/8-approximation algorithm.
Pf. By previous lemma, each iteration succeeds with probability at
least 1/(8k). By the waiting-time bound, the expected number of trials
to find the satisfying assignment is at most 8k. ▪
27
Extensions.
Allow one, two, or more literals per clause.
Find max weighted set of satisfied clauses.
Theorem. [Asano-Williamson 2000] There exists a 0.784-
approximation algorithm for MAX-SAT.
Theorem. [Karloff-Zwick 1997, Zwick+computer 2002] There exists a
7/8-approximation algorithm for version of MAX-3SAT where each
clause has at most 3 literals.
Theorem. [Håstad 1997] Unless P = NP, no ρ-approximation algorithm
for MAX-3SAT (and hence MAX-SAT) for any ρ > 7/8.
very unlikely to improve over simple randomized
algorithm for MAX-3SAT
28
Monte Carlo algorithm. Guaranteed to run in poly-time, likely to find
correct answer.
Ex: Contraction algorithm for global min cut.
Las Vegas algorithm. Guaranteed to find correct answer, likely to run
in poly-time.
Ex: Randomized quicksort, Johnson's MAX-3SAT algorithm.
Remark. Can always convert a Las Vegas algorithm into Monte Carlo,
but no known method to convert the other way.
stop algorithm after a certain point
29
RP. [Monte Carlo] Decision problems solvable with one-sided error in
poly-time.
One-sided error.
If the correct answer is no, always return no.
If the correct answer is yes, return yes with probability ≥ ½.
ZPP. [Las Vegas] Decision problems solvable in expected poly-time.
Theorem. P ⊆ ZPP ⊆ RP ⊆ NP.
Fundamental open questions. To what extent does randomization help?
Does P = ZPP? Does ZPP = RP? Does RP = NP?
Can decrease probability of false negative
to 2
by 100 independent repetitions
running time can be unbounded, but
on average it is fast
31
Dictionary. Given a universe U of possible elements, maintain a subset
S ⊆ U so that inserting, deleting, and searching in S is efficient.
Dictionary interface.
Create() : Initialize a dictionary with S = φ.
Insert(u) : Add element u ∈ U to S.
Delete(u) : Delete u from S, if u is currently in S.
Lookup(u) : Determine whether u is in S.
Challenge. Universe U can be extremely large so defining an array of
size |U| is infeasible.
Applications. File systems, databases, Google, compilers, checksums
P2P networks, associative arrays, cryptography, web caching, etc.
32
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)].
Collision. When h(u) = h(v) but u ≠ v.
A collision is expected after Θ(√n) random insertions. This
phenomenon is known as the "birthday paradox."
Separate chaining: H[i] stores linked list of elements u with h(u) = i.
jocularly seriously
browsing
H[1]
H[2]
H[n]
H[3] suburban untravelled considerating
null
37
Universal hashing property. Let H be a universal class of hash
functions; let h ∈ H be chosen uniformly at random from H; and let
u ∈ U. For any subset S ⊆ U of size at most n, the expected number of
items in S that collide with u is at most 1.
Pf. For any element s ∈ S, define indicator random variable X
s
= 1 if
h(s) = h(u) and 0 otherwise. Let X be a random variable counting the
total number of collisions with u.
h " H
s
s " S
s
s " S
s
s " S
1
s " S n
1
n
linearity of expectation X
s
is a 0-1 random variable universal
(assumes u ∉ S)
38
Theorem. [Chebyshev 1850] There exists a prime between n and 2n.
Modulus. Choose a prime number p ≈ n.
Integer encoding. Identify each element u ∈ U with a base-p integer
of r digits: x = (x
1
, x
2
, …, x
r
Hash function. Let A = set of all r-digit, base-p integers. For each
a = (a
1
, a
2
, …, a
r
) where 0 ≤ a
i
< p, define
Hash function family. H = { h
a
: a ∈ A }.
h
a
( x ) = a
i
x
i
i = 1
r
mod p
no need for randomness here
39
Theorem. H = { h
a
: a ∈ A } is a universal class of hash functions.
Pf. Let x = (x
1
, x
2
, …, x
r
) and y = (y
1
, y
2
, …, y
r
) be two distinct elements of
U. We need to show that Pr[h
a
(x) = h
a
(y)] ≤ 1/n.
Since x ≠ y, there exists an integer j such that x
j
≠ y
j
We have h
a
(x) = h
a
(y) iff
Can assume a was chosen uniformly at random by first selecting all
coordinates a
i
where i ≠ j, then selecting a
j
at random. Thus, we can
assume a
i
is fixed for all coordinates i ≠ j.
Since p is prime, a
j
z = m mod p has at most one solution among p
possibilities.
Thus Pr[h
a
(x) = h
a
(y)] = 1/p ≤ 1/n. ▪
a
j
( y
j
" x
j
z
= a
i
( x
i
" y
i
i # j
m
mod p
see lemma on next slide
40
Fact. Let p be prime, and let z ≠ 0 mod p. Then αz = m mod p has at most
one solution 0 ≤ α < p.
Pf.
Suppose α and β are two different solutions.
Then (α - β)z = 0 mod p; hence (α - β)z is divisible by p.
Since z ≠ 0 mod p, we know that z is not divisible by p;
it follows that (α - β) is divisible by p.
This implies α = β. ▪
Bonus fact. Can replace "at most one" with "exactly one" in above fact.
Pf idea. Euclid's algorithm.
42
Theorem. Suppose X
1
n
are independent 0-1 random variables. Let
1
n
. Then for any μ ≥ E[X] and for any δ > 0, we have
Pf. We apply a number of simple transformations.
For any t > 0,
Now
μ
!
!
!
! μ
"
1 +
Pr[ ( 1 ) ]
e
sum of independent 0-1 random variables
is tightly centered on the mean
Pr[ X > ( 1 + ")μ] = Pr e
t X
> e
t ( 1 +")μ
[ ]
$ t ( 1 +")μ
% E [ e
tX
f(x) = e
tX
is monotone in x Markov's inequality: Pr[X > a] ≤ E[X] / a
E [ e
tX
] = E [ e
t X
i i
"
] = E [ e
t X i
i
definition of X independence
43
Pf. (cont)
Let p
i
= Pr[X
i
= 1]. Then,
Combining everything:
Finally, choose t = ln(1 + δ). ▪
Pr[ X > ( 1 + ")μ] # e
$ t ( 1 +")μ
E [ e
t X i
i
% # e
$ t ( 1 +")μ
e
p i
( e
t
$ 1 )
i
% # e
$ t ( 1 +")μ
e
μ( e
t
$ 1 )
( 1 ) 0
!
t
i i
pe t
i i
t
i
t X
Ee pe pe pe e
for any α ≥ 0, 1+α ≤ e
α
previous slide inequality above ∑
i
p
i
= E[X] ≤ μ
44
Theorem. Suppose X
1
n
are independent 0-1 random variables. Let
1
n
. Then for any μ ≤ E[X] and for any 0 < δ < 1, we have
Pf idea. Similar.
Remark. Not quite symmetric since only makes sense to consider δ < 1.
/ 2
2
Pr[ ( 1 ) ]
! μ
! μ
"
X < " < e
51
Sorting. Given a set of n distinct elements S, rearrange them in
ascending order.
Remark. Can implement in-place.
RandomizedQuicksort(S) {
if |S| = 0 return
choose a splitter a
i
∈ S uniformly at random
foreach (a ∈ S) {
if (a < a
i
) put a in S
-
else if (a > a
i
) put a in S
+
}
RandomizedQuicksort(S
-
)
output a
i
RandomizedQuicksort(S
+
)
}
O(log n) extra space
52
Running time.
[Best case.] Select the median element as the splitter: quicksort
makes Θ(n log n) comparisons.
[Worst case.] Select the smallest element as the splitter:
quicksort makes Θ(n
2
) comparisons.
Randomize. Protect against worst case by choosing splitter at random.
Intuition. If we always select an element that is bigger than 25% of
the elements and smaller than 25% of the elements, then quicksort
makes Θ(n log n) comparisons.
Notation. Label elements so that x
1
< x
2
< … < x
n
53
x
7
x
6
x
12
x
3
x
8
x
7
x
1
x
15
x
10
x
16
x
14
x
9
x
17
x
11
x
13
x
5
x
4
x
10
x
13
x
5
x
16
x
11
x
3
x
9
x
2
x
4
x
7
x
12
x
15
x
17
x
1
x
6
x
8
x
14
first splitter, chosen uniformly at random
BST representation. Draw recursive BST of splitters.
S
S
54
Observation. Element only compared with its ancestors and descendants.
x
2
and x
7
are compared if their lca = x
2
or x
7
x
2
and x
7
are not compared if their lca = x
3
or x
4
or x
5
or x
6
Claim. Pr[x
i
and x
j
are compared] = 2 / |j - i + 1|.
x
10
x
13
x
5
x
16
x
11
x
3
x
9
x
2
x
4
x
7
x
12
x
15
x
17
x
1
x
6
x
8
x
14
55
Theorem. Expected # of comparisons is O(n log n).
Pf.
Theorem. [Knuth 1973] Stddev of number of comparisons is ~ 0.65N.
Ex. If n = 1 million, the probability that randomized quicksort takes
less than 4n ln n comparisons is at least 99.94%.
Chebyshev's inequality. Pr[|X - μ| ≥ kδ] ≤ 1 / k
2
.
j " i + 1
j j = 2
i
i = 1
n
1 $ i < j $ n
j j = 1
n
x
dx
x = 1
n
& = 2 n ln n
probability that i and j are compared