Presentation is loading. Please wait.

Presentation is loading. Please wait.

Collision Resolution: Open Addressing

Similar presentations


Presentation on theme: "Collision Resolution: Open Addressing"— 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 Al-Turki, Musab Ahmad Bakeer Al-Saegh, Radha Mahdi Al-Shahrani, Adel Saad Al-Awami, Louai Adnan Muhammad Al-Amer, Yousuf Jauwad Al-Helal, Husain Ali AbdulMohsen Then insert the following records quadratic probing to resolve collisions, if any. Al-Najjar, Khaled Ziyad Al-Ali, Amr Ali Zaid Al-Ramadi, Husam Yahya

4 Example 1: Quadratic Probing (cont'd)
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 Al-Turki, Musab Ahmad Bakeer Al-Saegh, Radha Mahdi Al-Shahrani, Adel Saad Al-Awami, Louai Adnan Muhammad Al-Amer, Yousuf Jauwad Al-Helal, Husain Ali AbdulMohsen Al-Najjar, Khaled Ziyad Al-Ali, Amr Ali Zaid Al-Ramadi, Husam Yahya 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
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) : 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 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: 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?


Download ppt "Collision Resolution: Open Addressing"

Similar presentations


Ads by Google