Double Hashing in Java

In programming, while we deal with data structure sometimes, we required to store two objects having the same hash value. Storing two objects having the same hash value is not possible. To overcome this problem, data structure provides collision resolution technique. In this section, we will focus only on double hashing, its advantages, example, and formula.

Data structure provides the following collision resolution techniques:

  1. Separate Chaining (also known as Open Hashing)
  2. Open Addressing (also known as Closed Hashing)
  • Linear Probing
  • Quadratic Probing
  • Double Hashing

What is double hashing?

It is a collision resolution technique in open addressing hash table that is used to avoid collisions. A collision occurs when two keys are hashed to the same index in a hash table. The reason for occurring collision is that every slot in hash table is supposed to store a single element.

Generally, hashing technique consists a hash function that takes a key and produces hash table index for that key. The double hashing technique uses two hash functions so it is called double hashing. The second hash function provides an offset value if the first hash function produces a collision. In other words, we can say that when two different objects have the same hash, is called collision.

Hash Function

A hash function is a function that accepts a group of characters (key) and maps that key to a value of a certain length (called a hash value or hash). The process is called hashing. It is done for indexing and locating items in databases. It provides an easy way to find longer value associated with the shorter hash value. It is widely used in encryption. It is also known as a hashing algorithm or message digest function.

Double Hash Function

The first hash function determines the initial location to located the key and the second hash function is to determine the size of the jumps in the probe sequence. The following function is an example of double hashing:

Or

In the above function, the value of i will keep incrementing until an empty slot is found.

If the table size is prime, the double hashing works well.

Where PRIME is a prime smaller than tableSize.

If the above functions compute an offset value that is already occupied by other object, it means there is a collision. A good hash function must have the following properties:

  • Quick to evaluate.
  • Differ from the original hash function.
  • Never evaluates to 0.

Advantages of Double Hashing

  • The technique does not yield any clusters.
  • It is the best form of probing because it can find next free slot in hash table more quickly than linear probing.
  • It produces a uniform distribution of records throughout a hash table.

Double Hashing Example

Suppose, we have a hash table of size 11. We want to insert keys 20, 34, 45, 70, 56 in the hash table. Let's insert the keys into hash table using the following double hash functions:

h1(k) = k mod 11         (first hash function)

h2(k) = 8 - (k mod 8)         (second hash function)

first, we will create a hash table of size 11.

Let's insert key one by one.

StepsKeyHash FunctionIndexDescription
120h1(20) = 20 mod 119No collision occurs.
234h1(34) = 34 mod 111No collision occurs.
345h1(45) = 45 mod 111Collision occur because index 1 is already occupied by 34. Now we will use the second hash function to calculate the index for the key 45.
h2(45) = 8 - (45 mod 8) = 3
h(45, 1) = (1 + 1 * 3) mod 11
4Here, we have taken the value if i 1 because first collision occurs. Note that here h2 (k) and 11 are relatively prime. The value of h2 (k) must be less than the table size.
470h1(70) = 70 mod 114Collision occur because index 4 is already occupied by 45. Now we will use the second hash function to calculate the index for the key 70.
h2(70) = 8 - (70 mod 8) = 2
h(70, 1) = (4 + 1 * 2) mod 11
6Here, we have taken the value if i 1 because first collision occurs. Note that here h2 (k) and 11 are relatively prime. The value of h2 (k) must be less than the table size.
556h1(56) = 56 mod 111Collision occur because index 1 is already occupied by 34. Now we will use the second hash function to calculate the index for the key 56.
h2(56) = 8 - (56 mod 8) = 8
h(56, 1) = (1 + 1 * 8) mod 11
9Again, collision occur. The index 9 is already occupied by 20.
h(56, 2) = (1 + 2 * 8) mod 116Here, the value of i is incremented by 1 because collision occurs second times. We note that again collision occurs because the index 6 is already occupied by 70.
h(56, 3) = (1 + 3 * 8) mod 113Here, the value of i is incremented by 1. The third index is empty. So, we will store 56 at index 3.

*indexes that are bold denotes collision.

After inserting all the keys, the hash table looks like the following.

Double Hashing in Java

Now we have clearly understood the double hashing. So, we can easily differentiate linear and quadratic probing.

In the linear probing, if collision occurs at any index, we look for the immediate next index. If the next index is also occupied, we look for the immediate next index. The process repeats until we get an empty index.

In quadratic probing, if collision occurs at any index, we look for the immediate next index. If the next index is also occupied, then we jump to the i2 index.

Suppose, index 2 is already occupied, then we look for 22 i.e. 4. It means look 4 indexes ahead from the current index (i.e. 2).

Similarly, if index 3 is also occupied, then we look for 32 i.e. 9. It means look 9 indexes ahead from the current index (i.e. 3).

Double Hashing Algorithm for Inserting an Element

  1. Set index = H(K); offset = H2(K)
  2. If table location index already contains the key, no need to insert it. Done!
  3. Else if table location index is empty, insert key there. Done!
  4. Else collision. Set index = (index + offset) mod M.
  5. If index == H(K), table is full! (Throw an exception, or enlarge table.) Else go to step 2.

Let's implement the above algorithm in a Java program.

Double Hashing Java Program

In the following Java programs, we have implemented double hashing in hash table.

DoubleHashingExample.java

Output:

Double Hashing in Java




Latest Courses