Presentation is loading. Please wait.

Presentation is loading. Please wait.

Double hashing Removal (open addressing) Chaining

Similar presentations


Presentation on theme: "Double hashing Removal (open addressing) Chaining"— Presentation transcript:

1 Double hashing Removal (open addressing) Chaining
Hash Tables Double hashing Removal (open addressing) Chaining November 29, 2017 Hassan Khosravi / Geoffrey Tien

2 Hassan Khosravi / Geoffrey Tien
Double hashing In both linear and quadratic probing the probe sequence is independent of the key Double hashing produces key dependent probe sequences In this scheme a second hash function, ℎ 2 , determines the probe sequence The second hash function must follow these guidelines ℎ 2 key ≠0 ℎ 2 ≠ ℎ 1 A typical ℎ 2 is 𝑝− key mod 𝑝 where 𝑝 is a prime number November 29, 2017 Hassan Khosravi / Geoffrey Tien

3 Double hashing example
Hash table is size 23 The hash function, ℎ=𝑥 mod 23, where x is the search key value The second hash function, ℎ 2 =5− key mod 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 29 32 58 21 November 29, 2017 Hassan Khosravi / Geoffrey Tien

4 Double hashing example
Insert 81, ℎ=81 mod 23=12 Which collides with 58 so use ℎ 2 to find the probe sequence value ℎ 2 =5− 81 𝑚𝑜𝑑 5 =4, so insert at = 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 29 32 58 81 21 November 29, 2017 Hassan Khosravi / Geoffrey Tien

5 Double hashing example
Insert 35, ℎ=35 mod 23=12 Which collides with 58 so use ℎ 2 to find a free space ℎ 2 =5− 35 mod 5 =5, so insert at = 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 29 32 58 81 35 21 November 29, 2017 Hassan Khosravi / Geoffrey Tien

6 Double hashing example
Insert 60, ℎ=60 mod 23=14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 29 32 58 60 81 35 21 November 29, 2017 Hassan Khosravi / Geoffrey Tien

7 Double hashing example
Insert 83, ℎ=83 mod 23=14 ℎ 2 =5− 83 mod 5 =2, so insert at = 16, which is occupied The second probe increments the insertion point by 2 again, so insert at = 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 29 32 58 60 81 35 83 21 November 29, 2017 Hassan Khosravi / Geoffrey Tien

8 Removals and open addressing
Removals add complexity to hash tables It is easy to find and remove a particular item But what happens when you want to search for some other item? The recently empty space may make a probe sequence terminate prematurely One solution is to mark a table location as either empty, occupied or removed (tombstone) Locations in the removed state can be re-used as items are inserted After confirming non-existence November 29, 2017 Hassan Khosravi / Geoffrey Tien

9 Tombstones and performance
After many removals, the table may become clogged with tombstones which must still be scanned as part of a cluster it may be beneficial to periodically re-hash all valid items Example with linear probing and ℎ 𝑥 =𝑥 mod 23 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 X 29 54 60 35 𝜆= 4 23 ~0.174 search(75) requires 15 probes! After rehashing: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 29 54 35 60 𝜆= 4 23 ~0.174 search(75) requires 2 probes November 29, 2017 Hassan Khosravi / Geoffrey Tien

10 Hassan Khosravi / Geoffrey Tien
Separate chaining Separate chaining takes a different approach to collisions Each entry in the hash table is a pointer to a linked list (or other dictionary-compatible data structure) If a collision occurs the new item is added to the end of the list at the appropriate location Performance degrades less rapidly using separate chaining with uniform random distribution, separate chaining maintains good performance even at high load factors 𝜆>1 November 29, 2017 Hassan Khosravi / Geoffrey Tien

11 Separate chaining example
ℎ 𝑥 =𝑥 mod 23 Insert 81, ℎ(𝑥)=12, add to back (or front) of list Insert 35, ℎ(𝑥)=12 Insert 60, ℎ(𝑥)=14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 29 32 58 60 21 81 November 29, 2017 Hassan Khosravi / Geoffrey Tien 35

12 Hassan Khosravi / Geoffrey Tien
Hash table discussion If 𝜆 is less than ½, open addressing and separate chaining give similar performance As 𝜆 increases, separate chaining performs better than open addressing However, separate chaining increases storage overhead for the linked list pointers It is important to note that in the worst case hash table performance can be poor That is, if the hash function does not evenly distribute data across the table November 29, 2017 Hassan Khosravi / Geoffrey Tien

13 Readings for this lesson
Thareja Chapter (Double hashing) Chapter (Chaining) Next class: We're done! November 29, 2017 Hassan Khosravi / Geoffrey Tien


Download ppt "Double hashing Removal (open addressing) Chaining"

Similar presentations


Ads by Google