lec21 - CS 3110 Lecture 21 Amortized analysis and dynamic...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
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 real-time 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

lec21 - CS 3110 Lecture 21 Amortized analysis and dynamic...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online