CS 3110 Lecture 21
Amortized analysis and dynamic tables
The claim that hash tables give have
O
(1) performance for lookup and insert is based on the assumption that
the number of elements stored in the table is comparable to the number of buckets. If a hash table has many
more elements than buckets, the number of elements stored at each bucket will become large. For instance,
with a constant number of buckets and
O
(n) elements, the lookup time is
O
(n) and not
O
(1).
The this problem is relatively simple: the array must be increased in size as the number of elements in the
hash table increases. However in doing so all the elements must be
rehashed
into the new buckets, thus
growing a hash table is not constant time, but rather takes time linear in the number of elements at the time
the table is grown. If we let the
load factor
be the ratio of the number of elements to the number of buckets,
generally when the load factor is more than some small constant, such as 2, the table is grown by a
multiplicative factor, e.g., doubled.
The linear running time of a resizing operation isn't as much of a problem as it might sound, though it can be
an issue for some realtime computing systems. If the bucket array is doubled in size every time it is needed,
then the insertion of
n
elements in a row into an empty array takes only
O
(
n
) time, perhaps surprisingly. We
say that add has
O
(1)
amortized run time
because the time required to insert an element is
O
(1) on the
average even though some elements trigger a lengthy rehashing of all the elements of the hash table.
Notice that it is crucial that the array size grows geometrically (doubling). It might be tempting to grow the
array by a fixed increment (e.g., 100 elements at time), but this results in asymptotic linear rather than
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 '07
 GIAMBATTISTA,A
 Analysis of algorithms, physicist

Click to edit the document details