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 sequence
of
n
operation
o
1
,
o
2
, …,
o
n
.
Operation cost:
Cost per operation may be large (e.g.
(
n
)
), worst-case cost =
max
c
(
o
i
)
.
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? (Total cost/
n
)
Examples
: Heapify vs. Build-Heap.
1
n
i
i
c

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.

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

Want to read all 32 pages?

- Fall '05
- RudolfFleischer