Sie sind auf Seite 1von 10

Data Structures and Algorithms

(DSA – 02)

Case Studies

Amortization

Experimentation
Case Studies

Using big–Oh notation

to analyse algorithms

that solve the same problem

but have different running times.


Prefix–averages of sequence of numbers
Given an Array X of n numbers,
Compute an array A such that
A[i] is the average of elements X[0], …, X[i],
for i = 0, …, n–1.

A[i] = ∑ i j=0 X[j] / (i + 1)

Has many applications in economics and statistics.


Example:
Given the year–by–year returns of a mutual fund,
an investor sees the fund’s average annual return of
the last year,
the last three years,
the last five years,
and the last ten years

Prefix–averages of sequence of numbers
Also useful as a “smoothing” function
for a parameter that is quickly changing.

Values
Prefix average
Quadratic–Time Prefix Averages Algorithm
Computes every element of A separately.

Algorithm prefixAverages1 (X):


Input: n-element array X of numbers
Output: n-element array A of numbers such that
A[i] is the average of elements X[0], …, X[i].
Running Time
Let A be an array of n numbers O(n)

for i ← 0 to n – 1 do O(n)
a← 0
for j ← 0 to i do O(n2)
a ← a + X[j]
A[i] ← a / (i + 1)
return array A

Running Time = O(n) + O(n) + O(n2) = O(n2)


Linear–Time Prefix Averages Algorithm

Two consecutive averages A[i – 1] and A[i] are similar

A[i – 1] = (X[0] + X[1] + … + X[i – 1]) / i

A[i] = (X[0] + X[1] + … + X[i – 1] + X[i]) / (i + 1)

Si = X[0] + X[1] + … + X[i] Prefix Sum

∴ A[i] = Si / (i + 1)
Linear–Time Prefix Averages Algorithm

Algorithm prefixAverages2 (X):


Input: n-element array X of numbers
Output: n-element array A of numbers such that
A[i] is the average of elements X[0], …, X[i].
Running Time
Let A be an array of n numbers O(n)
s← 0 O(1)
for i ← 0 to n – 1 do O(n)
s ← s + X[i]
A[i] ← s / (i + 1)
return array A

Running Time = O(n) + O(1) + O(n) = O(n)


Amortization

In Finance
Systematic repayment (in monthly installments) of a debt.

In Accounting
Systematic writing off of some account
over a period of years.

Mathematical Analysis tool


used to characterise the running time of the algorithm.

Useful for understanding running times of algorithms


that have steps with widely varying performance.

Considers the interactions between all the operations


by studying running time of series of operations.
Amortization

Advantage
Does Average–Case analysis
without using any Probability.

Techniques
Accounting method (based on financial model)
Potential
function method (based on energy model)
Experimentation

Helps to perform Algorithm Analysis.

Steps in Experimentation
Deciding what to test.

Measuring actual running time of an algorithm.

Generating Test data.

Coding the solution.

Performing the experiment.

Das könnte Ihnen auch gefallen