• No results found

Lecture 3: Probabilistic Analysis, Randomized Quicksort

N/A
N/A
Protected

Academic year: 2022

Share "Lecture 3: Probabilistic Analysis, Randomized Quicksort"

Copied!
73
0
0

Loading.... (view fulltext now)

Full text

(1)

Lecture 3: Probabilistic Analysis, Randomized Quicksort

Michael Dinitz

September 7, 2021

601.433/633 Introduction to Algorithms

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 1 / 16

(2)

Introduction: Sorting

Sorting: given array of comparable elements, put them in sorted order

Popular topic to cover in Algorithms courses

This course:

I assume you know the basics (mergesort, quicksort, insertion sort, selection sort, bubble sort, etc.) from Data Structures

Today: more advanced sorting (randomized quicksort)

Next week: Sorting lower bound and ways around it.

(3)

Randomized Algorithms and Probabilistic Analysis

First lecture: “Average-case” problematic.

What is the “average case”?

Want to design algorithms that work in all applications.

Instead of assuming random distribution over inputs (average-case analysis, machine learning), add randomization inside algorithm!

Still assume worst-case inputs, give bound on worst-case expected running time. Many Fall semesters: 601.434/634 Randomized and Big Data Algorithms. Great class! Today: adding randomness into quicksort.

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 3 / 16

(4)

Randomized Algorithms and Probabilistic Analysis

First lecture: “Average-case” problematic.

What is the “average case”?

Want to design algorithms that work in all applications.

Instead of assuming random distribution over inputs (average-case analysis, machine learning), add randomization inside algorithm!

Still assume worst-case inputs, give bound on worst-case expected running time.

Many Fall semesters: 601.434/634 Randomized and Big Data Algorithms. Great class! Today: adding randomness into quicksort.

(5)

Randomized Algorithms and Probabilistic Analysis

First lecture: “Average-case” problematic.

What is the “average case”?

Want to design algorithms that work in all applications.

Instead of assuming random distribution over inputs (average-case analysis, machine learning), add randomization inside algorithm!

Still assume worst-case inputs, give bound on worst-case expected running time.

Many Fall semesters: 601.434/634 Randomized and Big Data Algorithms. Great class!

Today: adding randomness into quicksort.

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 3 / 16

(6)

Randomized Algorithms and Probabilistic Analysis

First lecture: “Average-case” problematic.

What is the “average case”?

Want to design algorithms that work in all applications.

Instead of assuming random distribution over inputs (average-case analysis, machine learning), add randomization inside algorithm!

Still assume worst-case inputs, give bound on worst-case expected running time.

Many Fall semesters: 601.434/634 Randomized and Big Data Algorithms. Great class!

Today: adding randomness into quicksort.

(7)

Quicksort Basics (Review)

Input: array A of length n.

Algorithm:

1. If n= 0 or 1, return A (already sorted) 2. Pick some element p as the pivot

3. Compare every element of A to p. Let L be the elements less than p, let G be the elements larger than p. Create array [L, p, G]

4. Recursively sort L and G.

Not fully specified: how to choose p?

Traditionally: some simple deterministic choice (first element, last element, etc.)

Next lecture: better deterministic choice (not very practical)

Now: first element

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 4 / 16

Dd A

to

(8)

Quicksort Basics (Review)

Input: array A of length n.

Algorithm:

1. If n= 0 or 1, return A (already sorted) 2. Pick some element p as the pivot

3. Compare every element of A to p. Let L be the elements less than p, let G be the elements larger than p. Create array [L, p, G]

4. Recursively sort L and G.

Not fully specified: how to choose p?

Traditionally: some simple deterministic choice (first element, last element, etc.)

Next lecture: better deterministic choice (not very practical)

Now: first element

(9)

Quicksort Analysis

Upper bound:

If p picked as pivot in step 2, then in correct place after step 3

�⇒ step 2 and 3 executed at most n times.

Step 3 takes time O(n) (compare every element to pivot)

�⇒ total time at most O(n2)

Lower Bound:

Suppose A already sorted.

�⇒ p = A[0] is smallest element �⇒ L = � and G = A[1..n − 1]

�⇒ in one call to quicksort, do ⌦(n) work to compare everything to p, then recurse on array of size n− 1

�⇒ running time is T(n) = T(n − 1) + cn �⇒ T(n) = ⇥(n2)

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 5 / 16

(10)

Quicksort Analysis

Upper bound:

If p picked as pivot in step 2, then in correct place after step 3

�⇒ step 2 and 3 executed at most n times.

Step 3 takes time O(n) (compare every element to pivot)

�⇒ total time at most O(n2)

Lower Bound:

Suppose A already sorted.

�⇒ p = A[0] is smallest element �⇒ L = � and G = A[1..n − 1]

�⇒ in one call to quicksort, do ⌦(n) work to compare everything to p, then recurse on array of size n− 1

�⇒ running time is T(n) = T(n − 1) + cn �⇒ T(n) = ⇥(n2)

(11)

Quicksort Analysis

Upper bound:

If p picked as pivot in step 2, then in correct place after step 3

�⇒ step 2 and 3 executed at most n times.

Step 3 takes time O(n) (compare every element to pivot)

�⇒ total time at most O(n2)

Lower Bound:

Suppose A already sorted.

�⇒ p = A[0] is smallest element �⇒ L = � and G = A[1..n − 1]

�⇒ in one call to quicksort, do ⌦(n) work to compare everything to p, then recurse on array of size n− 1

�⇒ running time is T(n) = T(n − 1) + cn �⇒ T(n) = ⇥(n2)

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 5 / 16

(12)

Quicksort Analysis

Upper bound:

If p picked as pivot in step 2, then in correct place after step 3

�⇒ step 2 and 3 executed at most n times.

Step 3 takes time O(n) (compare every element to pivot)

�⇒ total time at most O(n2)

Lower Bound:

Suppose A already sorted.

�⇒ p = A[0] is smallest element �⇒ L = � and G = A[1..n − 1]

�⇒ in one call to quicksort, do ⌦(n) work to compare everything to p, then recurse on array of size n− 1

�⇒ running time is T(n) = T(n − 1) + cn �⇒ T(n) = ⇥(n2)

(13)

Quicksort Analysis

Upper bound:

If p picked as pivot in step 2, then in correct place after step 3

�⇒ step 2 and 3 executed at most n times.

Step 3 takes time O(n) (compare every element to pivot)

�⇒ total time at most O(n2)

Lower Bound:

Suppose A already sorted.

�⇒ p = A[0] is smallest element �⇒ L = � and G = A[1..n − 1]

�⇒ in one call to quicksort, do ⌦(n) work to compare everything to p, then recurse on array of size n− 1

�⇒ running time is T(n) = T(n − 1) + cn �⇒ T(n) = ⇥(n2)

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 5 / 16

(14)

Quicksort Analysis

Upper bound:

If p picked as pivot in step 2, then in correct place after step 3

�⇒ step 2 and 3 executed at most n times.

Step 3 takes time O(n) (compare every element to pivot)

�⇒ total time at most O(n2)

Lower Bound:

Suppose A already sorted.

�⇒ p = A[0] is smallest element

�⇒ L = � and G = A[1..n − 1]

�⇒ in one call to quicksort, do ⌦(n) work to compare everything to p, then recurse on array of size n− 1

�⇒ running time is T(n) = T(n − 1) + cn �⇒ T(n) = ⇥(n2)

(15)

Quicksort Analysis

Upper bound:

If p picked as pivot in step 2, then in correct place after step 3

�⇒ step 2 and 3 executed at most n times.

Step 3 takes time O(n) (compare every element to pivot)

�⇒ total time at most O(n2)

Lower Bound:

Suppose A already sorted.

�⇒ p = A[0] is smallest element �⇒ L = � and G = A[1..n − 1]

�⇒ in one call to quicksort, do ⌦(n) work to compare everything to p, then recurse on array of size n− 1

�⇒ running time is T(n) = T(n − 1) + cn �⇒ T(n) = ⇥(n2)

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 5 / 16

(16)

Quicksort Analysis

Upper bound:

If p picked as pivot in step 2, then in correct place after step 3

�⇒ step 2 and 3 executed at most n times.

Step 3 takes time O(n) (compare every element to pivot)

�⇒ total time at most O(n2)

Lower Bound:

Suppose A already sorted.

�⇒ p = A[0] is smallest element �⇒ L = � and G = A[1..n − 1]

�⇒ in one call to quicksort, do ⌦(n) work to compare everything to p, then recurse on array of size n− 1

�⇒ running time is T(n) = T(n − 1) + cn �⇒ T(n) = ⇥(n2)

(17)

Quicksort Analysis

Upper bound:

If p picked as pivot in step 2, then in correct place after step 3

�⇒ step 2 and 3 executed at most n times.

Step 3 takes time O(n) (compare every element to pivot)

�⇒ total time at most O(n2)

Lower Bound:

Suppose A already sorted.

�⇒ p = A[0] is smallest element �⇒ L = � and G = A[1..n − 1]

�⇒ in one call to quicksort, do ⌦(n) work to compare everything to p, then recurse on array of size n− 1

�⇒ running time is T(n) = T(n − 1) + cn

�⇒ T(n) = ⇥(n2)

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 5 / 16

(18)

Quicksort Analysis

Upper bound:

If p picked as pivot in step 2, then in correct place after step 3

�⇒ step 2 and 3 executed at most n times.

Step 3 takes time O(n) (compare every element to pivot)

�⇒ total time at most O(n2)

Lower Bound:

Suppose A already sorted.

�⇒ p = A[0] is smallest element �⇒ L = � and G = A[1..n − 1]

�⇒ in one call to quicksort, do ⌦(n) work to compare everything to p, then recurse on array of size n− 1

�⇒ running time is T(n) = T(n − 1) + cn �⇒ T(n) = ⇥(n2)

(19)

Randomized Quicksort

Randomized Quicksort: pick p uniformly at random from A.

Today: prove that expected running time at most O(n log n) for every input A.

Better than an average-case bound: holds for every single input!

Maybe in one application inputs tend to be pretty well-sorted: original deterministic quicksort bad, this still good!

Today only expectation. Can be more clever to get high probability bounds. Before doing analysis, quick review of basic probability theory.

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 6 / 16

(20)

Randomized Quicksort

Randomized Quicksort: pick p uniformly at random from A.

Today: prove that expected running time at most O(n log n) for every input A.

Better than an average-case bound: holds for every single input!

Maybe in one application inputs tend to be pretty well-sorted: original deterministic quicksort bad, this still good!

Today only expectation. Can be more clever to get high probability bounds. Before doing analysis, quick review of basic probability theory.

(21)

Randomized Quicksort

Randomized Quicksort: pick p uniformly at random from A.

Today: prove that expected running time at most O(n log n) for every input A.

Better than an average-case bound: holds for every single input!

Maybe in one application inputs tend to be pretty well-sorted: original deterministic quicksort bad, this still good!

Today only expectation. Can be more clever to get high probability bounds.

Before doing analysis, quick review of basic probability theory.

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 6 / 16

(22)

Randomized Quicksort

Randomized Quicksort: pick p uniformly at random from A.

Today: prove that expected running time at most O(n log n) for every input A.

Better than an average-case bound: holds for every single input!

Maybe in one application inputs tend to be pretty well-sorted: original deterministic quicksort bad, this still good!

Today only expectation. Can be more clever to get high probability bounds.

Before doing analysis, quick review of basic probability theory.

(23)

Probability Basics I

Only semi-formal here. Look at CLRS Chapter 5, take Introduction to Probability

⌦: Sample space. Set of all possible outcomes.

Roll two dice. ⌦ = {1, 2, . . . , 6} × {1, 2, . . . , 6}. Not {2, 3, . . . , 12} Event: subset of ⌦

“Event that first die is 3”: {(3, x) ∶ x ∈ {1, 2, . . . , 6}}

“Event that dice add up to 7 or 11”: {(x, y) ∈ ⌦ ∶ (x + y = 7) or (x + y = 11)} Random Variable: X∶ ⌦ → R

X1: value of first die. X1(x, y) = x

X2: value of second die. X2(x, y) = y

X = X1+ X2: sum of the dice. X(x, y) = x + y = X1(x, y) + X2(x, y)

Random variables super important! Running time of randomized quicksort is a random variable.

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 7 / 16

(24)

Probability Basics I

Only semi-formal here. Look at CLRS Chapter 5, take Introduction to Probability

⌦: Sample space. Set of all possible outcomes.

Roll two dice. ⌦ = {1, 2, . . . , 6} × {1, 2, . . . , 6}. Not {2, 3, . . . , 12} Event: subset of ⌦

“Event that first die is 3”: {(3, x) ∶ x ∈ {1, 2, . . . , 6}}

“Event that dice add up to 7 or 11”: {(x, y) ∈ ⌦ ∶ (x + y = 7) or (x + y = 11)} Random Variable: X∶ ⌦ → R

X1: value of first die. X1(x, y) = x

X2: value of second die. X2(x, y) = y

X = X1+ X2: sum of the dice. X(x, y) = x + y = X1(x, y) + X2(x, y)

Random variables super important! Running time of randomized quicksort is a random variable.

(25)

Probability Basics I

Only semi-formal here. Look at CLRS Chapter 5, take Introduction to Probability

⌦: Sample space. Set of all possible outcomes.

Roll two dice. ⌦ =

{1, 2, . . . , 6} × {1, 2, . . . , 6}. Not {2, 3, . . . , 12} Event: subset of ⌦

“Event that first die is 3”: {(3, x) ∶ x ∈ {1, 2, . . . , 6}}

“Event that dice add up to 7 or 11”: {(x, y) ∈ ⌦ ∶ (x + y = 7) or (x + y = 11)} Random Variable: X∶ ⌦ → R

X1: value of first die. X1(x, y) = x

X2: value of second die. X2(x, y) = y

X = X1+ X2: sum of the dice. X(x, y) = x + y = X1(x, y) + X2(x, y)

Random variables super important! Running time of randomized quicksort is a random variable.

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 7 / 16

(26)

Probability Basics I

Only semi-formal here. Look at CLRS Chapter 5, take Introduction to Probability

⌦: Sample space. Set of all possible outcomes.

Roll two dice. ⌦ = {1, 2, . . . , 6} × {1, 2, . . . , 6}.

Not {2, 3, . . . , 12} Event: subset of ⌦

“Event that first die is 3”: {(3, x) ∶ x ∈ {1, 2, . . . , 6}}

“Event that dice add up to 7 or 11”: {(x, y) ∈ ⌦ ∶ (x + y = 7) or (x + y = 11)} Random Variable: X∶ ⌦ → R

X1: value of first die. X1(x, y) = x

X2: value of second die. X2(x, y) = y

X = X1+ X2: sum of the dice. X(x, y) = x + y = X1(x, y) + X2(x, y)

Random variables super important! Running time of randomized quicksort is a random variable.

G

x G

(27)

Probability Basics I

Only semi-formal here. Look at CLRS Chapter 5, take Introduction to Probability

⌦: Sample space. Set of all possible outcomes.

Roll two dice. ⌦ = {1, 2, . . . , 6} × {1, 2, . . . , 6}. Not {2, 3, . . . , 12}

Event: subset of ⌦

“Event that first die is 3”: {(3, x) ∶ x ∈ {1, 2, . . . , 6}}

“Event that dice add up to 7 or 11”: {(x, y) ∈ ⌦ ∶ (x + y = 7) or (x + y = 11)} Random Variable: X∶ ⌦ → R

X1: value of first die. X1(x, y) = x

X2: value of second die. X2(x, y) = y

X = X1+ X2: sum of the dice. X(x, y) = x + y = X1(x, y) + X2(x, y)

Random variables super important! Running time of randomized quicksort is a random variable.

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 7 / 16

(28)

Probability Basics I

Only semi-formal here. Look at CLRS Chapter 5, take Introduction to Probability

⌦: Sample space. Set of all possible outcomes.

Roll two dice. ⌦ = {1, 2, . . . , 6} × {1, 2, . . . , 6}. Not {2, 3, . . . , 12}

Event: subset of ⌦

“Event that first die is 3”: {(3, x) ∶ x ∈ {1, 2, . . . , 6}}

“Event that dice add up to 7 or 11”: {(x, y) ∈ ⌦ ∶ (x + y = 7) or (x + y = 11)} Random Variable: X∶ ⌦ → R

X1: value of first die. X1(x, y) = x

X2: value of second die. X2(x, y) = y

X = X1+ X2: sum of the dice. X(x, y) = x + y = X1(x, y) + X2(x, y)

Random variables super important! Running time of randomized quicksort is a random variable.

Fa

(29)

Probability Basics I

Only semi-formal here. Look at CLRS Chapter 5, take Introduction to Probability

⌦: Sample space. Set of all possible outcomes.

Roll two dice. ⌦ = {1, 2, . . . , 6} × {1, 2, . . . , 6}. Not {2, 3, . . . , 12}

Event: subset of ⌦

“Event that first die is 3”: {(3, x) ∶ x ∈ {1, 2, . . . , 6}}

“Event that dice add up to 7 or 11”: {(x, y) ∈ ⌦ ∶ (x + y = 7) or (x + y = 11)}

Random Variable: X∶ ⌦ → R

X1: value of first die. X1(x, y) = x

X2: value of second die. X2(x, y) = y

X = X1+ X2: sum of the dice. X(x, y) = x + y = X1(x, y) + X2(x, y)

Random variables super important! Running time of randomized quicksort is a random variable.

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 7 / 16

(30)

Probability Basics I

Only semi-formal here. Look at CLRS Chapter 5, take Introduction to Probability

⌦: Sample space. Set of all possible outcomes.

Roll two dice. ⌦ = {1, 2, . . . , 6} × {1, 2, . . . , 6}. Not {2, 3, . . . , 12}

Event: subset of ⌦

“Event that first die is 3”: {(3, x) ∶ x ∈ {1, 2, . . . , 6}}

“Event that dice add up to 7 or 11”: {(x, y) ∈ ⌦ ∶ (x + y = 7) or (x + y = 11)}

Random Variable: X∶ ⌦ → R

X1: value of first die. X1(x, y) = x

X2: value of second die. X2(x, y) = y

X = X1+ X2: sum of the dice. X(x, y) = x + y = X1(x, y) + X2(x, y)

Random variables super important! Running time of randomized quicksort is a random variable.

O O

(31)

Probability Basics I

Only semi-formal here. Look at CLRS Chapter 5, take Introduction to Probability

⌦: Sample space. Set of all possible outcomes.

Roll two dice. ⌦ = {1, 2, . . . , 6} × {1, 2, . . . , 6}. Not {2, 3, . . . , 12}

Event: subset of ⌦

“Event that first die is 3”: {(3, x) ∶ x ∈ {1, 2, . . . , 6}}

“Event that dice add up to 7 or 11”: {(x, y) ∈ ⌦ ∶ (x + y = 7) or (x + y = 11)}

Random Variable: X∶ ⌦ → R

X1: value of first die. X1(x, y) = x

X2: value of second die. X2(x, y) = y

X = X1+ X2: sum of the dice. X(x, y) = x + y = X1(x, y) + X2(x, y)

Random variables super important! Running time of randomized quicksort is a random variable.

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 7 / 16

(32)

Probability Basics II

Want to define probabilities. Should use measure theory. Won’t.

For each e∈ ⌦ let Pr[e] be probability of e (probability distribution)

Pr[e] ≥ 0 for all e ∈ ⌦, and ∑e∈⌦Pr[e] = 1

Probability of an event A is Pr[A] = ∑e∈APr[e] Conditional probability: if A and B are events:

Pr[B�A] = Pr[A �B]

Pr[A] = ∑e∈A∩BPr[e]

e∈APr[e]

(33)

Probability Basics II

Want to define probabilities. Should use measure theory. Won’t.

For each e∈ ⌦ let Pr[e] be probability of e (probability distribution)

Pr[e] ≥ 0 for all e ∈ ⌦, and ∑e∈⌦Pr[e] = 1

Probability of an event A is Pr[A] = ∑e∈APr[e]

Conditional probability: if A and B are events: Pr[B�A] = Pr[A �B]

Pr[A] = ∑e∈A∩BPr[e]

e∈APr[e]

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 8 / 16

I

(34)

Probability Basics II

Want to define probabilities. Should use measure theory. Won’t.

For each e∈ ⌦ let Pr[e] be probability of e (probability distribution)

Pr[e] ≥ 0 for all e ∈ ⌦, and ∑e∈⌦Pr[e] = 1

Probability of an event A is Pr[A] = ∑e∈APr[e]

Conditional probability: if A and B are events:

Pr[B�A] = Pr[A �B]

Pr[A] = ∑e∈A∩BPr[e]

e∈APr[e]

Do

(35)

Probability Basics III: Expectations

Expectation of a random variable:

E[X] = �

e∈⌦X(e)Pr[e]

“Average” of the random variable according to probability distribution

Can be useful to rearrange terms to get di↵erent equation: E[X] = �

e∈⌦X(e)Pr[e] = �

y∈R

e∈⌦∶X(e)=yy⋅ Pr[e] = �

y∈Ry⋅ Pr[X = y] Conditional Expectation: A an event, X a random variable.

E[X�A] = 1

Pr[A] �e∈AX(e)Pr[e]

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 9 / 16

(36)

Probability Basics III: Expectations

Expectation of a random variable:

E[X] = �

e∈⌦X(e)Pr[e]

“Average” of the random variable according to probability distribution Can be useful to rearrange terms to get di↵erent equation:

E[X] = �

e∈⌦X(e)Pr[e] = �

y∈R

e∈⌦∶X(e)=y

y⋅ Pr[e] = �

y∈Ry⋅ Pr[X = y]

Conditional Expectation: A an event, X a random variable. E[X�A] = 1

Pr[A] �e∈AX(e)Pr[e]

(37)

Probability Basics III: Expectations

Expectation of a random variable:

E[X] = �

e∈⌦X(e)Pr[e]

“Average” of the random variable according to probability distribution Can be useful to rearrange terms to get di↵erent equation:

E[X] = �

e∈⌦X(e)Pr[e] = �

y∈R

e∈⌦∶X(e)=y

y⋅ Pr[e] = �

y∈Ry⋅ Pr[X = y]

Conditional Expectation: A an event, X a random variable.

E[X�A] = 1

Pr[A] �e∈AX(e)Pr[e]

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 9 / 16

(38)

Linearity of Expectations

Amazing feature of expectations: linearity!

Theorem

For any two random variables X and Y, and any constants ↵ and : E[↵X + Y] = ↵E[X] + E[Y]

Consider rolling two dice. Let X be sum. What is E[X]?

E[X] = ∑e∈⌦X(e)Pr[e]. 36 term sum!

E[X] = ∑y∈Ry⋅ Pr[X = y]. What is Pr[X = 2], Pr[X = 3], . . . ? Instead: X = X1+ X2. So E[X] = E[X1+ X2] = E[X1] + E[X2]

E[X1] = E[X2] = 6

y=1

1

6y = 21

6 = 3.5

�⇒ E[X] = 3.5 + 3.5 = 7

(39)

Linearity of Expectations

Amazing feature of expectations: linearity!

Theorem

For any two random variables X and Y, and any constants ↵ and : E[↵X + Y] = ↵E[X] + E[Y]

Consider rolling two dice. Let X be sum. What is E[X]?

E[X] = ∑e∈⌦X(e)Pr[e]. 36 term sum!

E[X] = ∑y∈Ry⋅ Pr[X = y]. What is Pr[X = 2], Pr[X = 3], . . . ?

Instead: X = X1+ X2. So E[X] = E[X1+ X2] = E[X1] + E[X2] E[X1] = E[X2] = 6

y=1

1

6y = 21

6 = 3.5

�⇒ E[X] = 3.5 + 3.5 = 7

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 10 / 16

(40)

Linearity of Expectations

Amazing feature of expectations: linearity!

Theorem

For any two random variables X and Y, and any constants ↵ and : E[↵X + Y] = ↵E[X] + E[Y]

Consider rolling two dice. Let X be sum. What is E[X]?

E[X] = ∑e∈⌦X(e)Pr[e]. 36 term sum!

E[X] = ∑y∈Ry⋅ Pr[X = y]. What is Pr[X = 2], Pr[X = 3], . . . ? Instead: X= X1+ X2. So E[X] = E[X1+ X2] = E[X1] + E[X2]

E[X1] = E[X2] = 6

y=1

1

6y = 21

6 = 3.5

�⇒ E[X] = 3.5 + 3.5 = 7

Xcel

X e

face Veer

(41)

Linearity of Expectations

Amazing feature of expectations: linearity!

Theorem

For any two random variables X and Y, and any constants ↵ and : E[↵X + Y] = ↵E[X] + E[Y]

Consider rolling two dice. Let X be sum. What is E[X]?

E[X] = ∑e∈⌦X(e)Pr[e]. 36 term sum!

E[X] = ∑y∈Ry⋅ Pr[X = y]. What is Pr[X = 2], Pr[X = 3], . . . ? Instead: X= X1+ X2. So E[X] = E[X1+ X2] = E[X1] + E[X2]

E[X1] = E[X2] = 6

y=1

1

6y = 21

6 = 3.5

�⇒ E[X] = 3.5 + 3.5 = 7

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 10 / 16

(42)

Linearity of Expectations

Amazing feature of expectations: linearity!

Theorem

For any two random variables X and Y, and any constants ↵ and : E[↵X + Y] = ↵E[X] + E[Y]

Consider rolling two dice. Let X be sum. What is E[X]?

E[X] = ∑e∈⌦X(e)Pr[e]. 36 term sum!

E[X] = ∑y∈Ry⋅ Pr[X = y]. What is Pr[X = 2], Pr[X = 3], . . . ? Instead: X= X1+ X2. So E[X] = E[X1+ X2] = E[X1] + E[X2]

E[X1] = E[X2] = 6

y=1

1

6y = 21

6 = 3.5

(43)

Linearity of Expectations: Proof

Theorem

For any two random variables X and Y, and any constants ↵ and : E[↵X + Y] = ↵E[X] + E[Y]

Proof.

E[↵X + Y] = �

e∈⌦Pr[e] (↵X(e) + Y(e))

= ↵ �

e∈⌦Pr[e]X(e) + �

e∈⌦Pr[e]X(e)

= ↵E[X] + E[Y]

Holds no matter how correlated X and Y are!

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 11 / 16

(44)

Linearity of Expectations: Proof

Theorem

For any two random variables X and Y, and any constants ↵ and : E[↵X + Y] = ↵E[X] + E[Y]

Proof.

E[↵X + Y] = �

e∈⌦Pr[e] (↵X(e) + Y(e))

= ↵ �

e∈⌦Pr[e]X(e) + �

e∈⌦Pr[e]X(e)

= ↵E[X] + E[Y]

Holds no matter how correlated X and Y are!

(45)

Linearity of Expectations: Proof

Theorem

For any two random variables X and Y, and any constants ↵ and : E[↵X + Y] = ↵E[X] + E[Y]

Proof.

E[↵X + Y] = �

e∈⌦Pr[e] (↵X(e) + Y(e))

= ↵ �

e∈⌦Pr[e]X(e) + �

e∈⌦Pr[e]X(e)

= ↵E[X] + E[Y]

Holds no matter how correlated X and Y are!

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 11 / 16

(46)

Linearity of Expectations: Proof

Theorem

For any two random variables X and Y, and any constants ↵ and : E[↵X + Y] = ↵E[X] + E[Y]

Proof.

E[↵X + Y] = �

e∈⌦Pr[e] (↵X(e) + Y(e))

= ↵ �

e∈⌦Pr[e]X(e) + �

e∈⌦Pr[e]X(e)

= ↵E[X] + E[Y]

Holds no matter how correlated X and Y are!

(47)

Randomized Quicksort I

Theorem

The expected running time of randomized quicksort is at most O(n log n).

Assume for simplicity all elements distinct. Running time = ⇥(# of comparisons) Definitions:

X = # of comparisons (random variable)

ei = i’th smallest element (for i ∈ {1, . . . , n})

Xij random variable for all i, j∈ {1, . . . , n} with i < j:

Xij =���

����

1 if algorithm compares ei and ej at any point in time 0 otherwise

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 12 / 16

(48)

Randomized Quicksort I

Theorem

The expected running time of randomized quicksort is at most O(n log n).

Assume for simplicity all elements distinct. Running time = ⇥(# of comparisons)

Definitions:

X = # of comparisons (random variable)

ei = i’th smallest element (for i ∈ {1, . . . , n})

Xij random variable for all i, j∈ {1, . . . , n} with i < j:

Xij =���

����

1 if algorithm compares ei and ej at any point in time 0 otherwise

(49)

Randomized Quicksort I

Theorem

The expected running time of randomized quicksort is at most O(n log n).

Assume for simplicity all elements distinct. Running time = ⇥(# of comparisons) Definitions:

X = # of comparisons (random variable)

ei = i’th smallest element (for i ∈ {1, . . . , n})

Xij random variable for all i, j ∈ {1, . . . , n} with i < j:

Xij =���

����

1 if algorithm compares ei and ej at any point in time 0 otherwise

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 12 / 16

(50)

Randomized Quicksort II

Algorithm never compares the same two elements more than once �⇒ X = ∑n−1i=1 nj=i+1Xij

E[X] = E����

��

n−1

i=1

n j=i+1Xij����

��=n−1

i=1

n

j=i+1E[Xij] So just need to understand E[Xij]

Simple cases:

j = i + 1: Xij = 1 no matter what, so E[Xij] = 1

i= 1, j = n: e1 and en compared if and only if first pivot chosen is e1 or en

�⇒ E[X1n] = 2n

(51)

Randomized Quicksort II

Algorithm never compares the same two elements more than once �⇒ X = ∑n−1i=1 nj=i+1Xij

E[X] = E����

��

n−1

i=1

n j=i+1Xij����

��=n−1

i=1

n

j=i+1E[Xij]

So just need to understand E[Xij] Simple cases:

j = i + 1: Xij = 1 no matter what, so E[Xij] = 1

i= 1, j = n: e1 and en compared if and only if first pivot chosen is e1 or en

�⇒ E[X1n] = 2n

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 13 / 16

(52)

Randomized Quicksort II

Algorithm never compares the same two elements more than once �⇒ X = ∑n−1i=1 nj=i+1Xij

E[X] = E����

��

n−1

i=1

n j=i+1Xij����

��=n−1

i=1

n

j=i+1E[Xij] So just need to understand E[Xij]

Simple cases:

j = i + 1: Xij = 1 no matter what, so E[Xij] = 1

i= 1, j = n: e1 and en compared if and only if first pivot chosen is e1 or en

�⇒ E[X1n] = 2n

(53)

Randomized Quicksort II

Algorithm never compares the same two elements more than once �⇒ X = ∑n−1i=1 nj=i+1Xij

E[X] = E����

��

n−1

i=1

n j=i+1Xij����

��=n−1

i=1

n

j=i+1E[Xij] So just need to understand E[Xij]

Simple cases:

j = i + 1: Xij = 1 no matter what, so E[Xij] = 1

i= 1, j = n: e1 and en compared if and only if first pivot chosen is e1 or en

�⇒ E[X1n] = 2n

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 13 / 16

(54)

Randomized Quicksort II

Algorithm never compares the same two elements more than once �⇒ X = ∑n−1i=1 nj=i+1Xij

E[X] = E����

��

n−1

i=1

n j=i+1Xij����

��=n−1

i=1

n

j=i+1E[Xij] So just need to understand E[Xij]

Simple cases:

j = i + 1:

Xij = 1 no matter what, so E[Xij] = 1

i= 1, j = n: e1 and en compared if and only if first pivot chosen is e1 or en

�⇒ E[X1n] = 2n

Fft A

Fifita

(55)

Randomized Quicksort II

Algorithm never compares the same two elements more than once �⇒ X = ∑n−1i=1 nj=i+1Xij

E[X] = E����

��

n−1

i=1

n j=i+1Xij����

��=n−1

i=1

n

j=i+1E[Xij] So just need to understand E[Xij]

Simple cases:

j = i + 1: Xij = 1 no matter what, so E[Xij] = 1

i= 1, j = n: e1 and en compared if and only if first pivot chosen is e1 or en

�⇒ E[X1n] = 2n

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 13 / 16

(56)

Randomized Quicksort II

Algorithm never compares the same two elements more than once �⇒ X = ∑n−1i=1 nj=i+1Xij

E[X] = E����

��

n−1

i=1

n j=i+1Xij����

��=n−1

i=1

n

j=i+1E[Xij] So just need to understand E[Xij]

Simple cases:

j = i + 1: Xij = 1 no matter what, so E[Xij] = 1

i= 1, j = n:

e1 and en compared if and only if first pivot chosen is e1 or en

�⇒ E[X1n] = 2n

(57)

Randomized Quicksort II

Algorithm never compares the same two elements more than once �⇒ X = ∑n−1i=1 nj=i+1Xij

E[X] = E����

��

n−1

i=1

n j=i+1Xij����

��=n−1

i=1

n

j=i+1E[Xij] So just need to understand E[Xij]

Simple cases:

j = i + 1: Xij = 1 no matter what, so E[Xij] = 1

i= 1, j = n: e1 and en compared if and only if first pivot chosen is e1 or en

�⇒ E[X1n] = 2n

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 13 / 16

I

1 In

O

CI Ey

(58)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej:

Xij = 1 If ei < p < ej: Xij = 0

If p< ei or p> ej: ? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej: E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]: still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

Éte

sorted

A

tisfiffeiore

chosen

as

pivot before any ee i Kc

(59)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej: Xij = 1

If ei < p < ej: Xij = 0

If p< ei or p> ej: ? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej: E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]: still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 14 / 16

(60)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej: Xij = 1 If ei < p < ej:

Xij = 0

If p< ei or p> ej: ? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej: E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]: still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

(61)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej: Xij = 1 If ei < p < ej: Xij = 0

If p< ei or p> ej: ? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej: E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]: still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 14 / 16

(62)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej: Xij = 1 If ei < p < ej: Xij = 0 If p< ei or p> ej:

? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej: E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]: still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

(63)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej: Xij = 1 If ei < p < ej: Xij = 0

If p< ei or p> ej: ? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej: E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]: still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 14 / 16

(64)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej: Xij = 1 If ei < p < ej: Xij = 0

If p< ei or p> ej: ? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej:

E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]: still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

(65)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej: Xij = 1 If ei < p < ej: Xij = 0

If p< ei or p> ej: ? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej: E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]: still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 14 / 16

(66)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej: Xij = 1 If ei < p < ej: Xij = 0

If p< ei or p> ej: ? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej: E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]:

still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

(67)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej: Xij = 1 If ei < p < ej: Xij = 0

If p< ei or p> ej: ? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej: E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]: still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 14 / 16

(68)

E [X

ij

]: General Case (i < j)

If p= ei or p= ej: Xij = 1 If ei < p < ej: Xij = 0

If p< ei or p> ej: ? Both ei, ej in same recursive call.

Condition on ei ≤ p ≤ ej: E[Xij � ei ≤ p ≤ ej] = j−i+12

Condition on p �∈ [ei, ej]: still undetermined

So Xij not determined until ei ≤ p ≤ ej, and when it is determined has E[Xij] = j−i+12

�⇒ E[Xij] = j−i+12

(69)

E [X

ij

]: General Case (formally)

Let Yk be event that the k’th pivot is in [ei, ej] and all previous pivots not in [ei, ej]

�⇒ by definition, the Yk events are disjoint and partition sample space Showed that E[Xij�Yk] = j−i+12 for all k.

E[Xij] = n

k=1E[Xij�Yk]Pr[Yk] (Yk disjoint and partition ⌦)

= 2

j− i + 1

n

k=1Pr[Yk]

= 2

j− i + 1

Michael Dinitz Lecture 3: Probability, Randomized Quicksort September 7, 2021 15 / 16

References

Related documents

Cilj rada je postići razumijevanje međusobne povezanosti i uvjetovanosti čimbenika u procesu izgradnje sposobnosti oružanih snaga: veličine i strukture izdataka za

(Note: this is not very good, but in HW we will see that repeating the algorithm polynomially many times gives a good success probability. What would be the success chance if we

 Lecture 1: Introduction to finance (framework of financial analysis)  Lecture 2: Present value (principles of asset

Efficient parallel stencil convolution in Haskell, B Lippmeier &amp; G Keller, Haskell, 2011. Accelerating Haskell array codes with multicore GPUs, M Chakravarty

Special training includes, but is not limited to ENT Laser Safety Policy Development, LSO training, Physician ENT laser training &amp; preceptorship, anesthesia team ENT Laser

For each event of the sample space , we assume that a number is defined and satisfies the following

Download the presentation slides Lecture 1 Introduction to Financial Risk Management Lecture 2 Market Risk Lecture 3 Credit Risk Lecture 4 Counterparty Credit Risk and..

PPT/Lecture 3 Introduction to Algorithm &amp; complexity PPT/Lecture 4 Complexity analysis of algorithm PPT/Lecture 5 Character array &amp; String processing