Randomness in Computing L ECTURE 6 Last time Conditional - - PowerPoint PPT Presentation

β–Ά
randomness in computing
SMART_READER_LITE
LIVE PREVIEW

Randomness in Computing L ECTURE 6 Last time Conditional - - PowerPoint PPT Presentation

Randomness in Computing L ECTURE 6 Last time Conditional expectation Branching process Geometric RVs Coupon collector problem Today Randomized quicksort Markovs inequality Variance 2/6/2020 Sofya


slide-1
SLIDE 1

2/6/2020

Randomness in Computing

LECTURE 6

Last time

  • Conditional expectation
  • Branching process
  • Geometric RVs
  • Coupon collector problem
  • Today
  • Randomized quicksort
  • Markov’s inequality
  • Variance

Sofya Raskhodnikova;Randomness in Computing

slide-2
SLIDE 2

Quicksort: divide and conquer

  • Find a pivot element
  • Divide: Find the correct position of the pivot

by comparing it to all elements.

  • Conquer: Recursively sort the two parts,

resulting from removing the pivot.

sort sort ≀ 𝑦 𝑦 β‰₯ 𝑦 ≀ 𝑦 𝑦 β‰₯ 𝑦 A:

1 𝒐 pivot

Sofya Raskhodnikova; based on notes by E. Demaine and C. Leiserson

slide-3
SLIDE 3

Exercise

How many comparisons does Quicksort perform on sorted array?

Answer: π‘œ βˆ’ 1 + π‘œ βˆ’ 2 + β‹― + 2 + 1 = π‘œ π‘œ βˆ’ 1 2 = Ξ©(π‘œ2) How many comparisons does Quicksort perform if, in every iteration, the pivot splits the array into two halves?

Answer: Let 𝐷 π‘œ be the number of comparisons performed on an array with π‘œ elements.

𝐷 π‘œ = Θ(π‘œ log π‘œ)

2/6/2020

slide-4
SLIDE 4

2/6/2020

  • S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Randomized Quicksort

BIG IDEA: Partition around a random element.

  • Analysis is similar when the input arrives in

random order.

  • But randomness in the input is unreliable.
  • Rely instead on random number generator.
slide-5
SLIDE 5

Analysis of Randomized Quicksort

  • Theorem. If Quicksort chooses each pivot uniformly and

independently at random from all possibilities then, for any input, the expected number of comparisons is 2π‘œ ln π‘œ + 𝑃(π‘œ). Proof (with an assumption that all elements are distinct):

  • Let π‘Œ be the R.V. for the # of comparisons.
  • Let 𝑦1, 𝑦2, … , π‘¦π‘œ be the input values.
  • Let 𝑧1, 𝑧2, … , π‘§π‘œ be the input values sorted in increasing order.
  • For 𝑗, π‘˜ ∈ π‘œ , 𝑗 < π‘˜, let π‘Œπ‘—π‘˜ be the indicator R.V. for the event that

𝑧𝑗 and π‘§π‘˜ are compared by the algorithm.

π‘Œ =

𝑗,π‘˜ π‘œ :𝑗<π‘˜

π‘Œπ‘—π‘˜ and, by linearity of expextation,𝔽[π‘Œ] =

𝑗,π‘˜ π‘œ :𝑗<π‘˜

𝔽 π‘Œπ‘—π‘˜

2/6/2020

slide-6
SLIDE 6

Analysis of Randomized Quicksort

  • Theorem. The expected number of comparisons is 2π‘œ ln π‘œ + 𝑃(π‘œ).

Proof (continued):

  • Let 𝑧1, 𝑧2, … , π‘§π‘œ be the input values sorted in increasing order.
  • For 𝑗, π‘˜ ∈ π‘œ , 𝑗 < π‘˜, let π‘Œπ‘—π‘˜ be the indicator R.V. for the event that

𝑧𝑗 and π‘§π‘˜ are compared by the algorithm.

  • 𝔽 π‘Œπ‘—π‘˜ = Pr[Xij = 1]
  • 𝑧𝑗 and π‘§π‘˜ are compared iff

either 𝑧𝑗 or π‘§π‘˜ is the first pivot chosen from 𝑍

π‘—π‘˜ = {𝑧𝑗, … , π‘§π‘˜}

  • The first time a pivot is chose from 𝑍

π‘—π‘˜, it is equally likely to be

any of π‘˜ βˆ’ 𝑗 + 1 elements of 𝑍

π‘—π‘˜.

2/6/2020

Sofya Raskhodnikova; Randomness in Computing

slide-7
SLIDE 7

Analysis of Randomized Quicksort

  • Theorem. The expected number of comparisons is 2π‘œ ln π‘œ + 𝑃(π‘œ).

Proof (continued):

2/6/2020

Sofya Raskhodnikova; Randomness in Computing

slide-8
SLIDE 8

Markov’s Inequality

  • Theorem. Let X be a RV taking only nonnegative values.

Then, for all 𝑏 > 0, Pr π‘Œ β‰₯ 𝑏 ≀ 𝔽 [π‘Œ] 𝑏 . Proof: Let a > 0.

2/6/2020

slide-9
SLIDE 9

Markov’s Inequality

  • Theorem. Let X be a RV taking only nonnegative values.

Then, for all 𝑏 > 0, Pr π‘Œ β‰₯ 𝑏 ≀ 𝔽 [π‘Œ] 𝑏 . Alternative proof: Let a > 0.

2/6/2020

slide-10
SLIDE 10

Markov’s Inequality

  • Theorem. Let X be a RV taking only nonnegative values.

Then, for all 𝑏 > 0, Pr π‘Œ β‰₯ 𝑏 ≀ 𝔽 [π‘Œ] 𝑏 .

  • Alternatively: Then, for all 𝑐 > 1,

Pr π‘Œ β‰₯ 𝑐 β‹… 𝔽[π‘Œ] ≀ 1 𝑐 .

  • Example: Show that Randomized Quicksort uses

≀ 10π‘œ ln π‘œ + 𝑃(π‘œ) comparisons with probability at least 4/5.

2/6/2020

slide-11
SLIDE 11

Bernoulli random walk

  • Start at position 0.
  • At every step go up or down by 1 with probability 1/2 each.
  • Let X be the position after π‘œ steps.

What is the probability space?

  • A. Uniform over 0,1 .
  • B. Uniform over {-1,1}.
  • C. 2π‘œ.
  • D. Position after π‘œ steps.
  • E. Uniform over 𝑑1, … , π‘‘π‘œ

𝑑𝑗 ∈ βˆ’1,1 for 𝑗 = 1, … , π‘œ}.

2/6/2020

slide-12
SLIDE 12

Bernoulli random walk

  • Start at position 0.
  • At every step go up or down by 1 with probability 1/2 each.
  • Let X be the position after π‘œ steps.
  • What is E[X]?
  • How far from the origin should we expect X to be?

A precise answer to this question is the expectation of |X|. However, it is easier to work with the expectation of π‘Œ2. (It is not the same! But gives us an idea.)

2/6/2020

slide-13
SLIDE 13

Random variables: variance

  • The variance of a random variable X with expectation

𝐹[π‘Œ] = 𝜈 is Var π‘Œ = 𝐹 π‘Œ βˆ’ 𝜈 2 .

  • Equivalently, Var π‘Œ = 𝐹 π‘Œ2 βˆ’πœˆ2.
  • The standard deviation of X is 𝜏 π‘Œ =

Var π‘Œ .

2/6/2020

slide-14
SLIDE 14

Variance as a measure of spread

  • X= βˆ’2 with probability 1/2

2 with probability 1/2

  • Y=

βˆ’10 with probability 0.001 0 with probability 0.998 10 with probability 0.001

  • Z=

βˆ’5 with probability 1/3 0 with probability 1/3 5 with probability 1/3

  • Compute the variances and standard deviations of X,Y and Z.

2/6/2020

Example from Mathematics: a Discrete introduction by E. Scheinerman