Advanced Algorithms (II) Shanghai Jiao Tong University Chihao Zhang - - PowerPoint PPT Presentation

advanced algorithms ii
SMART_READER_LITE
LIVE PREVIEW

Advanced Algorithms (II) Shanghai Jiao Tong University Chihao Zhang - - PowerPoint PPT Presentation

Advanced Algorithms (II) Shanghai Jiao Tong University Chihao Zhang March 9th, 2020 Random Variables Random Variables Recall that a probability space is a tuple ( , , Pr) Random Variables Recall that a probability space is a tuple (


slide-1
SLIDE 1

Advanced Algorithms (II)

Shanghai Jiao Tong University

Chihao Zhang

March 9th, 2020

slide-2
SLIDE 2

Random Variables

slide-3
SLIDE 3

Random Variables

Recall that a probability space is a tuple (Ω, ℱ, Pr)

slide-4
SLIDE 4

Random Variables

Recall that a probability space is a tuple (Ω, ℱ, Pr) In this course, we mainly focus on countable Ω

slide-5
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
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
SLIDE 7

Linearity of Expectations

slide-8
SLIDE 8

Linearity of Expectations

For any random variables

n X1, …, Xn

E [

n

i=1

Xi] =

n

i=1

E[Xi]

slide-9
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
SLIDE 10

Coupon Collector

slide-11
SLIDE 11

Coupon Collector

There are coupons to collect…

n

slide-12
SLIDE 12

Coupon Collector

There are coupons to collect…

n

Each time one coupon is drawn independently uniformly at random

slide-13
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 14
slide-15
SLIDE 15

Let be the number of draws between -th distinct coupon to the

  • th distinct coupon

Xi i i + 1

slide-16
SLIDE 16

Let be the number of draws between -th distinct coupon to the

  • th distinct coupon

Xi i i + 1

Number of draws =

X :=

n−1

i=0

Xi

slide-17
SLIDE 17

Let be the number of draws between -th distinct coupon to the

  • th distinct coupon

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
SLIDE 18

Geometric Distribution

slide-19
SLIDE 19

Geometric Distribution

Let be a random variable following geometric distribution with probability .

X p

slide-20
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
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 22
slide-23
SLIDE 23

Back to Coupon Collector…

slide-24
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
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
SLIDE 26

Linearity may fail when…

slide-27
SLIDE 27

Linearity may fail when…

  • n = ∞
slide-28
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 29
slide-30
SLIDE 30

What is the expected money he wins?

slide-31
SLIDE 31

What is the expected money he wins?

  • In stage , he wins

with ,

  • so

i Xi E[Xi] = 0

i=1

E[Xi] = 0

slide-32
SLIDE 32

What is the expected money he wins?

  • In stage , he wins

with ,

  • so

i Xi E[Xi] = 0

i=1

E[Xi] = 0

  • On the other hand, he eventually wins

,

  • so

!

$1 E [

i=1

Xi] = 1 ≠

i=1

E[Xi]

slide-33
SLIDE 33

Linearity may fail when…

slide-34
SLIDE 34

Linearity may fail when…

  • is random

n = N

slide-35
SLIDE 35

Linearity may fail when…

  • is random

n = N

Suppose we draw a number and toss dices , what is ?

N N X1, …, XN E [

N

i=1

XN]

slide-36
SLIDE 36
slide-37
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
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
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
SLIDE 40

Wald’s Equation

slide-41
SLIDE 41

Wald’s Equation

If the variables satisfy

slide-42
SLIDE 42

Wald’s Equation

If the variables satisfy

  • and all

are independent and finite;

  • All

are identically distributed

N Xi Xi

slide-43
SLIDE 43

Wald’s Equation

If the variables satisfy

  • and all

are independent and finite;

  • All

are identically distributed

N Xi Xi

N

i=1

E [Xi] = E[N] ⋅ E[X1]

slide-44
SLIDE 44

Wald’s Equation

If the variables satisfy

  • and all

are independent and finite;

  • All

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
SLIDE 45

Application: Quick Select

slide-46
SLIDE 46

Application: Quick Select

Find the -th largest number in an unsorted array

k A

slide-47
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 48
slide-49
SLIDE 49

The partition step takes time

O(|A|)

slide-50
SLIDE 50

The partition step takes time

O(|A|)

What is the total time cost in expectation?

slide-51
SLIDE 51

The partition step takes time

O(|A|)

What is the total time cost in expectation?

  • size of at -th round

and The time cost is

Xi A i X1 = n E[Xi+1 ∣ Xi] ≤ 3 4 Xi

i=1

Xi

slide-52
SLIDE 52
slide-53
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
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
SLIDE 55

KUW inequality

slide-56
SLIDE 56

KUW inequality

While analyzing random algorithms, a common recursion is for random

T(n) = 1 + T(n − Xn) Xn

slide-57
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
SLIDE 58

Application: Expectation of Geometric Variables

slide-59
SLIDE 59

Application: Expectation of Geometric Variables

T(1) = 1 + T(1 − X1), where E[X1] = p

slide-60
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
SLIDE 61

Application: Rounds of Quick Select

slide-62
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
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
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
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
SLIDE 66

Application: Coupon Collector

slide-67
SLIDE 67

Application: Coupon Collector

where

T(m) = 1 + T(n − Xm) Xm ∼ Ber(m/n)

slide-68
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
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
SLIDE 70

Proof of KUW inequality