Professional Documents
Culture Documents
the data
ontained in the Data Type will stay more or less the same between operations,
and
previous operations in
uen
e the
urrent state of the data stru
ture used by the Data
Type.
After all, it is the purpose of some operations to
hange the
ontent, and sometimes even
the internal organization, of the data stru
ture. In this
ontext, then, it is appropriate to
worry not so mu
h about the running time of a single operation as about the running time
of a sequen
e of operations, in whi
h an o
asional lengthy operation may be overshadowed
by long sequen
es of short operations. Let's
all the running time of an operation its a
tual
ost. In a worst
ase analysis of an algorithm the a
tual
ost of an operation is repla
ed by
its worst
ase
ost. In
ontrast
amortized analysis aims at providing an amortized
ost per operation in su
h a way that at
any time the total amortized
ost of the sequen
e of operations performed so far
is not less than the a
tual
ost in
urred.
In the following se
tions we will explore several methods for
omputing upper bounds
on the amortized
ost of an operation. I emphasized the words \upper bound" be
ause I
want you to be aware that that's all we're
omputing. So do not be surprised, or worried,
if dierent methods give you dierent bounds. That is the advantage of having dierent
methods at your disposal: you pi
k the one that gives you the best result - in this
ase the
lowest upper bound. I will illustrate the methods on an example, the pw-sta
k, whi
h may
seem to be arti
ial at rst glan
e, but in a
tuality appears, in various guises, in a number
of algorithms. Se
tion 3.5 presents one su
h guise.
Whereas the standard sta
k operations run in O(1) time, the pop-while operation
an
on
eivably pop the
ontents of the entire sta
k and has therefore a running time that is
proportional to the size of the sta
k (at that time), in the worst
ase.
Suppose now that the algorithm that employs the pw-sta
k exe
utes a sequen
e of n
operations in all,
onsisting of some mixture of the permissible operations. What then is the
total running time? A
rude analysis goes as follows: if the sequen
e
ontains O(n) pushes
the sta
k may
ontain as many as O(n) obje
ts; a single pop-while
ould therefore take as
long as O(n). If the sequen
e
ontains k pop-whiles it looks like the running time
ould be
as bad as O(kn), whi
h for k = O(n) is O(n2 )! In a
tuality, however, the running time is
mu
h less.
Theorem 3.2.1 Any sequen e of n operations on the pw-sta k runs in time O(n).
The following two se
tions give ea
h a dierent proof of this theorem. Sin
e the running
time is bounded by a linear fun
tion of the number of individual pushes and pops that are
performed, we
an, and will, simplify the analysis by assigning ea
h a
ost of one unit (a
pw-unit) and analyze the running time in terms of these abstra
t
osts.
1. The
harge-per-operation method, whi
h lists the
harge for ea
h operation separately,
mu
h as it is on your
redit
ard statement. Let's see how this works with the pw-sta
k.
Suppose there were k pushes ea
h
osting one pw-unit, and ` pops and pop-whiles of
whi
h the i-th operation popped ni items (ni = 1 for a straight pop), and therefore
P P
osted ni pw-units. Then the total of the re
orded
harges is k + `i=1 ni . But `i=1 ni
k be
ause no more items
an be popped o the sta
k then were pushed on. Thus the
total number of
harges does not ex
eed 2k. This proves Theorem 3.2.1 sin
e k n.
2. The distributed-
harge method, in whi
h the
harge for ea
h operation is distributed
among all, or some, of the elements that were ae
ted by the operation. At the end of
the sequen
e all
harges atta
hed to individual elements are
olle
ted. In the pw-sta
k
example,
Pp
suppose p items are in use, and that item number i is pushed mi times, with
i=1 mi = k . Then the
harges
olle
ted by that item do not ex
eed 2mi , be
ause the
item
annot P be popped more often than it is pushed. Hen
e the a
umulated
harges
are at most pi=1 2mi = 2k, the same bound as obtained by method 1.
18 CHAPTER 3. AMORTIZED ANALYSIS
p1
p5
p3
p7
p6
p4
p8
p2
p9
The algorithm proposed by R. Graham for solving this problem
omprises two stages:
I. Renumber the points so that
1. p1 is the point with least x-
oordinate (and in
ase of ties, with least y -
oordinate
p1 if and only if 3 is to the left of 1 2 , the dire
ted line from 1 to 2 . This
an be done as follows:
p p p p p
ompute
1
1 1
x y
= 1 2 2
x y
1
3 3
x y
Then = the area of the triangle 1 2 3, whereas () 0 if and only if 3 is to the left of the
j j p p p sign > p
Finger exer
ise: Prove that is invariant under translation and rotation. We may therefore assume,
without loss of generality, that 1 = 0 and 2 = ( 2 0); what is the value of () in this
ase?
p p x ; sign
Let's skip now to the se
ond stage, be
ause there appears what I'm really interested
in: a use of the pw-sta
k. Observe that if one \
onne
ts the dots" after stage I the
20 CHAPTER 3. AMORTIZED ANALYSIS
p9
p8
p1 p7
p6
p5
p3
p4
p2
Figure 3.5.2: Renumbering the points, and onne ting the dots
sequen
e < p1 ; : : : ; pn ; p1 > des
ribes a simple polygon; this polygon, however, is not
ne
essarily
onvex, see gure 3.5.2 (it is, however, a star-shaped polygon: one in whi
h
one spe
ial point, in this
ase the pivot,
an "see" the whole polygon). Whi
h is why
we need the next stage. Observe also that p1 ; p2 , and pn are guaranteed to be verti
es
of C (P ).
Here pt represents the vertex at the top of the sta
k, and pt 1 the one just below the
Copyright
2001 by Avraham. A. Melkman 21
top.
I will leave it to you to prove the
orre
tness of this s
an, i.e., to prove the invariant
(guidan
e: note that the angle of pi is greater than any that
ame before, so that it ne
essarily
is outside the
onvex hull assembled so far). My point here is that the pop-while
onstru
t
does appear in a
tual algorithms.
than O(log N ), any sequen
e of m operations runs in a total time bounded by m log n. Even
if the sequen
e was
leverly arranged by a endish enemy to be the most diÆ
ult to exe
ute.
It is therefore a kind of worst-
ase analysis - a worst sequen
e s
enario. In
ontrast, when
we say that the worst-
ase
ost of an operation is O(log N ) we mean that the running time
of ea
h and every operation is bounded by a
onstant times log n. Unless the algorithm that
employs these operations is real-time, an amortized
ost of O(log N ) is just as good as a
worst-
ase
ost of O(log N ), be
ause they guarantee the same overall performan
e.