You are on page 1of 4

# Computing Amortized Costs for Tables with

Matei Popovici

November 3, 2012

## 1 Tables with Expansion (only)

Let T be a dynamic table and S be a sequence of size n of operations of
type insert. Let Ti designate the state of T after operation i, elems(Ti )
and size(Ti ) designate the number of elements in T and the size of T after
operation i, respectively. We designate by T0 the state of T before any
operation in S is performed.
Once a table is full (elems(Ti ) = size(Ti )), a subsequent insertion trig-
gers the doubling of the table size.
We establish the amortized cost of an operation of type insert using
the potential method. Let:

## (Ti ) = 2 elems(Ti ) size(Ti ) for i S

(T0 ) = 0
It is easy to see that (Tn ) 0 for any sequence of n operations.

Case 1 (elems(Ti )/size(Ti ) < 1) i.e. operation i does not trigger an ex-
pansion. Therefore, size(Ti ) = size(Ti1 ) and elems(Ti ) = 1 + elems(Ti1 ).

ci = ci + (Ti ) (Ti1 )
= 1 + 2 elems(Ti ) size(Ti ) 2 elems(Ti1 ) + size(Ti1 )
= 1 + 2 + 2 elems(Ti1 ) size(Ti1 ) 2 elems(Ti1 ) + size(Ti1 )
= 3

## unde i desemneaza o operatie de tip insert.

1
Case 2 (elems(Ti1 )/size(Ti1 ) = 1) i.e. operation i does trigger an
expansion. Therefore, ci = elems(Ti1 ) + 1, size(Ti ) = 2 size(Ti1 ) and
elems(Ti ) = 1 + elems(Ti1 ).

ci = ci + (Ti ) (Ti1 )
= 1 + 2 elems(Ti ) size(Ti ) 2 elems(Ti1 ) + size(Ti1 )
= elems(Ti1 ) + 1 + 2 + 2 elems(Ti1 ) 2 size(Ti1 ) 2 elems(Ti1 ) + size(Ti1 )
= elems(Ti1 ) + 3 size(Ti1 )
= 3

## 2 Tables with Expansion and Contraction

Consider now a sequence S of n operations of types insert and remove,
and the following potential function:

2 elems(Ti ) size(Ti ) if elems(Ti )/size(Ti ) 1/2
(Ti ) = size(Ti )/2 elems(Ti ) if elems(Ti )/size(Ti ) < 1/2
0 if i = 0

## If a removal i makes elems(Ti ) = size(Ti )/4, then the table T is con-

tracted: size(Ti ) = size(Ti1 )/2.
The amortized cost ci where i is an operation of type insert follows the
same cases as above, when elems(Ti )/size(Ti ) 1/2.

## Case 3.a (elems(Ti )/size(Ti ) = 1/2). Furthermore elems(Ti ) = elems(Ti1 )+

1 and size(Ti ) = size(Ti1 )

ci = ci + (Ti ) (Ti1 )
= 1 + 2 elems(Ti ) size(Ti ) size(Ti1 )/2 + elems(Ti1 )
= 1 + size(Ti ) size(Ti ) size(Ti )/2 + elems(Ti ) 1
= 0 size(Ti )/2 + elems(Ti )
= 0

Case 4.a (elems(Ti )/size(Ti ) < 1/2) The same setting as in Case 3.a
holds.

ci = ci + (Ti ) (Ti1 )
= 1 + size(Ti )/2 elems(Ti ) size(Ti1 )/2 + elems(Ti1 )
= 1 elems(Ti ) + elems(Ti ) 1
= 0

2
Notice that, although in cases 3.a and 4.a the amortized cost for insert
is not equal to 3, it is however bounded-above by 3, therefore 3 is an appro-
priate value for ci = 3, for i of type insert.
The amortized cost ci where i is an operation of type remove follows the
cases:

## Case 1.b (elems(Ti1 )/size(Ti1 ) < 1/2). Moreover, we do not have

a contraction. We have elems(Ti ) = elems(Ti1 ) 1 and size(Ti ) =
size(Ti1 ).

ci = ci + (Ti ) (Ti1 )
= 1 + size(Ti )/2 elems(Ti ) size(Ti1 )/2 + elems(Ti1 )
= 1 elems(Ti ) + elems(Ti1 )
= 2

## Case 2.b (elems(Ti1 )/size(Ti1 ) < 1/2). Also, we have a contraction.

Thus elems(Ti ) = elems(Ti1 )1, size(Ti ) = size(Ti1 )/2 and elems(Ti ) =
size(Ti )/2.

ci = ci + (Ti ) (Ti1 )
= 1 + elems(Ti ) + size(Ti )/2 elems(Ti ) size(Ti1 )/2 + elems(Ti1 )
= 1 + elems(Ti ) + elems(Ti ) elems(Ti ) 2 elems(Ti ) + elems(Ti ) + 1
= 2

## Case 2.c (elems(Ti1 )/size(Ti1 ) = 1/2). The same conditions as in case

1.b hold.
ci = ci + (Ti ) (Ti1 )
= 1 + size(Ti )/2 elems(Ti ) 2 elems(Ti1 ) + size(Ti1 )
= 1 + elems(Ti1 ) elems(Ti1 ) + 1 2 elems(Ti1 ) + 2 elems(Ti1 )
= 2

Case 2.d (elems(Ti )/size(Ti ) > 1/2). Again, the same conditions as in
case 1.b hold.
ci = ci + (Ti ) (Ti1 )
= 1 + 2 elems(Ti ) size(Ti ) 2 elems(Ti1 ) + size(Ti1 )
= 1 + 2 elems(Ti1 ) 2 2 elems(Ti1 )
= 1

3
Notice that the amortized cost for remove is bounded above by 2, there-
fore, we can set ci = 2 when i is of type remove.