algoritmauzmanı
Sign in
TREN
9.3Separate Chaining (Linked List)
Lesson 9.3.4

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 . 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.

What you will learn
  • 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
5 min

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.

NULLNULLNULL01234567891022?26?37?15?31?
Chaining: the three colliding keys are in bucket 4's chain. Every new key goes to the head, so the order is 26, 37, 15.
2201231543752667831910
Open addressing with linear probing: 37 and 26 found their own bucket taken and moved to cells 5 and 6 (orange).

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 . With chaining there is no such interaction: the crowd in one bucket affects only its own keys.

2Load factor and speed

The α = 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, successfulChaining, unsuccessfulLinear probing, successfulLinear probing, unsuccessful
0.51.251.51.52.5
0.751.381.752.58.5
0.91.451.95.550.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 int and a pointer take 16 bytes with alignment, plus malloc'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 next jumps 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

PropertyOpen addressingChaining
Where a key livesInside the array, in another cell if neededIn the linked list of its own bucket
Load factorAt most 1; kept around 0.5Can go above 1; kept around 1
ClusteringYes (worst with linear probing)No; every bucket is independent
DeletionNeeds a deleted markerThe node is removed
Memory per keyOne cellOne node: value, pointer and malloc record
Cache friendlyYes, the cells are contiguousNo, the nodes are scattered
When the table is fullInsertion fails, growing is a mustChains get longer, growing is optional
Average cost of an operationO(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 dict and set use open addressing.
  • Chaining: when deletions are frequent, the number of keys cannot be predicted or the records are large. Java's HashMap uses 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?

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?

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?

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.