9.1Hash Functions
The division method
The most widely used hash function is a single operation: divide the key by the table size and take the remainder. The remainder is always between 0 and M - 1, so it is a cell index directly. In this lesson we look at the division method and at how the table size is chosen.
- How
h(k) = k % Mturns a key into a cell - Why the table size is chosen to be a prime number
- Which table sizes spread keys badly
1Idea
In the division method the cell is the remainder of the key divided by M: h(k) = k % M. For M = 11, the remainder of 25 is 3, of 40 is 7 and of 121 is 0. The work is one division and takes the same time however large the key is.
The real job of the function is to spread keys evenly over the table. If the remainder only depends on the last part of the key, the spread breaks down. With M = 10 the remainder is the last digit of the key; every key ending in 0 lands in cell 0. With M = 2^k the remainder is the last k bits of the key; every even key lands in an even cell.
| Keys | With M = 10 | With M = 11 |
|---|---|---|
| 10, 20, 30, 40 | 0, 0, 0, 0 | 10, 9, 8, 7 |
| 15, 25, 35, 45 | 5, 5, 5, 5 | 4, 3, 2, 1 |
That is why M is chosen to be a prime that is not close to a power of 2. A prime shares no divisor with the keys; even if the keys grow by a fixed amount (10, 20, 30 ...), the remainders spread over the table.
Take care if keys can be negative: in C and Java -7 % 11 is -4, which is not a valid index. In this section keys are never negative; when needed, write ((k % M) + M) % M. In Python % always gives a non-negative result.
2Step by step
The place of 25, 40, 121 and 18 is computed in turn for an 11-cell table. Writing into the cell is not this lesson's job; the drawing only shows the cell that was computed.
hash(25) = 25 % 11 = 3: 25 = 2 · 11 + 3.- The place of 25 is cell 3.
hash(40) = 40 % 11 = 7: 40 = 3 · 11 + 7.hash(121) = 121 % 11 = 0: 121 = 11 · 11, no remainder.hash(18) = 18 % 11 = 7. Cell 7 belongs to 40: two keys landed on the same cell. This is a collision.
#define M 11 int hash(int key) { return key % M; // between 0 and M - 1}static final int M = 11; int hash(int key) { return key % M; // between 0 and M - 1}M = 11 def h(key): return key % M # between 0 and M - 1The snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
A single division; it does not depend on how large the key is.
No extra memory is used.
Division is slower than addition on a processor, but it still takes constant time. Where speed matters a lot, the multiplication method is preferred.
4Common mistake
A common mistake is choosing a "round" table size:
#define M 100 // bad: only the last two digits are usedFor keys whose last digits follow a pattern, such as student numbers or record numbers that all end in 00, every key piles into a few cells. A prime such as 97 or 101 spreads the same keys much better.
5Try it
With M = 11, what is hash(100)?
The answer is A. 100 = 9 · 11 + 1. The remainder is 1, so the key lands in cell 1.
Show the answer
The answer is A. 100 = 9 · 11 + 1. The remainder is 1, so the key lands in cell 1.
If the keys are multiples of 8 (8, 16, 24, 32 ...), which table size is the worst?
The answer is B. 16 and 8 share the divisor 8: the remainders are only 0 and 8, so two of the 16 cells are used. 13 and 17 are prime, and the remainders spread over every cell.
Show the answer
The answer is B. 16 and 8 share the divisor 8: the remainders are only 0 and 8, so two of the 16 cells are used. 13 and 17 are prime, and the remainders spread over every cell.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.