SimuVeda/ Computer Science

Computer Science · Simulator 058

Hash Tables

A hash table turns a key into an array index so lookups are nearly instant. But two keys can land in the same slot. Insert keys one by one, compare collision-resolution strategies, and watch how probe counts climb as the table fills.

CollegeCollege data structures

Experiments 0 of 0 Challenge not tried

How the model works

A hash function maps a key to a slot: h(k) = k mod m for integer keys, or a polynomial string hash. When two keys map to the same slot, that is a collision:

  • Separate chaining: each slot holds a list; colliding keys are appended. Expected list length is the load factor α = n/m.
  • Linear probing: try h, h+1, h+2, … until a free slot. Simple and cache-friendly, but keys bunch into primary clusters. Expected probes for a successful search ≈ ½(1 + 1/(1 − α)).
  • Quadratic probing: try h + 1², h + 2², …, which breaks up primary clusters.

As α approaches 1, open addressing degrades sharply. Tables usually resize (double m and rehash) at α ≈ 0.7. The readout compares measured average probes with these formulas. A poor hash, for example m sharing factors with patterned keys, makes collisions much worse.

Keys are integers or short strings; deletion (which needs tombstones in open addressing) is not shown.

Guided experiments

  1. Fill to 70% with linear probing. m = 31, insert 22 random keys. Prediction: clusters form (the longest here is 7 slots) and the average search needs about 1.5 probes. The textbook value ½(1 + 1/(1 − α)) ≈ 2.2 is an average over large tables, so a single small table can differ. Try other seeds.
  2. Same keys, chaining. Prediction: average chain length 0.7; successful search ≈ 1 + α/2 ≈ 1.35 probes.
  3. Bad table size. m = 10 and keys that are multiples of 5. Prediction: every key lands in slot 0 or 5, so long chains form however big the table is. Change m to 11 (prime) and they spread out.

Challenge

With linear probing, why is inserting into a slot right after a long cluster more likely than into an isolated empty slot?

Reveal the explanation

Any key hashing anywhere into the cluster probes to its end, so a slot right after a cluster of length L is filled with probability about (L + 1)/m, against 1/m for an isolated slot. Clusters therefore grow faster the longer they are: the “rich get richer” effect behind primary clustering.

References

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, ch. 11 “Hash Tables”.
  • Knuth, The Art of Computer Programming, Vol. 3, §6.4.

Related simulators

Worked example

Table of size m = 11 with h(k) = k mod 11. Insert 22, 13, 35, 44 and 5.

Home slots: 22 → 0, 13 → 2, 35 → 2, 44 → 0, 5 → 5. Two collisions.

Linear probing: 22 goes to 0 and 13 to 2. 35 finds 2 taken and moves to 3. 44 finds 0 taken and moves to 1. 5 goes to 5. Probes: 1, 1, 2, 2, 1, an average of 1.4 to find each key again.

Chaining: slot 0 holds 22 and 44, slot 2 holds 13 and 35 and slot 5 holds 5. The average successful search is also 1.4.

Load factor α = 5 / 11 ≈ 0.45. At this level both methods are fast; linear probing slows sharply above about 0.7. Set m = 11 and type the five keys in.

Common questions

What is a hash table?

A structure that turns a key into an array position with a hash function, so lookups, inserts and deletes take about the same short time however many items are stored.

What is the difference between chaining and open addressing?

Chaining keeps a small list at each slot for all keys that land there. Open addressing, such as linear or quadratic probing, stores every key in the array itself and searches for the next free slot on a collision.

Why should the table size be a prime number?

Keys often share patterns, such as all being even or multiples of 5. With a prime size, k mod m spreads such keys over all slots instead of a few.

Keyboard shortcuts

Space
Play or pause
S
Step
R
Reset
F
Full screen
?
Show this list