0% found this document useful (0 votes)
6 views11 pages

Amortized Analysis

Amortized cost analysis

Uploaded by

Shrishu Ranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Amortized Analysis

Amortized cost analysis

Uploaded by

Shrishu Ranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

AMORTIZED ANALYSIS

By

Prasanta K. Jana, IEEE Senior Member

Department of Computer Science and Engineering


Indian Institute of Technology (ISM), Dhanbad
E-mail: [email protected]
Purpose: In an amortized analysis, the time required to perform a sequence of
data structure operations is averaged over all the operations performed.

Why: Amortized analysis can be used to show that the average cost of an
operation is small, if one averages over a sequence of operations, even though a
single operation might be expensive.

How does it differ from average case analysis: It differs from average case
analysis in that probability is not involved.

What does amortization means: it means transforming cost. In an amortized


scheme, we charge some of the actual cost of an operation to other operations.
This reduces the charged cost of some operations and increases that of others.

Advantage: It guarantees the average performance of each operation in the worst


case.

Common techniques:
Aggregate method
Accounting method
Potential method
Aggregate Method
Here, we show that if a sequence of n operations takes worst case time T(n) in
total. In the worst case, the average cost or amortized cost, per operation is
therefore T(n) / n.

An Example (Stack Operations)

PUSH(S, x) requiring O(1) costs 1


POP(S) requiring O(1) costs 1
The total cost of a sequence of n PUSH and POP operations is therefore n.
Therefore, the actual running time of n operations is O(n).

Algorithm MULTIPOP(S, k)

1: while not STACK_EMPTY (S) and K≠ 0


2: POP(S)
3: k = k -1
4: end while

An Illustration:

Figure 1: (a). The top 4 objects are popped by MULTIPOP(S, 4), whose result is
shown in (b). The next operation is MULTIPOP(S, 7), which empties the stack
shown in (c) since there were fewer than 7 objects remaining.

The worst case time of MULTIPOP is O(n).


Therefore, a sequence of n PUSH, POP, and MULTIHOP costs O(n2).
Conclusion: This bound is not tight.
We obtain a better upper bound by amortized analysis.
How?

So, for any sequence of n PUSH, POP, and MULTIPOP operations takes a total
of O(n) time.
The average cost of an operation is O(n)/n = O(1).

Hence, Amortized cost of an operation is O(1).

Another Example: Incrementing a binary counter

Problem: Implementation of a k-bit binary counter that counts upward from 0.


It is to increment, i.e, to add 1 (mod 2k):

Algorithm INCREMENT(A)
1: i = 0
2: while i < length[A]) and A[i] == 1
3: A[i] = 0
4: i=i+1
5: end while
6: if i < length[A] then
7: A[i] = 1
8: end if
Aggregate Analysis: Consider the following figure:

n/2i
POTENTIAL METHOD
Illustration

Stack Operations:

Exercise 1: A sequence of n operations is performed on a data structure. The


ith operation costs i if i is an exact power of 2, and 1 otherwise. Use aggregate
analysis to determine the amortized cost per operation.
:

Exercise 2:
Ex. 17.2-2: A sequence of n operations is performed on a data structure. The ith
operation costs i if i is an exact power of 2, and 1 otherwise. Use Account method
to determine the amortized cost per operation.
Solution:

You might also like