Trouble understanding the solution to this double hashing problem

168 Views Asked by At

I am having trouble understanding the third question about double hashing at the link below

http://www.cs.cmu.edu/~cburch/211-fa97/review/q-hash/index.html

Here is the question,

In the following, say our hash function for n is n % 20 and our second hash function, where applicable, is (n % 7) + 1.

Say we use double hashing for collision resolution. Beginning with an empty hash table, we insert the following.

8 38 3 5 28 18 65 83

How will the hash table look after the final insertion? If we then search for 48, how many of the inserted keys will we look at?

The solution on the page gives a hash table with the answer.

Answer:

   +----+
   | :  |
   | :  |
   +----+
 3 |  3 |
   +----+
 4 |    |
   +----+
 5 |  5 |
   +----+
 6 |    |
   +----+
 7 |    |
   +----+
 8 |  8 |
   +----+
 9 | 28 |
   +----+
10 | 83 |
   +----+
11 | 65 |
   +----+
12 |    |
   +----+
13 | 18 |
   +----+
   | :  |
   | :  |
   +----+
18 | 38 |
   +----+
19 |    |
   +----+

But I do not understand how the hash of 28 goes into slot 9 of the hash table.

From my understanding, when there is a collision we use the formula (h1(n) + i*h2(n)) mod m. Since 8 is hashed to slot 8 prior, we have a collision when we hash 28 since h1(28) = 8.

To resolve we use the formula above (28 % 20) + 1*((28 % 7) + 1) mod 8. Which is (8 + 1) mod 8 = 1. So I expect 28 to hash to slot 1 after collision, but the solution says it hashes to slot 9.

What am I doing wrong?

1

There are 1 best solutions below

1
madlee On

As someone pointed out, i was confusing m for array size, when it is table size. Once you set m = 20 everything makes sense. Still not sure why we know m = 20 but thank you.