Our Goal: Randomized Algorithms - Two Types (Two Examples)
Our Goal: Randomized Algorithms - Two Types (Two Examples)
Our goal
Randomized Algorithms – Two types (Two examples)
I Monte Carlo
I Las Vegas
Input x Output y
Deterministic Algorithm
Input x Output y
Deterministic Algorithm
random bits r
Input x Output yr
Randomized Algorithm
Input x Output y
Deterministic Algorithm
random bits r
Input x Output yr
Randomized Algorithm
Input x Output y
Deterministic Algorithm
random bits r
Input x Output yr
Randomized Algorithm
Input x Output y
Deterministic Algorithm
random bits r
Input x Output yr
Randomized Algorithm
Deterministic algorithm:
1 Multiply A and B and check if equal to C .
2 Running time?
Deterministic algorithm:
1 Multiply A and B and check if equal to C .
2 Running time? O(n3 ) by straight forward approach. O(n2.37 )
with fast matrix multiplication (complicated and impractical).
Randomized algorithm:
1 Pick a random n ⇥ 1 vector r 2 {0, 1}n .
2 Return the answer of the equality ABr = Cr .
3 Running time?
Randomized algorithm:
1 Pick a random n ⇥ 1 vector r 2 {0, 1}n .
2 Return the answer of the equality ABr = Cr .
3 Running time? O(n2 )!
Randomized algorithm:
1 Pick a random n ⇥ 1 vector r 2 {0, 1}n .
2 Return the answer of the equality ABr = Cr .
3 Running time? O(n2 )!
Theorem
If AB = C then the algorithm will always say YES. If AB 6= C then
the algorithm will say YES with probability at most 1/2. Can repeat
the algorithm 100 times independently to reduce the probability of a
false positive to 1/2100 .
Fahad (IITH) CS5390 6 6 / 58
Example 2: Randomized QuickSort
QuickSort ?
1 Pick a pivot element from array
2 Split array into 3 subarrays: those smaller than pivot, those
larger than pivot, and the pivot itself.
3 Recursively sort the subarrays, and concatenate them.
Randomized QuickSort
1 Pick a pivot element uniformly at random from the array
2 Split array into 3 subarrays: those smaller than pivot, those
larger than pivot, and the pivot itself.
3 Recursively sort the subarrays, and concatenate them.
Theorem
Randomized QuickSort sorts a given array of length n in
O(n log n) expected time.
Theorem
Randomized QuickSort sorts a given array of length n in
O(n log n) expected time.
Randomized algorithms:
1 Algorithm uses random bits in addition to input.
2 Analyze algorithms average performance over the given input
where the average is over the random bits that the algorithm
uses.
3 On each input behaviour of algorithm is random. Analyze
worst-case over all inputs of the (average) performance.
Examples
An unbiased coin.
Examples
An unbiased coin. ⌦ = {H, T } and Pr[H] = Pr[T ] = 1/2.
Examples
An unbiased coin. ⌦ = {H, T } and Pr[H] = Pr[T ] = 1/2.
A 6-sided unbiased die.
Examples
An unbiased coin. ⌦ = {H, T } and Pr[H] = Pr[T ] = 1/2.
A 6-sided unbiased die. ⌦ = {1, 2, 3, 4, 5, 6} and
Pr[i ] = 1/6 for all i 2 ⌦.
Examples
An unbiased coin. ⌦ = {H, T } and Pr[H] = Pr[T ] = 1/2.
A 6-sided unbiased die. ⌦ = {1, 2, 3, 4, 5, 6} and
Pr[i ] = 1/6 for all i 2 ⌦.
A pair of independent dice.
Examples
An unbiased coin. ⌦ = {H, T } and Pr[H] = Pr[T ] = 1/2.
A 6-sided unbiased die. ⌦ = {1, 2, 3, 4, 5, 6} and
Pr[i ] = 1/6 for all i 2 ⌦.
A pair of independent dice.
⌦ = {(i , j ) | 1 i 6, 1 j 6} and Pr[(i , j )] = 1/6
for all (i , j ) 2 ⌦.
Fahad (IITH) CS5390 12 12 / 58
Events
Definition
Given a probability space (⌦, Pr) an event is a subset of ⌦. In other
words an event is a collection of elementary
P events. The probability
of an event A, denoted by Pr[A], is !2A Pr[!].
Example
A pair of independent dice. ⌦ = {(i , j ) | 1 i 6, 1 j 6}.
Let A be the event that the sum of the two numbers on the dice is
even. n o
Then A = (i , j ) 2 ⌦ (i + j ) is even .
Pr[A] = |A|/36 = 1/2.
Fahad (IITH) CS5390 13 13 / 58
Independent Events
Definition
Given a probability space (⌦, Pr) and two events A, B are
independent if and only if Pr[A \ B] = Pr[A] Pr[B]. Otherwise
they are dependent. In other words A, B independent implies one
does not a↵ect the other.
Example
Two coins. ⌦ = {HH, TT , HT , TH} and
Pr[HH] = Pr[TT ] = Pr[HT ] = Pr[TH] = 1/4.
1 A is the event that the first coin is heads and B is the event
that second coin is tails.
Example
Two coins. ⌦ = {HH, TT , HT , TH} and
Pr[HH] = Pr[TT ] = Pr[HT ] = Pr[TH] = 1/4.
1 A is the event that the first coin is heads and B is the event
that second coin is tails. A, B are independent.
Example
Two coins. ⌦ = {HH, TT , HT , TH} and
Pr[HH] = Pr[TT ] = Pr[HT ] = Pr[TH] = 1/4.
1 A is the event that the first coin is heads and B is the event
that second coin is tails. A, B are independent.
2 A is the event that both are not tails and B is event that second
coin is heads.
Example
Two coins. ⌦ = {HH, TT , HT , TH} and
Pr[HH] = Pr[TT ] = Pr[HT ] = Pr[TH] = 1/4.
1 A is the event that the first coin is heads and B is the event
that second coin is tails. A, B are independent.
2 A is the event that both are not tails and B is event that second
coin is heads. A, B are dependent.
Lemma
Forh any two
i eventsh Ei and F,h we
i have that
Pr E [ F Pr E + Pr F .
Lemma
Forh any two
i eventsh Ei and F,h we
i have that
Pr E [ F Pr E + Pr F .
Proof.
h i X
Pr E [ F = Pr[x]
x2E[F
X X h i h i
Pr[x] + Pr[x] = Pr E + Pr F .
x2E x2F
Randomized algorithm:
1 Pick a random n ⇥ 1 vector r 2 {0, 1}n .
2 Return the answer of the equality ABr = Cr .
3 Running time?
Randomized algorithm:
1 Pick a random n ⇥ 1 vector r 2 {0, 1}n .
2 Return the answer of the equality ABr = Cr .
3 Running time? O(n2 )!
Theorem
If AB = C then the algorithm will always say YES. If AB 6= C then
the algorithm will say YES with probability at most 1/2. Can repeat
the algorithm 100 times independently to reduce the probability of a
false positive to 1/2100 .
Fahad (IITH) CS5390 17 17 / 58
Principle of deferred decisions
If AB = C then the algorithm will always say YES.
1
Success probability is at least 1 2100
> 0.99