Skip to content

Collisions: open addressing

Insert and search with linear probing, see how clusters make probes grow with the load factor, and delete with tombstones so later searches still work.

It opens on

A table of 7 slots holds 12 in slot 5, and slot = key % 7. 40 % 7 is also 5. Where does 40 go?

Why it matters

Open addressing keeps every key in one flat array, which is cache friendly and is what Python's dict and set use, with a cleverer probe sequence; tracing probes, explaining clustering and getting deletion right are standard exam questions on hashing.

Good to know first

  • Collisions: separate chaining: the load factor α = n/m and resizing by rehashing
  • Hash functions and hash tables: slot = key % m and collisions

Step by step

  1. Linear probing

    Insert keys with linear probing, trying slot key % m and then the next slots in turn, wrapping around, and count the probes.

    The run put 40 in slot 6 with 2 probes, which answers the opening question, and 7 went straight to its home, slot 0.

    the slots after 40 and 7

    A common mix-up

    A key that cannot go to its home slot is stored somewhere else at random.

    In fact: It goes to the first free slot after its home, in order, so a search can follow the same path.

  2. Searching and clustering

    Search by probing until the key or an empty slot, count probes, and explain how clusters make probe counts grow quickly with α.

    Searching 19 retraces its insert, 5, 6, 0, 1, and finds it in 4 probes.

    A common mix-up

    Probe counts grow in step with the load factor, as chain lengths do.

    In fact: Clusters make them grow much faster, about 1/(1 − α)² for failed searches, so open addressing tables resize at a lower α, often around 0.5 to 0.75.

  3. Deleting with tombstones

    Show why emptying a deleted key's slot loses other keys, delete with a tombstone instead, and reuse tombstones on insert.

    The run reported 19 not found after 2 probes. Emptying slot 6 cut the path that 19's insert had followed.

    A common mix-up

    Deleting from an open-addressing table is just emptying the slot.

    In fact: That breaks the probe path of keys pushed past it; a tombstone is needed.

Try it

Insert 26 into the table after 19. Where does it go, and after how many probes?Answer

Slot 2, after 5 probes, trying 5, 6, 0, 1 and 2

Hint: 26 % 7 = 5

In the table, how many probes does a search for 12 make, and for 5?Answer

1 for 12, at its home; 5 for 5, trying 5, 6, 0, 1 and stopping at the empty slot 2

Hint: 5 % 7 = 5

After 40 is replaced by a tombstone, delete 7 with a tombstone too. How many probes does a search for 19 take now?Answer

Still 4, through 5, 6 with DEL, 0 with DEL, and 1

Hint: Tombstones are walked past

40's home slot is 5, holding 12. Where does it go, and after how many probes?Answer

Slot 6, the next one, after 2 probes.

How many probes do searches for 26 and 9 make, and what do they find?Answer

26 tries 5, 6, 0, 1 and then 2, which is empty, so 5 probes and not found; 9 tries slot 2, empty, so 1 probe and not found.

Collisions: open addressing

30 minutes every month. No card.