Cuckoo Hashing Flashcards
Cuckoo Hashing
Another way to resolve collisions
Idea
The cuckoo bird
o Shares a nest with other species o … then kicks the other species out
§ Same idea with cuckoo hashing
§ When we insert k1 we kick out what occupies the bucket, let’s say k2
§Then, k2 finds a new bucket
Implementation
§ We have two hash tables T1 and T2
§ Collision strategy
o Each hash table has a hash function h1 and h2
o We prefer T1, but if we have to move we’ll go to T2
o If we are in T2 and have to move, we’ll go to T1
Why two tables?
o Simple to visualize and implement
-Two hash functions h1 and h2 (one per table)
§ One table works just as well
-Using two hash functions h1 and h2
Cuckoo Hashing running time
§ Cuckoo hashing guarantees o O(1) search, O(1) insert
§ Remember: other hashing strategies can’t guarantees this!
Previous Collision Strategies
§Open addressing:
o Advantages: Simplicity and fixed memory usage
o Disadvantages:
-Deletions are problematic. Hence, chaining is more common when keys have to be deleted
§ Chaining:
o Advantages: Simplicity and fixed memory usage
-Simpler insertion and removal
o Disadvantages: Memory overhead is large if entries are small