12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

342 Chap. 9 Searching09050090501100111001223344556679877798778203782037991059(a)(b)Figure 9.6 Example of problems with linear probing. (a) Four values are insertedin the order 1001, 9050, 9877, <strong>and</strong> 2037 using hash function h(K) = K mod 10.(b) The value 1059 is added <strong>to</strong> the hash table.in slot 2 with probability 6/10. This is illustrated by Figure 9.6(b). This tendencyof linear probing <strong>to</strong> cluster items <strong>to</strong>gether is known as primary clustering. Smallclusters tend <strong>to</strong> merge in<strong>to</strong> big clusters, making the problem worse. The objection<strong>to</strong> primary clustering is that it leads <strong>to</strong> long probe sequences.Improved Collision Resolution MethodsHow can we avoid primary clustering? One possible improvement might be <strong>to</strong> uselinear probing, but <strong>to</strong> skip slots by a constant c other than 1. This would make theprobe functionp(K, i) = ci,<strong>and</strong> so the ith slot in the probe sequence will be (h(K) + ic) mod M. In this way,records with adjacent home positions will not follow the same probe sequence. Forexample, if we were <strong>to</strong> skip by twos, then our offsets from the home slot wouldbe 2, then 4, then 6, <strong>and</strong> so on.One quality of a good probe sequence is that it will cycle through all slots inthe hash table before returning <strong>to</strong> the home position. Clearly linear probing (which“skips” slots by one each time) does this. Unfortunately, not all values for c willmake this happen. For example, if c = 2 <strong>and</strong> the table contains an even numberof slots, then any key whose home position is in an even slot will have a probe

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

Saved successfully!

Ooh no, something went wrong!