SLIDE 1 Advanced Algorithms (II)
Shanghai Jiao Tong University
Chihao Zhang
March 9th, 2020
SLIDE 2
Random Variables
SLIDE 3
Random Variables
Recall that a probability space is a tuple (Ω, ℱ, Pr)
SLIDE 4
Random Variables
Recall that a probability space is a tuple (Ω, ℱ, Pr) In this course, we mainly focus on countable Ω
SLIDE 5
Random Variables
Recall that a probability space is a tuple (Ω, ℱ, Pr) A random variable is a function
X X : Ω → ℝ
In this course, we mainly focus on countable Ω
SLIDE 6 Random Variables
Recall that a probability space is a tuple (Ω, ℱ, Pr) A random variable is a function
X X : Ω → ℝ
In this course, we mainly focus on countable Ω The expectation E[X] =
∑
a∈Ω:Pr[X=a]>0
a ⋅ Pr[X = a]
SLIDE 7
Linearity of Expectations
SLIDE 8 Linearity of Expectations
For any random variables
n X1, …, Xn
E [
n
∑
i=1
Xi] =
n
∑
i=1
E[Xi]
SLIDE 9 Linearity of Expectations
For any random variables
n X1, …, Xn
E [
n
∑
i=1
Xi] =
n
∑
i=1
E[Xi]
E[X1 + X2] = ∑
a,b
(a + b) ⋅ Pr[X1 = a, X2 = b] = ∑
a,b
a ⋅ Pr[X1 = a, X2 = b] + ∑
a,b
b ⋅ Pr[X1 = a, X2 = b] = ∑
a
a ⋅ Pr[X1 = a] + ∑
b
b ⋅ Pr[X2 = b] = E[X1] + E[X2]
SLIDE 10
Coupon Collector
SLIDE 11
Coupon Collector
There are coupons to collect…
n
SLIDE 12
Coupon Collector
There are coupons to collect…
n
Each time one coupon is drawn independently uniformly at random
SLIDE 13
Coupon Collector
There are coupons to collect…
n
Each time one coupon is drawn independently uniformly at random How many times one needs to draw to collect all coupons?
SLIDE 14
SLIDE 15 Let be the number of draws between -th distinct coupon to the
Xi i i + 1
SLIDE 16 Let be the number of draws between -th distinct coupon to the
Xi i i + 1
Number of draws =
X :=
n−1
∑
i=0
Xi
SLIDE 17 Let be the number of draws between -th distinct coupon to the
Xi i i + 1
Number of draws =
X :=
n−1
∑
i=0
Xi
For any , follows geometric distribution with probability
i Xin − i n
SLIDE 18
Geometric Distribution
SLIDE 19
Geometric Distribution
Let be a random variable following geometric distribution with probability .
X p
SLIDE 20
Geometric Distribution
Let be a random variable following geometric distribution with probability .
X p
Namely, we toss a coin who comes to HEAD with probability , is the number of tosses to see the first HEAD.
p X
SLIDE 21
Geometric Distribution
Let be a random variable following geometric distribution with probability .
X p
Namely, we toss a coin who comes to HEAD with probability , is the number of tosses to see the first HEAD.
p X
It is not hard to see that E[X] = 1
p
SLIDE 22
SLIDE 23
Back to Coupon Collector…
SLIDE 24 Back to Coupon Collector… E[X] = E [
n−1
∑
i=0
Xi] =
n−1
∑
i=0
E[Xi] =
n−1
∑
i=0
n n − i = n n + n n − 1 + n n − 2 + … + n 1 = n ⋅ H(n) → n log n + γn
SLIDE 25 Back to Coupon Collector… E[X] = E [
n−1
∑
i=0
Xi] =
n−1
∑
i=0
E[Xi] =
n−1
∑
i=0
n n − i = n n + n n − 1 + n n − 2 + … + n 1 = n ⋅ H(n) → n log n + γn The constant is called Euler constant
γ = 0.577...
SLIDE 26
Linearity may fail when…
SLIDE 27 Linearity may fail when…
SLIDE 28 Linearity may fail when…
- n = ∞
- St. Petersburg paradox
Each stage of the game a fair coin is tossed and a gambler guesses the result. He wins the amount he bet if his guess is correct and lose the money if he is wrong. He bets at the first
- stage. If he loses, he doubles the money and bets
- again. The game ends when the gambler wins.
$1
SLIDE 29
SLIDE 30
What is the expected money he wins?
SLIDE 31 What is the expected money he wins?
with ,
i Xi E[Xi] = 0
∞
∑
i=1
E[Xi] = 0
SLIDE 32 What is the expected money he wins?
with ,
i Xi E[Xi] = 0
∞
∑
i=1
E[Xi] = 0
- On the other hand, he eventually wins
,
!
$1 E [
∞
∑
i=1
Xi] = 1 ≠
∞
∑
i=1
E[Xi]
SLIDE 33
Linearity may fail when…
SLIDE 34 Linearity may fail when…
n = N
SLIDE 35 Linearity may fail when…
n = N
Suppose we draw a number and toss dices , what is ?
N N X1, …, XN E [
N
∑
i=1
XN]
SLIDE 36
SLIDE 37 Each is uniform in , one might expect
Xi {1,…,6} E [
N
∑
i=1
Xi] = E[N] ⋅ E[X1] = 3.5 × 3.5 = 12.25
SLIDE 38 If itself is drawn by tossing a dice and let
N X1 = X2 = … = XN = N
Each is uniform in , one might expect
Xi {1,…,6} E [
N
∑
i=1
Xi] = E[N] ⋅ E[X1] = 3.5 × 3.5 = 12.25
SLIDE 39 Then E [
N
∑
i=1
Xi] = E[N ⋅ N] = 15.166..
If itself is drawn by tossing a dice and let
N X1 = X2 = … = XN = N
Each is uniform in , one might expect
Xi {1,…,6} E [
N
∑
i=1
Xi] = E[N] ⋅ E[X1] = 3.5 × 3.5 = 12.25
SLIDE 40
Wald’s Equation
SLIDE 41
Wald’s Equation
If the variables satisfy
SLIDE 42 Wald’s Equation
If the variables satisfy
are independent and finite;
are identically distributed
N Xi Xi
SLIDE 43 Wald’s Equation
If the variables satisfy
are independent and finite;
are identically distributed
N Xi Xi
N
∑
i=1
E [Xi] = E[N] ⋅ E[X1]
SLIDE 44 Wald’s Equation
If the variables satisfy
are independent and finite;
are identically distributed
N Xi Xi
N
∑
i=1
E [Xi] = E[N] ⋅ E[X1] More generally if is a stopping time
N
SLIDE 45
Application: Quick Select
SLIDE 46
Application: Quick Select
Find the -th largest number in an unsorted array
k A
SLIDE 47 Application: Quick Select
Find the -th largest number in an unsorted array
k A
Find( ) Randomly choose a pivot 1. Partition into such that , 2. If , return 3. If , return Find( ) 4. return Find( )
A, k x ∈ A A − {x} A1, A2 ∀y ∈ A1, y < x ∀z ∈ A2, z > x |A1| = k − 1 x |A1| ≥ k A1, k A2, k − |A1| − 1
SLIDE 48
SLIDE 49
The partition step takes time
O(|A|)
SLIDE 50
The partition step takes time
O(|A|)
What is the total time cost in expectation?
SLIDE 51 The partition step takes time
O(|A|)
What is the total time cost in expectation?
and The time cost is
Xi A i X1 = n E[Xi+1 ∣ Xi] ≤ 3 4 Xi
∞
∑
i=1
Xi
SLIDE 52
SLIDE 53 E[Xi+1 ∣ Xi] ≤ 3 4 Xi ⟹ E[Xi+1] = E[E[Xi+1 ∣ Xi]] ≤ 3 4 E[Xi] ≤ ( 3 4 )
i
n
SLIDE 54 E[Xi+1 ∣ Xi] ≤ 3 4 Xi ⟹ E[Xi+1] = E[E[Xi+1 ∣ Xi]] ≤ 3 4 E[Xi] ≤ ( 3 4 )
i
n E [
∞
∑
i=1
Xi] = E [
n
∑
i=1
Xi] =
n
∑
i=1
E[Xi] ≤
n
∑
i=1 (
3 4 )
i−1
n = 4n .
SLIDE 55
KUW inequality
SLIDE 56
KUW inequality
While analyzing random algorithms, a common recursion is for random
T(n) = 1 + T(n − Xn) Xn
SLIDE 57 KUW inequality
While analyzing random algorithms, a common recursion is for random
T(n) = 1 + T(n − Xn) Xn
- Theorem. (Karp-Upfal-Wigderson Inequality)
Assume for every , is an integer for some such that . If for all , where is positive and increasing , then
n 0 ≤ Xn ≤ n − a a T(a) = 0 E[Xn] ≥ μ(n) n > a μ(n) E[T(n)] ≤ ∫
n a
1 μ(t) dt
SLIDE 58
Application: Expectation of Geometric Variables
SLIDE 59
Application: Expectation of Geometric Variables
T(1) = 1 + T(1 − X1), where E[X1] = p
SLIDE 60 Application: Expectation of Geometric Variables
T(1) = 1 + T(1 − X1), where E[X1] = p Choosing gives
μ(n) = p E[T(1)] ≤ ∫
1
1 p dt = 1 p .
SLIDE 61
Application: Rounds of Quick Select
SLIDE 62
Application: Rounds of Quick Select
In our Find( ) algorithm, we have
A, k
T(n) = 1 + max{T(m), T(n − m − 1)},
SLIDE 63
Application: Rounds of Quick Select
In our Find( ) algorithm, we have
A, k
T(n) = 1 + max{T(m), T(n − m − 1)}, where is in uniformly at random.
m {1,2,…, n − 1}
SLIDE 64
Application: Rounds of Quick Select
In our Find( ) algorithm, we have
A, k
T(n) = 1 + max{T(m), T(n − m − 1)}, where is in uniformly at random.
m {1,2,…, n − 1}
We can choose (Why?)
μ(n) = n 4
SLIDE 65 Application: Rounds of Quick Select
In our Find( ) algorithm, we have
A, k
T(n) = 1 + max{T(m), T(n − m − 1)}, where is in uniformly at random.
m {1,2,…, n − 1}
We can choose (Why?)
μ(n) = n 4
KUW implies E[T(n)] ≤ ∫
n 1
4 t dt = 4 log n
SLIDE 66
Application: Coupon Collector
SLIDE 67
Application: Coupon Collector
where
T(m) = 1 + T(n − Xm) Xm ∼ Ber(m/n)
SLIDE 68
Application: Coupon Collector
where
T(m) = 1 + T(n − Xm) Xm ∼ Ber(m/n)
So we can choose μ(m) = ⌈m⌉
n
SLIDE 69 Application: Coupon Collector
where
T(m) = 1 + T(n − Xm) Xm ∼ Ber(m/n)
So we can choose μ(m) = ⌈m⌉
n
KUW implies E[T(n)] ≤ ∫
n
n ⌈t⌉ dt = n ⋅ Hn
SLIDE 70
Proof of KUW inequality