9.3Separate Chaining (Linked List)
Open addressing or chaining?
You handled collisions in two ways: with open addressing a colliding key looks for another free cell in the array, with chaining it is added to the linked list of its own bucket. The operations had the same names and came in the same order: insert, search, delete. This lesson puts the differences side by side and explains which one to pick and when.
- How the same keys are placed in the table by the two methods
- How the load factor affects the speed of the two methods differently
- Which one to pick and when, for memory, deletion and the cache
1The same keys, two tables
22, 15, 37, 26 and 31 are inserted in that order into a table of size M = 11 with h(k) = k % 11. 15, 37 and 26 all land in bucket 4.
With open addressing 37 and 26 settled in the home cells of other keys. If a key that belongs to bucket 5 or 6 arrives now, it will find its own cell taken, even though no key had ever landed in those buckets before. Runs of filled cells that grow and merge are called clustering. With chaining there is no such interaction: the crowd in one bucket affects only its own keys.
2Load factor and speed
The load factor α = n / M sets the speed of both methods, but in different ways. With chaining α is the average chain length and can go above 1; the operations slow down linearly with α. With open addressing α can be at most 1, and as it gets close to 1 the number of probes explodes. The table below gives the average number of cells or nodes looked at, assuming the keys are spread evenly (with chaining the bucket itself counts as one look):
| α | Chaining, successful | Chaining, unsuccessful | Linear probing, successful | Linear probing, unsuccessful |
|---|---|---|---|---|
| 0.5 | 1.25 | 1.5 | 1.5 | 2.5 |
| 0.75 | 1.38 | 1.75 | 2.5 | 8.5 |
| 0.9 | 1.45 | 1.9 | 5.5 | 50.5 |
The formulas are 1 + α/2 and 1 + α for chaining, and Knuth's ½(1 + 1/(1 − α)) and ½(1 + 1/(1 − α)²) for linear probing. The result is clear: open addressing works well only while the table is about half empty, which is why the table is usually grown once α passes 0.5. With chaining even α = 1 is acceptable.
3Memory, deletion and the cache
- Memory: with open addressing every key is one cell of the array. With chaining every key is a separate node: on a 64-bit system an
intand a pointer take 16 bytes with alignment, plusmalloc's own record. The array of bucket pointers takes room too. On the other hand, open addressing has to set aside empty cells in advance, because α must stay small. - Deletion: with chaining the node is taken out of the list and leaves no trace. Open addressing needs a deleted marker; the markers pile up, searches have to step over them and the table has to be rebuilt from time to time.
- Cache: with open addressing probing looks at neighbouring cells of an array; the processor's cache loves that. The nodes of a chain are scattered in memory and every
nextjumps somewhere else. That is why at a low α open addressing is often faster in practice. - Filling up: with open addressing the table fills up and insertion fails. With chaining the table never fills, it only slows down.
4Summary table
| Property | Open addressing | Chaining |
|---|---|---|
| Where a key lives | Inside the array, in another cell if needed | In the linked list of its own bucket |
| Load factor | At most 1; kept around 0.5 | Can go above 1; kept around 1 |
| Clustering | Yes (worst with linear probing) | No; every bucket is independent |
| Deletion | Needs a deleted marker | The node is removed |
| Memory per key | One cell | One node: value, pointer and malloc record |
| Cache friendly | Yes, the cells are contiguous | No, the nodes are scattered |
| When the table is full | Insertion fails, growing is a must | Chains get longer, growing is optional |
| Average cost of an operation | O(1 / (1 − α)) | O(1 + α) |
5Which one to pick?
- Open addressing: when the keys are small and of fixed size, deletions are rare and the table can be kept large enough. Python's
dictandsetuse open addressing. - Chaining: when deletions are frequent, the number of keys cannot be predicted or the records are large. Java's
HashMapuses chaining; if a chain gets too long it turns it into a balanced tree. The lesson on built-in hash tables covers both in detail.
In an exam there is no single answer to "which is better". Back your answer with the load factor, how often keys are deleted and memory: open addressing for a low α and few deletions, chaining for a high α and frequent deletions.
6Try it
A table with M = 11 holds 22 keys. Which method makes this possible?
The answer is B. With open addressing every key takes a cell; at most 11 keys fit in 11 cells. With chaining α = 22 / 11 = 2 and the chains hold two nodes on average.
Show the answer
The answer is B. With open addressing every key takes a cell; at most 11 keys fit in 11 cells. With chaining α = 22 / 11 = 2 and the chains hold two nodes on average.
When the load factor rises to 0.9, which method slows down much more?
The answer is B. An unsuccessful search takes 1.9 looks on average with chaining and 50.5 with linear probing. The cost of open addressing grows with 1 / (1 − α) and explodes as α gets close to 1.
Show the answer
The answer is B. An unsuccessful search takes 1.9 looks on average with chaining and 50.5 with linear probing. The cost of open addressing grows with 1 / (1 − α) and explodes as α gets close to 1.
Which method suits an application that deletes very often?
The answer is A. With open addressing a cell cannot be emptied, a deleted marker is left and the markers slow searches down. With chaining the deleted node is taken out of the list and nothing is left behind.
Show the answer
The answer is A. With open addressing a cell cannot be emptied, a deleted marker is left and the markers slow searches down. With chaining the deleted node is taken out of the list and nothing is left behind.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.