23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

only the one entry. We leave the details of this f<strong>in</strong>al space optimization to an<br />

exercise (C-9.5). In Figure 9.4, we give an illustration of a hash table with<br />

separate cha<strong>in</strong><strong>in</strong>g.<br />

Assum<strong>in</strong>g we use a good hash function to <strong>in</strong>dex the n entries of our map <strong>in</strong> a<br />

bucket array of capacity N, we expect each bucket to be of size n/N. This value,<br />

called the load factor of the hash table (<strong>and</strong> denoted with δ), should be bounded<br />

by a small constant, preferably below 1. For, given a good hash function, the<br />

expected runn<strong>in</strong>g time of operations get, put, <strong>and</strong> remove <strong>in</strong> a map implemented<br />

with a hash table that uses this function is O( n/N). Thus, we can implement<br />

these operations to run <strong>in</strong> O(1) expected time, provided n is O(N).<br />

Figure 9.4: A hash table of size 13, stor<strong>in</strong>g 10<br />

entries with <strong>in</strong>teger keys, with colli sions resolved by<br />

separate cha<strong>in</strong><strong>in</strong>g. The compression function is h(k) =<br />

k mod 13. For simplicity, we do not show the values<br />

associated with the keys.<br />

Open Address<strong>in</strong>g<br />

The separate cha<strong>in</strong><strong>in</strong>g rule has many nice properties, such as allow<strong>in</strong>g for simple<br />

implementations of map operations, but it nevertheless has one slight disadvan<br />

tage: it requires the use of an auxiliary data structure—a list—to hold entries with<br />

collid<strong>in</strong>g keys. We can h<strong>and</strong>le collisions <strong>in</strong> other ways besides us<strong>in</strong>g the separate<br />

533

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!