Presentation is loading. Please wait.

Presentation is loading. Please wait.

Resolving collisions: Open addressing

Similar presentations


Presentation on theme: "Resolving collisions: Open addressing"— Presentation transcript:

1 Resolving collisions: Open addressing
Store all elements within the table The space we save from the chain pointers is used instead to make the array larger. If there is a collision, probe the table in a systematic way to find an empty slot. If the table fills up, we need to enlarge it and rehash all the keys.

2 Open addressing: Linear Probing
hash function: (h(k) + i ) mod m for i=0, 1,...,m-1 Insert : Start with the location where the key hashed and do a sequential search for an empty slot. Search : Start with the location where the key hashed and do a sequential search until you either find the key (success) or find an empty slot (failure). Delete : (lazy deletion) follow same route but mark slot as DELETED rather than EMPTY, otherwise subsequent searches will fail.

3 Open addressing: Linear Probing
Advantage: very easy to implement Disadvantage: primary clustering Long sequences of used slots build up with gaps between them. Every insertion requires several probes and adds to the cluster. The average length of a probe sequence when inserting is

4 Open addressing: Quadratic Probing
Probe the table at slots ( h(k) + i2 ) mod m for i =0, 1,2, 3, ..., m-1 Ease of computation: Not as easy as linear probing. Do we really have to compute a power? Clustering Primary clustering is avoided, since the probes are not sequential. But...

5 Open addressing: Quadratic Probing
Probe sequence for hash value 3 in a table of size 16: = 3 = 4 = 7 = 12 = 3 = 12 = 7 = 4 = 3 = 4 = 7 = 12 = 3 = 12 = 7 = 4

6 Open addressing: Quadratic Probing
Probe sequence for hash value 3 in a table of size 19: Why did this happen? = 3 = 4 = 7 = 12 = 0 = 9 = 1 = 14 = 10 = 8 = 8 = 10 = 14 = 1 = 9 = 0 = 12 = 7 = 4 i2 is not a good idea after all. Use a small variant instead: h(k)+(-1)i-1 ((i+1)/1)2

7 Open addressing: Quadratic Probing
An element can always be inserted, All probes will be at different indices Prime table size  <= 0.5

8 Open addressing: Quadratic Probing
Disadvantage: secondary clustering: if h(k1)==h(k2) the probing sequences for k1 and k2 are exactly the same. Is this really bad? In practice, not so much It becomes an issue when the load factor is high.

9 Open addressing: Double hashing
The hash function is (h(k)+i h2(k)) mod m In English: use a second hash function to obtain the next slot. The probing sequence is: h(k), h(k)+h2(k), h(k)+2h2(k), h(k)+3h3(k), ... Performance : Much better than linear or quadratic probing. Does not suffer from clustering BUT requires computation of a second function

10 Open addressing: Double hashing
The choice of h2(k) is important It must never evaluate to zero consider h2(k)=k mod 9 for k=81 The choice of m is important If it is not prime, we may run out of alternate locations very fast. If m and h2(k) are relatively prime, we’ll end up probing the entire table. A good choice for h2 is h2(k) = p  (k mod p) where p is a prime less than m.

11 Open addressing: Random hashing
Use a pseudorandom number generator to obtain the sequence of slots that are probed.


Download ppt "Resolving collisions: Open addressing"

Similar presentations


Ads by Google