CS 473: Algorithms Chandra Chekuri Ruta Mehta University of - - PowerPoint PPT Presentation

cs 473 algorithms
SMART_READER_LITE
LIVE PREVIEW

CS 473: Algorithms Chandra Chekuri Ruta Mehta University of - - PowerPoint PPT Presentation

CS 473: Algorithms Chandra Chekuri Ruta Mehta University of Illinois, Urbana-Champaign Fall 2016 Chandra & Ruta (UIUC) CS473 1 Fall 2016 1 / 37 CS 473: Algorithms, Fall 2016 Inequalities & QuickSort w.h.p. Lecture 8 September


slide-1
SLIDE 1

CS 473: Algorithms

Chandra Chekuri Ruta Mehta

University of Illinois, Urbana-Champaign

Fall 2016

Chandra & Ruta (UIUC) CS473 1 Fall 2016 1 / 37

slide-2
SLIDE 2

CS 473: Algorithms, Fall 2016

Inequalities & QuickSort w.h.p.

Lecture 8

September 16, 2016

Chandra & Ruta (UIUC) CS473 2 Fall 2016 2 / 37

slide-3
SLIDE 3

Outline

Slick Analysis of Randomized QuickSort

Concentration of Mass Around Mean

Markov’s Inequality Chebyshev’s Inequality Chernoff Bound Randomized QuickSort: High Probability Analysis

Chandra & Ruta (UIUC) CS473 3 Fall 2016 3 / 37

slide-4
SLIDE 4

Part I Slick analysis of QuickSort

Chandra & Ruta (UIUC) CS473 4 Fall 2016 4 / 37

slide-5
SLIDE 5

Recall: Randomized QuickSort

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.

Chandra & Ruta (UIUC) CS473 5 Fall 2016 5 / 37

slide-6
SLIDE 6

A Slick Analysis of QuickSort

Let Q(A) be number of comparisons done on input array A:

1

For 1 ≤ i < j < n let Rij be the event that rank i element is compared with rank j element.

2

Xij is the indicator random variable for Rij. That is, Xij = 1 if rank i is compared with rank j element, otherwise 0.

Chandra & Ruta (UIUC) CS473 6 Fall 2016 6 / 37

slide-7
SLIDE 7

A Slick Analysis of QuickSort

Let Q(A) be number of comparisons done on input array A:

1

For 1 ≤ i < j < n let Rij be the event that rank i element is compared with rank j element.

2

Xij is the indicator random variable for Rij. That is, Xij = 1 if rank i is compared with rank j element, otherwise 0. Q(A) =

  • 1≤i<j≤n

Xij and hence by linearity of expectation, E

  • Q(A)
  • =
  • 1≤i<j≤n

E

  • Xij
  • =
  • 1≤i<j≤n

Pr

  • Rij
  • .

Chandra & Ruta (UIUC) CS473 6 Fall 2016 6 / 37

slide-8
SLIDE 8

A Slick Analysis of QuickSort

Rij = rank i element is compared with rank j element. Question: What is Pr[Rij]?

Chandra & Ruta (UIUC) CS473 7 Fall 2016 7 / 37

slide-9
SLIDE 9

A Slick Analysis of QuickSort

Rij = rank i element is compared with rank j element. Question: What is Pr[Rij]? With ranks:

7 5 9 1 3 4 8 6

1 2 3 4 5 6 7 8

Chandra & Ruta (UIUC) CS473 7 Fall 2016 7 / 37

slide-10
SLIDE 10

A Slick Analysis of QuickSort

Rij = rank i element is compared with rank j element. Question: What is Pr[Rij]? With ranks:

7 5 9 1 3 4 8 6

1 2 3 4 5 6 7 8 As such, probability of comparing 5 to 8 is Pr[R4,7].

Chandra & Ruta (UIUC) CS473 7 Fall 2016 7 / 37

slide-11
SLIDE 11

A Slick Analysis of QuickSort

Rij = rank i element is compared with rank j element. Question: What is Pr[Rij]? With ranks:

7 5 9 1 3 4 8 6

1 2 3 4 5 6 7 8

1

If pivot too small (say 3 [rank 2]). Partition and call recursively:

7 5 9 1 3 4 8 6

= ⇒

7 5 9 3 4 8 6 1

Decision if to compare 5 to 8 is moved to subproblem.

Chandra & Ruta (UIUC) CS473 7 Fall 2016 7 / 37

slide-12
SLIDE 12

A Slick Analysis of QuickSort

Rij = rank i element is compared with rank j element. Question: What is Pr[Rij]? With ranks:

7 5 9 1 3 4 8 6

1 2 3 4 5 6 7 8

1

If pivot too small (say 3 [rank 2]). Partition and call recursively:

7 5 9 1 3 4 8 6

= ⇒

7 5 9 3 4 8 6 1

Decision if to compare 5 to 8 is moved to subproblem.

2

If pivot too large (say 9 [rank 8]):

7 5 9 1 3 4 8 6 7 5 9 1 3 4 8 6

= ⇒

7 5 1 3 4 8 6 9

Decision if to compare 5 to 8 moved to subproblem.

Chandra & Ruta (UIUC) CS473 7 Fall 2016 7 / 37

slide-13
SLIDE 13

A Slick Analysis of QuickSort

Question: What is Pr[Ri,j]?

7 5 9 1 3 4 8 6

1 2 3 4 5 6 7 8 As such, probability of com- paring 5 to 8 is Pr[R4,7].

1

If pivot is 5 (rank 4). Bingo!

7 5 9 1 3 4 8 6

= ⇒

1 3 4 5 7 9 8 6

Chandra & Ruta (UIUC) CS473 8 Fall 2016 8 / 37

slide-14
SLIDE 14

A Slick Analysis of QuickSort

Question: What is Pr[Ri,j]?

7 5 9 1 3 4 8 6

1 2 3 4 5 6 7 8 As such, probability of com- paring 5 to 8 is Pr[R4,7].

1

If pivot is 5 (rank 4). Bingo!

7 5 9 1 3 4 8 6

= ⇒

1 3 4 5 7 9 8 6

2

If pivot is 8 (rank 7). Bingo!

7 5 9 1 3 4 8 6

= ⇒

7 5 9 1 3 4 6 8

Chandra & Ruta (UIUC) CS473 8 Fall 2016 8 / 37

slide-15
SLIDE 15

A Slick Analysis of QuickSort

Question: What is Pr[Ri,j]?

7 5 9 1 3 4 8 6

1 2 3 4 5 6 7 8 As such, probability of com- paring 5 to 8 is Pr[R4,7].

1

If pivot is 5 (rank 4). Bingo!

7 5 9 1 3 4 8 6

= ⇒

1 3 4 5 7 9 8 6

2

If pivot is 8 (rank 7). Bingo!

7 5 9 1 3 4 8 6

= ⇒

7 5 9 1 3 4 6 8

3

If pivot in between the two numbers (say 6 [rank 5]):

7 5 9 1 3 4 8 6

= ⇒

7 5 9 1 3 4 6 8

5 and 8 will never be compared to each other.

Chandra & Ruta (UIUC) CS473 8 Fall 2016 8 / 37

slide-16
SLIDE 16

A Slick Analysis of QuickSort

Question: What is Pr[Ri,j]?

Conclusion:

Ri,j happens if and only if: ith or jth ranked element is the first pivot out of ith to jth ranked elements.

Chandra & Ruta (UIUC) CS473 9 Fall 2016 9 / 37

slide-17
SLIDE 17

A Slick Analysis of QuickSort

Question: What is Pr[Rij]?

Chandra & Ruta (UIUC) CS473 10 Fall 2016 10 / 37

slide-18
SLIDE 18

A Slick Analysis of QuickSort

Question: What is Pr[Rij]?

Lemma

Pr

  • Rij
  • =

2 j−i+1.

Chandra & Ruta (UIUC) CS473 10 Fall 2016 10 / 37

slide-19
SLIDE 19

A Slick Analysis of QuickSort

Question: What is Pr[Rij]?

Lemma

Pr

  • Rij
  • =

2 j−i+1.

Proof.

Let a1, . . . , ai, . . . , aj, . . . , an be elements of A in sorted order. Let S = {ai, ai+1, . . . , aj} Observation: If pivot is chosen outside S then all of S either in left array or right array. Observation: ai and aj separated when a pivot is chosen from S for the first time. Once separated no comparison. Observation: ai is compared with aj if and only if the first chosen pivot from S is either ai or aj.

Chandra & Ruta (UIUC) CS473 10 Fall 2016 10 / 37

slide-20
SLIDE 20

A Slick Analysis of QuickSort

Continued...

Lemma

Pr

  • Rij
  • =

2 j−i+1.

Proof.

Let a1, . . . , ai, . . . , aj, . . . , an be sort of A. Let S = {ai, ai+1, . . . , aj} Observation: ai is compared with aj if and only if the first chosen pivot from S is either ai or aj. Observation: Given that pivot is chosen from S the probability that it is ai or aj is exactly 2/|S| = 2/(j − i + 1) since the pivot is chosen uniformly at random from the array.

Chandra & Ruta (UIUC) CS473 11 Fall 2016 11 / 37

slide-21
SLIDE 21

A Slick Analysis of QuickSort

Continued...

Lemma

Pr

  • Rij
  • =

2 j−i+1.

Proof.

Let a1, . . . , ai, . . . , aj, . . . , an be sort of A. Let S = {ai, ai+1, . . . , aj}. Event E when first pivot from S is chosen. Observation: Given E probability that the pivot is ai or aj is exactly 2/|S| = 2/(j − i + 1),

Chandra & Ruta (UIUC) CS473 12 Fall 2016 12 / 37

slide-22
SLIDE 22

A Slick Analysis of QuickSort

Continued...

Lemma

Pr

  • Rij
  • =

2 j−i+1.

Proof.

Let a1, . . . , ai, . . . , aj, . . . , an be sort of A. Let S = {ai, ai+1, . . . , aj}. Event E when first pivot from S is chosen. Observation: Given E probability that the pivot is ai or aj is exactly 2/|S| = 2/(j − i + 1), i.e. Pr[Rij|E] = 2/(j − i + 1).

Chandra & Ruta (UIUC) CS473 12 Fall 2016 12 / 37

slide-23
SLIDE 23

A Slick Analysis of QuickSort

Continued...

Lemma

Pr

  • Rij
  • =

2 j−i+1.

Proof.

Let a1, . . . , ai, . . . , aj, . . . , an be sort of A. Let S = {ai, ai+1, . . . , aj}. Event E when first pivot from S is chosen. Observation: Given E probability that the pivot is ai or aj is exactly 2/|S| = 2/(j − i + 1), i.e. Pr[Rij|E] = 2/(j − i + 1). Since Pr[E] = 1, we get Pr[Rij] = 2/(j − i + 1).

Chandra & Ruta (UIUC) CS473 12 Fall 2016 12 / 37

slide-24
SLIDE 24

A Slick Analysis of QuickSort

Continued...

So far we know

Pr[Rij] =

2 j−i+1.

Chandra & Ruta (UIUC) CS473 13 Fall 2016 13 / 37

slide-25
SLIDE 25

A Slick Analysis of QuickSort

Continued...

So far we know

Pr[Rij] =

2 j−i+1.

E

  • Q(A)
  • =
  • 1≤i<j≤n

Pr

  • Rij
  • =
  • 1≤i<j≤n

2 j − i + 1

Chandra & Ruta (UIUC) CS473 13 Fall 2016 13 / 37

slide-26
SLIDE 26

A Slick Analysis of QuickSort

Continued...

So far we know

Pr[Rij] =

2 j−i+1.

E

  • Q(A)
  • =
  • 1≤i<j≤n

2 j − i + 1

Chandra & Ruta (UIUC) CS473 13 Fall 2016 13 / 37

slide-27
SLIDE 27

A Slick Analysis of QuickSort

Continued...

So far we know

Pr[Rij] =

2 j−i+1.

E

  • Q(A)
  • = 2

n−1

  • i=1

n

  • i<j

1 j − i + 1

Chandra & Ruta (UIUC) CS473 13 Fall 2016 13 / 37

slide-28
SLIDE 28

A Slick Analysis of QuickSort

Continued...

So far we know

Pr[Rij] =

2 j−i+1.

E

  • Q(A)
  • = 2

n−1

  • i=1

n

  • i<j

1 j − i + 1 ≤ 2

n−1

  • i=1

n−i+1

  • ∆=2

1 ∆

Chandra & Ruta (UIUC) CS473 13 Fall 2016 13 / 37

slide-29
SLIDE 29

A Slick Analysis of QuickSort

Continued...

So far we know

Pr[Rij] =

2 j−i+1.

E

  • Q(A)
  • = 2

n−1

  • i=1

n

  • i<j

1 j − i + 1 ≤ 2

n−1

  • i=1

n−i+1

  • ∆=2

1 ∆ ≤ 2

n−1

  • i=1

(Hn−i+1 − 1) ≤ 2

  • 1≤i<n

Hn

Chandra & Ruta (UIUC) CS473 13 Fall 2016 13 / 37

slide-30
SLIDE 30

A Slick Analysis of QuickSort

Continued...

So far we know

Pr[Rij] =

2 j−i+1.

E

  • Q(A)
  • = 2

n−1

  • i=1

n

  • i<j

1 j − i + 1 ≤ 2

n−1

  • i=1

n−i+1

  • ∆=2

1 ∆ ≤ 2

n−1

  • i=1

(Hn−i+1 − 1) ≤ 2

  • 1≤i<n

Hn ≤ 2nHn = O(n log n)

Chandra & Ruta (UIUC) CS473 13 Fall 2016 13 / 37

slide-31
SLIDE 31

Part II Inequalities

Chandra & Ruta (UIUC) CS473 14 Fall 2016 14 / 37

slide-32
SLIDE 32

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-33
SLIDE 33

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-34
SLIDE 34

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-35
SLIDE 35

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-36
SLIDE 36

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-37
SLIDE 37

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-38
SLIDE 38

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-39
SLIDE 39

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-40
SLIDE 40

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-41
SLIDE 41

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-42
SLIDE 42

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-43
SLIDE 43

Massive randomness.. Is not that random.

Consider flipping a fair coin n times independently, head gives 1, tail gives zero. How many 1s? Binomial distribution: k w.p. n

k

  • 1/2n.

Chandra & Ruta (UIUC) CS473 15 Fall 2016 15 / 37

slide-44
SLIDE 44

Massive randomness.. Is not that random.

This is known as concentration of mass. This is a very special case of the law of large numbers.

Chandra & Ruta (UIUC) CS473 16 Fall 2016 16 / 37

slide-45
SLIDE 45

Side note...

Law of large numbers (weakest form)...

Informal statement of law of large numbers

For n large enough, the middle portion of the binomial distribution looks like (converges to) the normal/Gaussian distribution.

Chandra & Ruta (UIUC) CS473 17 Fall 2016 17 / 37

slide-46
SLIDE 46

Massive randomness.. Is not that random.

Intuitive conclusion

Randomized algorithm are unpredictable in the tactical level, but very predictable in the strategic level.

Chandra & Ruta (UIUC) CS473 18 Fall 2016 18 / 37

slide-47
SLIDE 47

Massive randomness.. Is not that random.

Intuitive conclusion

Randomized algorithm are unpredictable in the tactical level, but very predictable in the strategic level. Use of well known inequalities in analysis.

Chandra & Ruta (UIUC) CS473 18 Fall 2016 18 / 37

slide-48
SLIDE 48

Randomized QuickSort: A possible analysis

Analysis

Random variable Q = #comparisons made by randomized QuickSort on an array of n elements.

Chandra & Ruta (UIUC) CS473 19 Fall 2016 19 / 37

slide-49
SLIDE 49

Randomized QuickSort: A possible analysis

Analysis

Random variable Q = #comparisons made by randomized QuickSort on an array of n elements. Suppose Pr[Q ≥ 10nlgn] ≤ c. Also we know that Q ≤ n2.

Chandra & Ruta (UIUC) CS473 19 Fall 2016 19 / 37

slide-50
SLIDE 50

Randomized QuickSort: A possible analysis

Analysis

Random variable Q = #comparisons made by randomized QuickSort on an array of n elements. Suppose Pr[Q ≥ 10nlgn] ≤ c. Also we know that Q ≤ n2. E[Q] ≤ 10n log n + (n2 − 10n log n)c.

Chandra & Ruta (UIUC) CS473 19 Fall 2016 19 / 37

slide-51
SLIDE 51

Randomized QuickSort: A possible analysis

Analysis

Random variable Q = #comparisons made by randomized QuickSort on an array of n elements. Suppose Pr[Q ≥ 10nlgn] ≤ c. Also we know that Q ≤ n2. E[Q] ≤ 10n log n + (n2 − 10n log n)c.

Question:

How to find c, or in other words bound Pr[Q ≥ 10n log n]?

Chandra & Ruta (UIUC) CS473 19 Fall 2016 19 / 37

slide-52
SLIDE 52

Markov’s Inequality

Markov’s inequality

Let X be a non-negative random variable over a probability space (Ω, Pr). For any a > 0, Pr[X ≥ a] ≤ E[X] a

Chandra & Ruta (UIUC) CS473 20 Fall 2016 20 / 37

slide-53
SLIDE 53

Markov’s Inequality

Markov’s inequality

Let X be a non-negative random variable over a probability space (Ω, Pr). For any a > 0, Pr[X ≥ a] ≤ E[X] a

Proof:

E[X] =

  • ω∈Ω X(ω) Pr[ω]

  • ω∈Ω, X(ω)≥a X(ω) Pr[ω]

≥ a

ω∈Ω, X(ω)≥a Pr[ω]

= a Pr[X ≥ a]

Chandra & Ruta (UIUC) CS473 20 Fall 2016 20 / 37

slide-54
SLIDE 54

Markov’s Inequality: Proof by Picture

Chandra & Ruta (UIUC) CS473 21 Fall 2016 21 / 37

slide-55
SLIDE 55

Example: Balls in a bin

n black and white balls in a bin. We wish to estimate the fraction of black balls. Lets say it is p∗.

Chandra & Ruta (UIUC) CS473 22 Fall 2016 22 / 37

slide-56
SLIDE 56

Example: Balls in a bin

n black and white balls in a bin. We wish to estimate the fraction of black balls. Lets say it is p∗. An approach: Draw k balls with replacement. If B are black then output p = B

k .

Chandra & Ruta (UIUC) CS473 22 Fall 2016 22 / 37

slide-57
SLIDE 57

Example: Balls in a bin

n black and white balls in a bin. We wish to estimate the fraction of black balls. Lets say it is p∗. An approach: Draw k balls with replacement. If B are black then output p = B

k .

Question

How large k needs to be before our estimated value p is close to p∗?

Chandra & Ruta (UIUC) CS473 22 Fall 2016 22 / 37

slide-58
SLIDE 58

Example: Balls in a bin

A rough estimate through Markov’s inequality.

Lemma

For any k ≥ 1, Pr[p ≥ 2p∗] ≤ 1

2

Chandra & Ruta (UIUC) CS473 23 Fall 2016 23 / 37

slide-59
SLIDE 59

Example: Balls in a bin

A rough estimate through Markov’s inequality.

Lemma

For any k ≥ 1, Pr[p ≥ 2p∗] ≤ 1

2

Proof.

For each 1 ≤ i ≤ k define random variable Xi, which is 1 if ith ball is black, otherwise 0. E[Xi] = Pr[Xi = 1] = p∗.

Chandra & Ruta (UIUC) CS473 23 Fall 2016 23 / 37

slide-60
SLIDE 60

Example: Balls in a bin

A rough estimate through Markov’s inequality.

Lemma

For any k ≥ 1, Pr[p ≥ 2p∗] ≤ 1

2

Proof.

For each 1 ≤ i ≤ k define random variable Xi, which is 1 if ith ball is black, otherwise 0. E[Xi] = Pr[Xi = 1] = p∗. B = k

i=1 Xi, then E[B] = k i=1 E[Xi] = kp∗. p = B/k.

Chandra & Ruta (UIUC) CS473 23 Fall 2016 23 / 37

slide-61
SLIDE 61

Example: Balls in a bin

A rough estimate through Markov’s inequality.

Lemma

For any k ≥ 1, Pr[p ≥ 2p∗] ≤ 1

2

Proof.

For each 1 ≤ i ≤ k define random variable Xi, which is 1 if ith ball is black, otherwise 0. E[Xi] = Pr[Xi = 1] = p∗. B = k

i=1 Xi, then E[B] = k i=1 E[Xi] = kp∗. p = B/k.

Markov’s inequality gives, Pr[p ≥ 2p∗] = Pr B k ≥ 2p∗

  • = Pr[B ≥ 2kp∗] = Pr[B ≥ 2 E[B]] ≤ 1

2

Chandra & Ruta (UIUC) CS473 23 Fall 2016 23 / 37

slide-62
SLIDE 62

Chebyshev’s Inequality: Variance

Variance

Given a random variable X over probability space (Ω, Pr), variance

  • f X is the measure of how much does it deviate from its mean value.

Formally, Var(X) = E

  • (X − E[X])2

= E

  • X2

− E[X]2

Chandra & Ruta (UIUC) CS473 24 Fall 2016 24 / 37

slide-63
SLIDE 63

Chebyshev’s Inequality: Variance

Variance

Given a random variable X over probability space (Ω, Pr), variance

  • f X is the measure of how much does it deviate from its mean value.

Formally, Var(X) = E

  • (X − E[X])2

= E

  • X2

− E[X]2

Intuitive Derivation

Define Y = (X − E[X])2 = X2 − 2X E[X] + E[X]2.

Chandra & Ruta (UIUC) CS473 24 Fall 2016 24 / 37

slide-64
SLIDE 64

Chebyshev’s Inequality: Variance

Variance

Given a random variable X over probability space (Ω, Pr), variance

  • f X is the measure of how much does it deviate from its mean value.

Formally, Var(X) = E

  • (X − E[X])2

= E

  • X2

− E[X]2

Intuitive Derivation

Define Y = (X − E[X])2 = X2 − 2X E[X] + E[X]2. Var(X) = E[Y] = E

  • X2

− 2 E[X] E[X] + E[X]2 = E

  • X2

− E[X]2

Chandra & Ruta (UIUC) CS473 24 Fall 2016 24 / 37

slide-65
SLIDE 65

Chebyshev’s Inequality: Variance

Independence

Random variables X and Y are called mutually independent if ∀x, y ∈ R, Pr[X = x ∧ Y = y] = Pr[X = x] Pr[Y = y]

Lemma

If X and Y are independent random variables then Var(X + Y) = Var(X) + Var(Y).

Chandra & Ruta (UIUC) CS473 25 Fall 2016 25 / 37

slide-66
SLIDE 66

Chebyshev’s Inequality: Variance

Independence

Random variables X and Y are called mutually independent if ∀x, y ∈ R, Pr[X = x ∧ Y = y] = Pr[X = x] Pr[Y = y]

Lemma

If X and Y are independent random variables then Var(X + Y) = Var(X) + Var(Y).

Lemma

If X and Y are mutually independent, then E[XY] = E[X] E[Y].

Chandra & Ruta (UIUC) CS473 25 Fall 2016 25 / 37

slide-67
SLIDE 67

Chebyshev’s Inequality

Chebyshev’s Inequality

Given a ≥ 0, Pr[|X − E[X] | ≥ a] ≤ Var(X)

a2

Chandra & Ruta (UIUC) CS473 26 Fall 2016 26 / 37

slide-68
SLIDE 68

Chebyshev’s Inequality

Chebyshev’s Inequality

Given a ≥ 0, Pr[|X − E[X] | ≥ a] ≤ Var(X)

a2

Proof.

Y = (X − E[X])2 is a non-negative random variable. Apply Markov’s Inequality to Y for a2. Pr

  • Y ≥ a2

≤ E

[Y]/a2

⇔ Pr

  • (X − E[X])2 ≥ a2

≤ Var(X)/a2 ⇔ Pr[|X − E[X] | ≥ a] ≤ Var(X)/a2

Chandra & Ruta (UIUC) CS473 26 Fall 2016 26 / 37

slide-69
SLIDE 69

Chebyshev’s Inequality

Chebyshev’s Inequality

Given a ≥ 0, Pr[|X − E[X] | ≥ a] ≤ Var(X)

a2

Proof.

Y = (X − E[X])2 is a non-negative random variable. Apply Markov’s Inequality to Y for a2. Pr

  • Y ≥ a2

≤ E

[Y]/a2

⇔ Pr

  • (X − E[X])2 ≥ a2

≤ Var(X)/a2 ⇔ Pr[|X − E[X] | ≥ a] ≤ Var(X)/a2 Pr[X ≤ E[X] − a] ≤ Var(X)/a2 AND Pr[X ≥ E[X] + a] ≤ Var(X)/a2

Chandra & Ruta (UIUC) CS473 26 Fall 2016 26 / 37

slide-70
SLIDE 70

Example:Balls in a bin (contd)

Lemma

For 0 < ǫ < 1 and k ≥ 1, Pr[|p − p∗| > ǫ] ≤ 1/kǫ2.

Proof.

Recall: Xi is 1 is ith ball is black, else 0, B = k

i=1 Xi.

E[Xi] = p∗, E[B] = kp∗. p = B/k.

Chandra & Ruta (UIUC) CS473 27 Fall 2016 27 / 37

slide-71
SLIDE 71

Example:Balls in a bin (contd)

Lemma

For 0 < ǫ < 1 and k ≥ 1, Pr[|p − p∗| > ǫ] ≤ 1/kǫ2.

Proof.

Recall: Xi is 1 is ith ball is black, else 0, B = k

i=1 Xi.

E[Xi] = p∗, E[B] = kp∗. p = B/k. Var(Xi) = E

  • X2

i

  • − E[Xi]2 = E[Xi] − E[Xi]2 = p∗(1 − p∗)

Chandra & Ruta (UIUC) CS473 27 Fall 2016 27 / 37

slide-72
SLIDE 72

Example:Balls in a bin (contd)

Lemma

For 0 < ǫ < 1 and k ≥ 1, Pr[|p − p∗| > ǫ] ≤ 1/kǫ2.

Proof.

Recall: Xi is 1 is ith ball is black, else 0, B = k

i=1 Xi.

E[Xi] = p∗, E[B] = kp∗. p = B/k. Var(Xi) = E

  • X2

i

  • − E[Xi]2 = E[Xi] − E[Xi]2 = p∗(1 − p∗)

Var(B) =

i Var(Xi) = kp∗(1 − p∗) (Exercise)

Chandra & Ruta (UIUC) CS473 27 Fall 2016 27 / 37

slide-73
SLIDE 73

Example:Balls in a bin (contd)

Lemma

For 0 < ǫ < 1 and k ≥ 1, Pr[|p − p∗| > ǫ] ≤ 1/kǫ2.

Proof.

Recall: Xi is 1 is ith ball is black, else 0, B = k

i=1 Xi.

E[Xi] = p∗, E[B] = kp∗. p = B/k. Var(Xi) = E

  • X2

i

  • − E[Xi]2 = E[Xi] − E[Xi]2 = p∗(1 − p∗)

Var(B) =

i Var(Xi) = kp∗(1 − p∗) (Exercise)

Pr[|B/k − p∗| ≥ ǫ] = Pr[|B − kp∗| ≥ kǫ] (Chebyshev) ≤

Var(B)/k2ǫ2 = kp∗(1−p∗)/k2ǫ2

<

1/kǫ2

Chandra & Ruta (UIUC) CS473 27 Fall 2016 27 / 37

slide-74
SLIDE 74

Chernoff Bound

Lemma

Let X1, . . . , Xk be k independent random variables such that, for each i ∈ [1, k], Xi equals 1 with probability pi, and 0 with probability (1 − pi).

Chandra & Ruta (UIUC) CS473 28 Fall 2016 28 / 37

slide-75
SLIDE 75

Chernoff Bound

Lemma

Let X1, . . . , Xk be k independent random variables such that, for each i ∈ [1, k], Xi equals 1 with probability pi, and 0 with probability (1 − pi). Let X = k

i=1 Xi and µ = E[X] = i pi. For

any 0 < δ < 1, it holds that: Pr[|X − µ| ≥ δµ] ≤ 2e

−δ2µ 3 Chandra & Ruta (UIUC) CS473 28 Fall 2016 28 / 37

slide-76
SLIDE 76

Chernoff Bound

Lemma

Let X1, . . . , Xk be k independent random variables such that, for each i ∈ [1, k], Xi equals 1 with probability pi, and 0 with probability (1 − pi). Let X = k

i=1 Xi and µ = E[X] = i pi. For

any 0 < δ < 1, it holds that: Pr[|X − µ| ≥ δµ] ≤ 2e

−δ2µ 3

Pr[X ≥ (1 + δ)µ] ≤ e

−δ2µ 3

and Pr[X ≤ (1 − δ)µ] ≤ e

−δ2µ 2 Chandra & Ruta (UIUC) CS473 28 Fall 2016 28 / 37

slide-77
SLIDE 77

Chernoff Bound

Lemma

Let X1, . . . , Xk be k independent random variables such that, for each i ∈ [1, k], Xi equals 1 with probability pi, and 0 with probability (1 − pi). Let X = k

i=1 Xi and µ = E[X] = i pi. For

any 0 < δ < 1, it holds that: Pr[|X − µ| ≥ δµ] ≤ 2e

−δ2µ 3

Pr[X ≥ (1 + δ)µ] ≤ e

−δ2µ 3

and Pr[X ≤ (1 − δ)µ] ≤ e

−δ2µ 2

Proof.

In notes!

Chandra & Ruta (UIUC) CS473 28 Fall 2016 28 / 37

slide-78
SLIDE 78

Example:Balls in a bin (Contd.)

Lemma

For any 0 < ǫ < 1, and k ≥ 1, Pr[|p − p∗| > ǫ] ≤ 2e− kǫ2

3 .

Proof.

Recall: Xi is 1 is ith ball is black, else 0, B = k

i=1 Xi.

E[Xi] = p∗, E[B] = kp∗. p = B/k.

Chandra & Ruta (UIUC) CS473 29 Fall 2016 29 / 37

slide-79
SLIDE 79

Example:Balls in a bin (Contd.)

Lemma

For any 0 < ǫ < 1, and k ≥ 1, Pr[|p − p∗| > ǫ] ≤ 2e− kǫ2

3 .

Proof.

Recall: Xi is 1 is ith ball is black, else 0, B = k

i=1 Xi.

E[Xi] = p∗, E[B] = kp∗. p = B/k. Pr[|p − p∗| ≥ ǫ] = Pr

  • | B

k − p∗| ≥ ǫ

  • =

Pr[|B − kp∗| ≥ kǫ] = Pr

  • |B − kp∗| ≥ ( ǫ

p∗)kp∗

Chandra & Ruta (UIUC) CS473 29 Fall 2016 29 / 37

slide-80
SLIDE 80

Example:Balls in a bin (Contd.)

Lemma

For any 0 < ǫ < 1, and k ≥ 1, Pr[|p − p∗| > ǫ] ≤ 2e− kǫ2

3 .

Proof.

Recall: Xi is 1 is ith ball is black, else 0, B = k

i=1 Xi.

E[Xi] = p∗, E[B] = kp∗. p = B/k. Pr[|p − p∗| ≥ ǫ] = Pr

  • | B

k − p∗| ≥ ǫ

  • =

Pr[|B − kp∗| ≥ kǫ] = Pr

  • |B − kp∗| ≥ ( ǫ

p∗)kp∗

(Chernoff) ≤ 2e

ǫ2 3p∗2 kp∗

= 2e− kǫ2

3p∗

(p∗ ≤ 1) ≤ 2e− kǫ2

3 Chandra & Ruta (UIUC) CS473 29 Fall 2016 29 / 37

slide-81
SLIDE 81

Example Summary

The problem was to estimate the fraction of black balls p∗ in a bin filled with white and black balls. Our estimate was p = B

k instead,

where out of k draws (with replacement) B balls turns out black.

Markov’s Inequality

For any k ≥ 1, Pr[p ≥ 2p∗] ≤ 1

2

Chandra & Ruta (UIUC) CS473 30 Fall 2016 30 / 37

slide-82
SLIDE 82

Example Summary

The problem was to estimate the fraction of black balls p∗ in a bin filled with white and black balls. Our estimate was p = B

k instead,

where out of k draws (with replacement) B balls turns out black.

Markov’s Inequality

For any k ≥ 1, Pr[p ≥ 2p∗] ≤ 1

2

Chebyshev’s Inequality

For any 0 < ǫ < 1, and k ≥ 1, Pr[|p − p∗| > ǫ] ≤ 1/kǫ2.

Chandra & Ruta (UIUC) CS473 30 Fall 2016 30 / 37

slide-83
SLIDE 83

Example Summary

The problem was to estimate the fraction of black balls p∗ in a bin filled with white and black balls. Our estimate was p = B

k instead,

where out of k draws (with replacement) B balls turns out black.

Markov’s Inequality

For any k ≥ 1, Pr[p ≥ 2p∗] ≤ 1

2

Chebyshev’s Inequality

For any 0 < ǫ < 1, and k ≥ 1, Pr[|p − p∗| > ǫ] ≤ 1/kǫ2.

Chernoff Bound

For any 0 < ǫ < 1, and k ≥ 1, Pr[|p − p∗| > ǫ] ≤ 2e− kǫ2

3 . Chandra & Ruta (UIUC) CS473 30 Fall 2016 30 / 37

slide-84
SLIDE 84

Part III Randomized QuickSort (Contd.)

Chandra & Ruta (UIUC) CS473 31 Fall 2016 31 / 37

slide-85
SLIDE 85

Randomized QuickSort: Recall

Input: Array A of n numbers. Output: Numbers in sorted order.

Randomized QuickSort

1

Pick a pivot element uniformly at random from A.

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.

Chandra & Ruta (UIUC) CS473 32 Fall 2016 32 / 37

slide-86
SLIDE 86

Randomized QuickSort: Recall

Input: Array A of n numbers. Output: Numbers in sorted order.

Randomized QuickSort

1

Pick a pivot element uniformly at random from A.

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. Note: On every input randomized QuickSort takes O(n log n) time in expectation. On every input it may take Ω(n2) time with some small probability.

Chandra & Ruta (UIUC) CS473 32 Fall 2016 32 / 37

slide-87
SLIDE 87

Randomized QuickSort: Recall

Input: Array A of n numbers. Output: Numbers in sorted order.

Randomized QuickSort

1

Pick a pivot element uniformly at random from A.

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. Note: On every input randomized QuickSort takes O(n log n) time in expectation. On every input it may take Ω(n2) time with some small probability. Question: With what probability it takes O(n log n) time?

Chandra & Ruta (UIUC) CS473 32 Fall 2016 32 / 37

slide-88
SLIDE 88

Randomized QuickSort: High Probability Analysis

Informal Statement

Random variable Q(A) = # comparisons done by the algorithm. We will show that Pr[Q(A) ≤ 32n ln n] ≥ 1 − 1/n3.

Chandra & Ruta (UIUC) CS473 33 Fall 2016 33 / 37

slide-89
SLIDE 89

Randomized QuickSort: High Probability Analysis

Informal Statement

Random variable Q(A) = # comparisons done by the algorithm. We will show that Pr[Q(A) ≤ 32n ln n] ≥ 1 − 1/n3. If n = 100 then this gives Pr[Q(A) ≤ 32n ln n] ≥ 0.99999.

Chandra & Ruta (UIUC) CS473 33 Fall 2016 33 / 37

slide-90
SLIDE 90

Randomized QuickSort: High Probability Analysis

Informal Statement

Random variable Q(A) = # comparisons done by the algorithm. We will show that Pr[Q(A) ≤ 32n ln n] ≥ 1 − 1/n3.

Outline of the proof

If depth of recursion is k then Q(A) ≤ kn. Prove that depth of recursion ≤ 32 ln n with high probability. Which will imply the result.

Chandra & Ruta (UIUC) CS473 34 Fall 2016 34 / 37

slide-91
SLIDE 91

Randomized QuickSort: High Probability Analysis

Informal Statement

Random variable Q(A) = # comparisons done by the algorithm. We will show that Pr[Q(A) ≤ 32n ln n] ≥ 1 − 1/n3.

Outline of the proof

If depth of recursion is k then Q(A) ≤ kn. Prove that depth of recursion ≤ 32 ln n with high probability. Which will imply the result.

1

Gocus on a single element. Prove that it “participates” in > 32 ln n levels with probability at most 1/n4.

2

By union bound, any of the n elements participates in > 32 ln n levels with probability at most

Chandra & Ruta (UIUC) CS473 34 Fall 2016 34 / 37

slide-92
SLIDE 92

Randomized QuickSort: High Probability Analysis

Informal Statement

Random variable Q(A) = # comparisons done by the algorithm. We will show that Pr[Q(A) ≤ 32n ln n] ≥ 1 − 1/n3.

Outline of the proof

If depth of recursion is k then Q(A) ≤ kn. Prove that depth of recursion ≤ 32 ln n with high probability. Which will imply the result.

1

Gocus on a single element. Prove that it “participates” in > 32 ln n levels with probability at most 1/n4.

2

By union bound, any of the n elements participates in > 32 ln n levels with probability at most 1/n3.

Chandra & Ruta (UIUC) CS473 34 Fall 2016 34 / 37

slide-93
SLIDE 93

Randomized QuickSort: High Probability Analysis

Informal Statement

Random variable Q(A) = # comparisons done by the algorithm. We will show that Pr[Q(A) ≤ 32n ln n] ≥ 1 − 1/n3.

Outline of the proof

If depth of recursion is k then Q(A) ≤ kn. Prove that depth of recursion ≤ 32 ln n with high probability. Which will imply the result.

1

Gocus on a single element. Prove that it “participates” in > 32 ln n levels with probability at most 1/n4.

2

By union bound, any of the n elements participates in > 32 ln n levels with probability at most 1/n3.

3

Therefore, all elements participate in ≤ 32 ln n w.p. (1 − 1/n3).

Chandra & Ruta (UIUC) CS473 34 Fall 2016 34 / 37

slide-94
SLIDE 94

Randomized QuickSort: High Probability Analysis

If k levels of recursion then kn comparisons.

Chandra & Ruta (UIUC) CS473 35 Fall 2016 35 / 37

slide-95
SLIDE 95

Randomized QuickSort: High Probability Analysis

If k levels of recursion then kn comparisons. Fix an element s ∈ A. We will track it at each level. Let Si be the partition containing s at ith level. S1 = A and Sk = {s}.

Chandra & Ruta (UIUC) CS473 35 Fall 2016 35 / 37

slide-96
SLIDE 96

Randomized QuickSort: High Probability Analysis

If k levels of recursion then kn comparisons. Fix an element s ∈ A. We will track it at each level. Let Si be the partition containing s at ith level. S1 = A and Sk = {s}. We call s lucky in ith iteration, if balanced split: |Si+1| ≤ (3/4)|Si| and |Si \ Si+1| ≤ (3/4)|Si|.

Chandra & Ruta (UIUC) CS473 35 Fall 2016 35 / 37

slide-97
SLIDE 97

Randomized QuickSort: High Probability Analysis

If k levels of recursion then kn comparisons. Fix an element s ∈ A. We will track it at each level. Let Si be the partition containing s at ith level. S1 = A and Sk = {s}. We call s lucky in ith iteration, if balanced split: |Si+1| ≤ (3/4)|Si| and |Si \ Si+1| ≤ (3/4)|Si|. If ρ =#lucky rounds in first k rounds, then |Sk| ≤ (3/4)ρn.

Chandra & Ruta (UIUC) CS473 35 Fall 2016 35 / 37

slide-98
SLIDE 98

Randomized QuickSort: High Probability Analysis

If k levels of recursion then kn comparisons. Fix an element s ∈ A. We will track it at each level. Let Si be the partition containing s at ith level. S1 = A and Sk = {s}. We call s lucky in ith iteration, if balanced split: |Si+1| ≤ (3/4)|Si| and |Si \ Si+1| ≤ (3/4)|Si|. If ρ =#lucky rounds in first k rounds, then |Sk| ≤ (3/4)ρn. For |Sk| = 1, ρ = 4 ln n ≥ log4/3 n suffices.

Chandra & Ruta (UIUC) CS473 35 Fall 2016 35 / 37

slide-99
SLIDE 99

How may rounds before 4 ln n lucky rounds?

Xi = 1 if s is lucky in ith iteration.

Chandra & Ruta (UIUC) CS473 36 Fall 2016 36 / 37

slide-100
SLIDE 100

How may rounds before 4 ln n lucky rounds?

Xi = 1 if s is lucky in ith iteration. Observation: X1, . . . , Xk are independent variables. Pr[Xi = 1] = 1

2

Why?

Chandra & Ruta (UIUC) CS473 36 Fall 2016 36 / 37

slide-101
SLIDE 101

How may rounds before 4 ln n lucky rounds?

Xi = 1 if s is lucky in ith iteration. Observation: X1, . . . , Xk are independent variables. Pr[Xi = 1] = 1

2

Why? Clearly, ρ = k

i=1 Xi. Let µ = E[ρ] = k 2.

Chandra & Ruta (UIUC) CS473 36 Fall 2016 36 / 37

slide-102
SLIDE 102

How may rounds before 4 ln n lucky rounds?

Xi = 1 if s is lucky in ith iteration. Observation: X1, . . . , Xk are independent variables. Pr[Xi = 1] = 1

2

Why? Clearly, ρ = k

i=1 Xi. Let µ = E[ρ] = k 2.

Set k = 32 ln n and δ = 3

  • 4. (1 − δ) = 1

4.

Chandra & Ruta (UIUC) CS473 36 Fall 2016 36 / 37

slide-103
SLIDE 103

How may rounds before 4 ln n lucky rounds?

Xi = 1 if s is lucky in ith iteration. Observation: X1, . . . , Xk are independent variables. Pr[Xi = 1] = 1

2

Why? Clearly, ρ = k

i=1 Xi. Let µ = E[ρ] = k 2.

Set k = 32 ln n and δ = 3

  • 4. (1 − δ) = 1

4.

Probability of NOT getting 4 ln n lucky rounds out of 32 ln n rounds is,

Chandra & Ruta (UIUC) CS473 36 Fall 2016 36 / 37

slide-104
SLIDE 104

How may rounds before 4 ln n lucky rounds?

Xi = 1 if s is lucky in ith iteration. Observation: X1, . . . , Xk are independent variables. Pr[Xi = 1] = 1

2

Why? Clearly, ρ = k

i=1 Xi. Let µ = E[ρ] = k 2.

Set k = 32 ln n and δ = 3

  • 4. (1 − δ) = 1

4.

Probability of NOT getting 4 ln n lucky rounds out of 32 ln n rounds is, Pr[ρ ≤ 4 ln n] = Pr[ρ ≤ k/8] = Pr[ρ ≤ (1 − δ)µ]

Chandra & Ruta (UIUC) CS473 36 Fall 2016 36 / 37

slide-105
SLIDE 105

How may rounds before 4 ln n lucky rounds?

Xi = 1 if s is lucky in ith iteration. Observation: X1, . . . , Xk are independent variables. Pr[Xi = 1] = 1

2

Why? Clearly, ρ = k

i=1 Xi. Let µ = E[ρ] = k 2.

Set k = 32 ln n and δ = 3

  • 4. (1 − δ) = 1

4.

Probability of NOT getting 4 ln n lucky rounds out of 32 ln n rounds is, Pr[ρ ≤ 4 ln n] = Pr[ρ ≤ k/8] = Pr[ρ ≤ (1 − δ)µ] (Chernoff) ≤ e

−δ2µ 2

= e− 9k

64

= e−4.5 ln n ≤

1 n4

Chandra & Ruta (UIUC) CS473 36 Fall 2016 36 / 37

slide-106
SLIDE 106

Randomized QuickSort w.h.p. Analysis

n input elements. Probability that depth of recursion in QuickSort > 32 ln n is at most

1 n4 ∗ n = 1 n3.

Chandra & Ruta (UIUC) CS473 37 Fall 2016 37 / 37

slide-107
SLIDE 107

Randomized QuickSort w.h.p. Analysis

n input elements. Probability that depth of recursion in QuickSort > 32 ln n is at most

1 n4 ∗ n = 1 n3.

Theorem

With high probability (i.e., 1 − 1

n3) the depth of the recursion of

QuickSort is ≤ 32 ln n. Due to n comparisons in each level, with high probability, the running time of QuickSort is O(n ln n).

Chandra & Ruta (UIUC) CS473 37 Fall 2016 37 / 37

slide-108
SLIDE 108

Randomized QuickSort w.h.p. Analysis

n input elements. Probability that depth of recursion in QuickSort > 32 ln n is at most

1 n4 ∗ n = 1 n3.

Theorem

With high probability (i.e., 1 − 1

n3) the depth of the recursion of

QuickSort is ≤ 32 ln n. Due to n comparisons in each level, with high probability, the running time of QuickSort is O(n ln n). Q: How to increase the probability?

Chandra & Ruta (UIUC) CS473 37 Fall 2016 37 / 37