Introduction to Algorithms
Lecture 9

2
Last Time
•
Dynamic programming
–
Matrix Chain Multiplication
–
Longest common subsequence
–
Optimal BST

3
Today’s Lecture
•
Amortized Analysis
•
Aggregate Method
•
Accounting Method
•
Potential Method
•
Binary Counter
•
Table Update

4
Amortized Analysis
Scenario:
Maintaining data structure over a sequenc
e of
n
operation.
Operation cost:
Cost per operation may be large (e.g.
(
n
)
).
Total cost:
But total cost may not be as much as
n
(worst-case cost of one operation).
Amortized analysis:
How to do tight analysis in such
scenarios?
Examples: Heapify vs. Build-Heap.

5
Three techniques
Today we will describe three techniques that
can be used to analyze such scenarios.
•
Aggregate method
•
Accounting method
•
Potential method
•
Motivate by the “Binary Counter” example.
•
Explain the differences.
•
Move to “Dynamic Table” problem.

6
Binary Counter
Data Structure:
k
-bit array, counting numbers from
0
to
2
k
– 1
.
Operation:
Increment.
Cost:
# of bits flipped.
(Toy problem! Motivates the ideas clearly!)
Example:
00000
00001
Cost =
1
00010
Cost =
2
00011
Cost =
1
00100
Cost
= 3

7
Aggregate method
Idea:
Count the total cost for
n
operations
directly.
Technique:
Ad-hoc. Possibly try to count from
a different way.

8
Example of Aggregate method
Cost =
1
Cost =
2
Cost =
1
Cost =
3
Cost =
1
Cost =
5
Cost
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
0
1
1
0
0
1
0
0
0
0
1
0
1
1
1
1
1
1
16
8
4
2
1

9
Aggregate method (cont.)
•
Count per row varies. Hard to sum up.
•
Instead count by columns.
•
i.e., count how many times
i
th bit is flipped.
•
Add the costs.
For a sequence of
n
increments:
i
th l.s.b. flipped
n
/2
i
times.

10
Aggregate method (cont.)
Total Cost
=
Thus total cost =
O
(
n
)
.
Amortized cost per insertion =
O
(
n
)/
n
=
O
(1)
.
n
i
i
n
2
log
0
2
/
n
n
n
i
i
n
i
i
2
2
/
1
2
/
0
log
0
2

11
Amortized cost
Definition:
Average cost of an operation over a
sequence of
n
operations, maximized over all n
and all sequences.
Warning:
•
Not average case analysis!
•
No assumptions about input sequence.
•
Amortization is still a worst-case principle.
•
In aggregate method, counted the total to bound
the amortized cost. Usually do this the other way.

12
Accounting method
•
Data structure comes with a “bank account”.
•
Every operation allotted a fixed
$
cost (its
amortized cost).
•
If actual cost less than allotted amount, deposit
extra
$
’s into bank.
•
If actual cost more than allotted amount, withdraw
from bank to pay for the operation.
•
Prove: Always have a non-negative balance.
•
Conclude: Sequence of
n
operations costs at most
n
times the amortized cost!

13
Example: Binary Counter
•
Amortized Cost of flipping
0
1 = 2$
.
•
Amortized Cost of flipping
1
0 = 0$
.

#### You've reached the end of your free preview.

Want to read all 61 pages?

- Fall '05
- RudolfFleischer