9.1Hash Functions
Collisions and the load factor
However good the hash function is, sooner or later two different keys land on the same cell. This is called a collision. This lesson explains why collisions are unavoidable and introduces the load factor, which measures how full a table is.
- Why collisions cannot be prevented, and how much earlier than expected they come
- How to compute the load factor
α = n / M - Why operations slow down as the load factor rises
1Idea
Collisions are unavoidable
Keys such as student numbers have billions of possible values, while a table has a few thousand cells. Any function that spreads more keys over fewer cells must make some keys share a cell. This is the pigeonhole principle: if 12 keys go into 11 cells, at least one cell gets two keys.
Collisions start long before the table is full. In a class of 23 people, the chance that two share a birthday is over 50 percent (the birthday problem). In an 11-cell table, the chance of at least one collision is about 46 percent with only 4 random keys, and 66 percent with 5.
So the real question is not how to avoid collisions but what to do when they happen. There are two families of solutions: open addressing, which puts the key into another empty cell inside the table, and separate chaining, which keeps the keys of a cell in a list.
The load factor
How full a table is, is measured by the load factor: α = n / M. n is the number of keys in the table and M the number of cells. If 6 keys are in 11 cells, α = 6 / 11 ≈ 0.55.
With open addressing each cell holds at most one key, so α cannot pass 1, and as it gets close to 1 the attempts grow quickly. The average numbers of attempts given by Knuth for linear probing:
| α | Successful search | Unsuccessful search and insertion |
|---|---|---|
| 0.5 | 1.5 | 2.5 |
| 0.75 | 2.5 | 8.5 |
| 0.9 | 5.5 | 50.5 |
With chaining α can pass 1; the average chain length is α. With both methods, once the table passes a certain load factor it is moved into a larger table: the threshold is usually 0.5 to 0.7 for open addressing and 1 for chaining.
2Step by step
25, 40, 18, 121, 36 and 7 are spread over an 11-cell table in turn. Each cell shows the first key that landed in it; a key landing on a cell that already has one is a collision.
- 25:
25 % 11 = 3. Cell 3 is free and gets marked. - 40:
40 % 11 = 7. Cell 7 is free. - 18:
18 % 11 = 7. 40 already landed in cell 7: the first collision. - 121:
121 % 11 = 0. Cell 0 is free. - 36:
36 % 11 = 3. Cell 3 holds 25: the second collision. - 7:
7 % 11 = 7. A third key lands in cell 7: the third collision. - 3 of the 6 keys collided, yet the table is not even half full:
α = 6 / 11 ≈ 0.55.
#define M 11 int countCollisions(int keys[], int n) { bool used[M] = {false}; int collisions = 0; for (int i = 0; i < n; i++) { int h = keys[i] % M; // home cell if (used[h]) { collisions++; // a key already landed in this cell } else { used[h] = true; } } return collisions;} double loadFactor(int n) { return (double)n / M; // α = n / M}static final int M = 11; int countCollisions(int[] keys) { boolean[] used = new boolean[M]; int collisions = 0; for (int i = 0; i < keys.length; i++) { int h = keys[i] % M; // home cell if (used[h]) { collisions++; // a key already landed in this cell } else { used[h] = true; } } return collisions;} double loadFactor(int n) { return (double) n / M; // α = n / M}M = 11 def count_collisions(keys): used = [False] * M collisions = 0 for k in keys: h = k % M # home cell if used[h]: collisions += 1 # a key already landed in this cell else: used[h] = True return collisions def load_factor(n): return n / M # α = n / MThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
An array of M elements that records which cells are used.
Computing the load factor is a single division (O(1)); the table keeps a count field, so there is no need to count the keys.
4Common mistake
A common mistake is computing the load factor with integer division:
double loadFactor(int n) { return n / M; // wrong: 6 / 11 is 0 in integer division}n and M are integers, so the division is integer division and the result is 0 until the table is completely full; the question "should the table grow?" never gets a yes. One of them has to be turned into a double before dividing: (double)n / M. In Python 3, / is already true division.
5Try it
With M = 11, how many collisions do the keys 10, 21, 32 and 43 cause?
The answer is C. All four leave remainder 10: 10, 21 = 11 + 10, 32 = 22 + 10, 43 = 33 + 10. The first one takes the cell and the other three collide.
Show the answer
The answer is C. All four leave remainder 10: 10, 21 = 11 + 10, 32 = 22 + 10, 43 = 33 + 10. The first one takes the cell and the other three collide.
A table with 20 cells holds 15 keys. What is the load factor?
The answer is B. α = n / M = 15 / 20 = 0.75. For a linearly probed table this is high; an unsuccessful search takes 8.5 attempts on average.
Show the answer
The answer is B. α = n / M = 15 / 20 = 0.75. For a linearly probed table this is high; an unsuccessful search takes 8.5 attempts on average.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.