1 / 15

Collision Resolution: Open Addressing

Collision Resolution: Open Addressing. Quadratic Probing Double Hashing Random Probing Informal Analysis of Hashing. Open Addressing: Quadratic Probing. Quadratic probing : Attempts to avoid cluster buildup. In this method, c(i) is a quadratic function in i : c(i) = a i 2 + b i + c

Télécharger la présentation

Collision Resolution: Open Addressing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Collision Resolution: Open Addressing • Quadratic Probing • Double Hashing • Random Probing • Informal Analysis of Hashing

  2. Open Addressing: Quadratic Probing • Quadratic probing: Attempts to avoid cluster buildup. • In this method, c(i) is a quadratic function in i: c(i) = a i2 + bi + c • Quadratic probing is usually done using c(i) = i2 . • We use the following slight modification to c (i) : c(i)=i2, c(i)= -i2, for i=1,2,…,(n-1)/2 • Thus the probe sequence is h(r)+i2, h(r)- i2, for i=1,2,…,(n-1)/2

  3. Example 2: Linear Probing • Use the hash function h(r) = r.id % 13 to load the following records into an array of size 13. Al-Otaibi Ziyad 1.73 985926 Al-Turki, Musab Ahmad Bakeer 1.60 970876 Al-Saegh, Radha Mahdi 1.58 980962 Al-Shahrani, Adel Saad 1.80 986074 Al-Awami, Louai Adnan Muhammad 1.73 970728 Al-Amer, Yousuf Jauwad 1.66 994593 Al-Helal, Husain Ali AbdulMohsen 1.70 996321 Then insert the following records quadratic probing to resolve collisions, if any. Al-Najjar, Khaled Ziyad 1.69 987615 Al-Ali, Amr Ali Zaid 1.79 987630 Al-Ramadi, Husam Yahya 1.58 987602

  4. Example 1: Quadratic Probing (cont'd) 0 1 2 3 4 5 6 7 8 9 10 11 12 Husain Yousuf Khalid Louai Ziyad Amr Radha Husam Musab Adel

  5. Quadratic Probing: Concluding Notes • In general, quadratic probing improves on linear probing but does not avoid cluster buildup. • Gives rise to secondary clusters which are less harmful than the primary clusters in linear probing. • Hash table size should not be an even number, therwise Property 2 will not be satisfied. • Ideally, table size should be a prime, 4j+3, for an integer j, which guarantees Property 2.

  6. Quadratic Probing: Concluding Notes (cont'd) • A disadvantage: colliding keys at a given address tread the same probe sequence. • This sequence of locations is called a secondary cluster. • Unlike primary clusters, secondary clusters cannot combine into larger secondary clusters. • To eliminate secondary clustering, synonyms must have different probe sequences. • Double hashing achieves this by having two hash functions that both depend on the hash key.

  7. Open Addressing: Double Hashing • Double hashing: Best addresses secondary clustering. • Uses two hash functions, h and hp, with the usual probe sequence: hi(r) = (h(r) + i*hp(r)) mod n • We see that c(i) = i*hp(r) satisfies Property 2 as well, provided hp(r) and n are relatively prime. • To guarantee Property 2, n must be a prime number.

  8. Open Addressing: Double Hashing (cont'd) • Using two hash functions can be expensive. • In practice, a common definition for hp is : hp(r)= 1 + (r mod (n-1)). • Thus, the probe sequence for r hashing to position j is: j, j+1*hp(r), j+2*hp(r), j+3*hp(r), … • Notice that if hp(r)=1, the probe sequence for r is the same as linear probing. • But if hp(r)=2, the probe sequence examines every other array location.

  9. Example 2: Illustrating Double Hashing • Use double hashing to load the following records into a hash table Al-Otaibi Ziyad 1.73 985926 Al-Turki, Musab Ahmad Bakeer 1.60 970876 Al-Saegh, Radha Mahdi 1.58 980962 Al-Shahrani, Adel Saad 1.80 986074 Al-Awami, Louai Adnan Muhammad 1.73 970728 Al-Amer, Yousuf Jauwad 1.66 994593 Al-Helal, Husain Ali AbdulMohsen 1.70 996321 Al-Najjar, Khaled Ziyad 1.69 987615 Al-Ali, Amr Ali Zaid 1.79 987630 Al-Ramadi, Husam Yahya 1.58 987602 Use the following pair of hash functions h(r) = r.id % 13 hp(r) = 1 + (r.id % (n-1)).

  10. Example 2: Animating Double Hashing 0 1 2 3 4 5 6 7 8 9 10 11 12 Husain Yousuf Husam Louai Ziyad Amr Radha Khalid Musab Adel

  11. Open Addressing: Random Probing • Random probing: Uses a pseudo-random function to "jump around" in the hash table. • Here, c(i) is defined so that it always produces a 'random‘ integer in the range 0, 1, ..., n - 1. • The function c(i) can be defined recursively as follows c(0) = 0 c(i+1) = (a*c(i) + 1) mod n • We choose a to insure Property 2, too. • This recursive definition of c() is a permutation of 0, 1, ..., n - 1 iff a - 1 is a multiple of every prime divisor of n, where 4 is considered as prime.

  12. Example 3: Random Probing • Let n = 16, a = 3. We get the sequence for c(i) : 0 1 4 13 8 9 12 5 0 1 4 13 8 9 12 5 0 ... which is not a permutation of 0,1,2, …, 15. • The prime divisors of 16 are 2 and 4, so (a-1) must be a multiple of 4. • If we choose a = 5, then, we get the sequence 0 1 6 15 12 13 2 11 8 9 14 7 4 5 10 3 0 1 6 ... which is a permutation of 0, 1, 2, …, 15. • We could also select a to be 9, 13, 17, etc.

  13. Random Probing: Concluding Remarks • For our students records example, n = 13 which has itself as its only prime divisor. • We can therefore select a = 14. The probe sequence turns out to be: 0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 ... which is the same as in linear probing. • Note that the name "random probing" is exaggerative. • Random probing eliminates the problem of secondary clusters.

  14. Hashing: Informal Analysis • In the best case, successful hash search requires one probe. • In the worst case, hash search becomes a sequential search. • For the average case, the number of probes depends on the load factor. • The load factor in open addressing is less than 1 while it can be greater than 1 in chaining. • Note that some methods are worse than others at high load factors. • Performance of open addressing methods about the same at low load factors.

  15. Exercises 1. If a hash table is 25% full what is its load factor? 2. Given that, c(i) = i2, for c(i) in quadratic probing, we discussed that this equation does not satisfy Property 2, in general. What cells are missed by this probing formula for a hash table of size 17? Characterise using a formula, if possible, the cells that are not examined by using this function for a hash table of size n. 3. It was mentioned in this session that secondary clusters are less harmful than primary clusters because the former cannot combine to form larger secondary clusters. Use an appropriate hash table of records to exemplify this situation. 4.What value would you select for a given a hash table of size 100?

More Related