11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

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.

328 Chap. 9 SearchingExample 9.9 Consider a table of size M = 101, with Perm[1] = 5,Perm[2] = 2, <strong>and</strong> Perm[3] = 32. Assume th<strong>at</strong> we have two keys k 1 <strong>and</strong>k 2 where h(k 1 ) = 30 <strong>and</strong> h(k 2 ) = 35. The probe sequence for k 1 is 30,then 35, then 32, then 62. The probe sequence for k 2 is 35, then 40, then37, then 67. Thus, while k 2 will probe to k 1 ’s home position as its secondchoice, the two keys’ probe sequences diverge immedi<strong>at</strong>ely thereafter.Another probe function th<strong>at</strong> elimin<strong>at</strong>es primary clustering is called quadr<strong>at</strong>icprobing. Here the probe function is some quadr<strong>at</strong>ic functionp(K, i) = c 1 i 2 + c 2 i + c 3for some choice of constants c 1 , c 2 , <strong>and</strong> c 3 . The simplest vari<strong>at</strong>ion is p(K, i) = i 2(i.e., c 1 = 1, c 2 = 0, <strong>and</strong> c 3 = 0. Then the ith value in the probe sequence wouldbe (h(K) + i 2 ) mod M. Under quadr<strong>at</strong>ic probing, two keys with different homepositions will have diverging probe sequences.Example 9.10 Given a hash table of size M = 101, assume for keys k 1<strong>and</strong> k 2 th<strong>at</strong> h(k 1 ) = 30 <strong>and</strong> h(k 2 ) = 29. The probe sequence for k 1 is 30,then 31, then 34, then 39. The probe sequence for k 2 is 29, then 30, then33, then 38. Thus, while k 2 will probe to k 1 ’s home position as its secondchoice, the two keys’ probe sequences diverge immedi<strong>at</strong>ely thereafter.Unfortun<strong>at</strong>ely, quadr<strong>at</strong>ic probing has the disadvantage th<strong>at</strong> typically not all hashtable slots will be on the probe sequence. Using p(K, i) = i 2 gives particularly inconsistentresults. For many hash table sizes, this probe function will cycle througha rel<strong>at</strong>ively small number of slots. If all slots on th<strong>at</strong> cycle happen to be full, thenthe record cannot be inserted <strong>at</strong> all! For example, if our hash table has three slots,then records th<strong>at</strong> hash to slot 0 can probe only to slots 0 <strong>and</strong> 1 (th<strong>at</strong> is, the probesequence will never visit slot 2 in the table). Thus, if slots 0 <strong>and</strong> 1 are full, thenthe record cannot be inserted even though the table is not full. A more realisticexample is a table with 105 slots. The probe sequence starting from any given slotwill only visit 23 other slots in the table. If all 24 of these slots should happen tobe full, even if other slots in the table are empty, then the record cannot be insertedbecause the probe sequence will continually hit only those same 24 slots.Fortun<strong>at</strong>ely, it is possible to get good results from quadr<strong>at</strong>ic probing <strong>at</strong> lowcost. The right combin<strong>at</strong>ion of probe function <strong>and</strong> table size will visit many slotsin the table. In particular, if the hash table size is a prime number <strong>and</strong> the probefunction is p(K, i) = i 2 , then <strong>at</strong> least half the slots in the table will be visited.Thus, if the table is less than half full, we can be certain th<strong>at</strong> a free slot will befound. Altern<strong>at</strong>ively, if the hash table size is a power of two <strong>and</strong> the probe function

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

Saved successfully!

Ooh no, something went wrong!