Double hashing in C

Hash tables are data structures that store key-value pairs and offer quick insertion, retrieval, and deletion. Double hashing is a collision resolution method used in hash tables. It offers a different approach to handling collisions than linear probing or chaining.

  • Hash Tables: A hash function is used to map keys to array indices in hash tables, which are arrays. Its main objective is to minimize collisions by distributing keys equally over the array.
  • Resolution of Collisions: Collisions happen when two or more keys hash to the same index in an array. Collision resolution techniques are used to deal with these circumstances.
  • Open Addressing: A sort of collision resolution method for open addressing is double hashing. If there is a collision, open addressing instructs you to insert the key-value pair in the next space in the array.
  • Two Hash Functions: The two hash functions hashFunction1 and hashFunction2 are necessary for double hashing. The primary hash function, hashFunction1 should determine the key's initial index. In the event of collisions, the step size for probing the array is decided by the secondary hash function, hashFunction2.
  • Double hashing with Insertion:
    • Initially, you use hashFunction1 to get the initial index when inserting a key-value pair.
    • HashFunction2 is used to determine the step size and probe the next slot if the initial index is filled (causing a collision).
    • You keep probing slots until one of them is empty, and then you put the key-value pair in there.
  • Search with Double Hashing:
    • HashFunction1 is used to generate the starting index while looking for a key.
    • The next slot is probed using hashFunction2 to calculate the step size if the key cannot be found at the first index.
    • When you hit an empty slot (which indicates the key is not in the table), you stop exploring slots and move on.

Example:

Here is an example of a C program that uses double hashing:

Output:

Value for key 10: 42
Value for key 20: 64

Explanation:

  1. In this example, we construct two structures: a HashTable structure and a HashTableEntry structure to represent key-value pairs and the hash table itself.
  2. The terms "hashFunction1" and "hashFunction2" refer to two hash functions.
  3. Using double hashing to avoid collisions, the insert function enters key-value pairs into the hash table.
  4. The key-value pair (10, 42) is inserted into the hash table with the syntax insert(&ht, 10, 42).
  5. The key-value pair (20, 64) is inserted into the hash table using the syntax insert(&ht, 20). Due to the hash functions and double hashing, it should be noted that these two keys hash to distinct locations in the table.
  6. The search function search(&ht, 10); looks for the value related to the key 10. Because 10 hashes to the same place as it was placed, it finds the value 42.
  7. The search function search(&ht, 20); looks for the value related to the key 20. Because 20 hashes to a different location than 10 does, it discovers the value 64.
  8. The search function uses similar double-hashing techniques to find a value based on a key.

The double hashing collision resolution technique uses two hash algorithms and open addressing to handle collisions in hash tables. When properly implemented and the right hash functions are selected, it provides advantages like even key distribution and memory efficiency.