

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!


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
In practice, many algorithms perform better than their worst-case. ^
The
average case
is analyzed by:
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
max
for
i^
to
length
)^ do
if^ max
[i]
then
max
[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
is incorrect
CS 5633 Analysis of Algorithms
Chapter 5: Slide – 3
Assume
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
On iteration
i,
max
is assigned a value iff
[i]^
is the maximum of the first
i
numbers. ^
Probability that
[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
n^ ←
length
for
i^
to
n
do
swap
[i]^
[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
Loop invariant: Before iteration
i,
through
[i^ −
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
[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