Illinois Institute of Technology
Department of Computer Science
Lectures 13–14: March 9–11, 2009
CS 430 Introduction to Algorithms
Spring Semester, 2009
1
Amortized Analysis
So far, we have analyzed best and worst case running times for an operation without considering its context.
With amortized analysis, we study a sequence of operations rather than individual operations.
Consider the operation of a change machine that accepts dollar bills and gives out quarters. Every so often,
the machine will run out of quarters and require the services of a technician to reFll it – a process that could
require a week of waiting. Thus, at the worst case, you could have to wait a week before you could get
change; a worst-case analysis would then say that it might take you 10 weeks to get change for $10! This is
clearly an absurd analysis, however, since the change machine stores enough quarters to run for several weeks
without being reFlled. Thus, the technician would need to be called at most one time (if you are unlucky
enough) during the time you try to change your $10. This gives an amortized time of 1 week and a couple
of minutes for you to get your change, which is somewhat better than our Frst analysis. We can tolerate one
expensive operation (e.g. calling the technician), if the rest are guaranteed to be cheap thereafter.
±ormally speaking, the amortized time for an operation is the
worst-case average
behavior of an operation
over a sequence of operations. If, given
any
n
operations
o
1
, o
2
, o
3
, . . . o
n
we can bound their average cost:
1
n
X
i
cost(
o
i
)
≤
BOUND
then we can say that BOUND is an amortized cost of one operation. Typically, we will rewrite the above
equation:
X
i
cost(
o
i
)
≤
n
×
BOUND
and analyze its left-hand side.
There are three di²erent methods of amortized analysis: the
aggregate
method, the
accounting
method, and
the method of
potential functions
. Each of these are equivalent in power, in that any analysis we run with one
method can be reworked using another method. Nevertheless, we will tend to use the method of potential
functions the most because it will conceal much of the menial labour associated with amortized analysis.
1.1
Aggregate method
Richard P. ±eynman, a Noble-prize winning professor at Caltech, was once asked to explain the “±eynman
technique” for solving a problem. He responded that his method of solving a problem is to:
•
write down the problem
•
stare at the problem really hard
•
write down the solution to the problem