Probabilistic Analysis in Algorithms: Average-Case Analysis and Randomization, Assignments of Algorithms and Programming

The concept of average-case analysis in algorithms, focusing on finding the maximum element in an array and randomizing an array. It introduces the idea of constructing a probabilistic model, determining probabilities and running times, and calculating expected running times. The document also explains how randomization can ensure the truth of the probabilistic model.

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-wy8-1
koofers-user-wy8-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 5: Probabilistic Analysis
Average-Case Analysis
Finding the Maximum Example
Randomizing an Array Example
Average-Case Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Example 1: Finding the Maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Example 1: Probablistic Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Example 1: Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Example 2: Random Permutation of an Array . . . . . . . . . . . . . . . . . . . . . . . 6
Example 2: Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1
Average-Case Analysis
In practice, many algorithms perform better than their worst-case.
The average case is analyzed by:
1. construct a probabilistic model of the input
2. determine the probabilities and running times (or costs) of alternate
executions
3. calculate expected running time (or cost)
Through randomization, one can often ensure that the probabilisitic model
is true.
CS 5633 Analysis of Algorithms Chapter 5: Slide 2
Example 1: Finding the Maximum
Maximum(A)
max A[1]
for i2to length(A)do
if max < A[i]then
max A[i]
return max
Problem: How many assignments to max?
Best-case: 1 (When does this happen?)
Worst-case: n(When does this happen?)
Average-case: (n+ 1)/2is incorrect
CS 5633 Analysis of Algorithms Chapter 5: Slide 3
Example 1: Probablistic Model
Assume Ahas ndistinct numbers. (What is the effect of duplicates?)
Assume each permutation of the numb ers is equally likely. (How can
randomization guarantee this?)
How many permutations are there?
What is the probability of the best case?
What is the probability of the worst case?
CS 5633 Analysis of Algorithms Chapter 5: Slide 4
2
pf2

Partial preview of the text

Download Probabilistic Analysis in Algorithms: Average-Case Analysis and Randomization and more Assignments Algorithms and Programming in PDF only on Docsity!

Chapter 5: Probabilistic Analysis

Average-Case AnalysisFinding the Maximum ExampleRandomizing an Array Example

Average-Case Analysis....................................... 2Example 1: Finding the Maximum............................... 3Example 1: Probablistic Model................................. 4Example 1: Analysis......................................... 5Example 2: Random Permutation of an Array....................... 6Example 2: Analysis......................................... 7

Average-Case Analysis ^

In practice, many algorithms perform better than their worst-case. ^

The

average case

is analyzed by:

1.^

construct a probabilistic model of the input

2.^

determine the probabilities and running times (or costs) of alternateexecutions

3.^

calculate expected running time (or cost)

^

Through

randomization

, one can often ensure that the probabilisitic model

is true. CS 5633 Analysis of Algorithms

Chapter 5: Slide – 2

Example 1: Finding the Maximum Maximum

(A

max

A

[1]

for

i^

to

length

(A

)^ do

if^ max

< A

[i]

then

max

A

[i]

return

max

^

Problem: How many assignments to

max

^

Best-case: 1 (When does this happen?) ^

Worst-case:

n

(When does this happen?)

^

Average-case:

(n

/^2

is incorrect

CS 5633 Analysis of Algorithms

Chapter 5: Slide – 3

Example 1: Probablistic Model ^

Assume

A

has

n

distinct numbers. (What is the effect of duplicates?)

^

Assume each permutation of the numbers is equally likely. (How canrandomization guarantee this?) ^

How many permutations are there?What is the probability of the best case?What is the probability of the worst case? CS 5633 Analysis of Algorithms

Chapter 5: Slide – 4

Example 1: Analysis ^

On iteration

i,

max

is assigned a value iff

A

[i]^

is the maximum of the first

i

numbers. ^

Probability that

A

[i]^

is the maximum of the first

i^

numbers =

1 /i

^

Probability of assignment =

1 /i

, cost = 1

Prob. of no assignment =

(i

/i, cost = 0

^

On iteration

i, the expected cost is:

/i)(1) + ((

i^ −

/i)(0) = 1

/i

^

Over the initial assignment and

n^

−^

1 iterations, the expected cost is:

n Σ i=

1 i^

which is between

ln

n^

and

1 + ln

n

CS 5633 Analysis of Algorithms

Chapter 5: Slide – 5

Example 2: Random Permutation of an Array Randomize-In-Place

(A

n^ ←

length

(A

for

i^

to

n

do

swap

A

[i]^

A

[Random

, i)]

^

Random

(a, b

)^ returns an integer

r,

a^

≤^

r^ ≤

b.

^

r^ is equally likely to be any integer between

a^

and

b

CS 5633 Analysis of Algorithms

Chapter 5: Slide – 6

Example 2: Analysis ^

Loop invariant: Before iteration

i,

A

[1]

through

A

[i^ −

1]

is a random

permutation of the first

i^ −

values.

^

For any permutation of the first

i^

values, there is exactly one way to

permute the first

i^

−^

1 values, and then swap

A

[i]^

into the correct position.

^

So after iteration

i, the first

i^

values have been randomly permuted, making

the loop invariant true the next iteration. CS 5633 Analysis of Algorithms

Chapter 5: Slide – 7