Chapter 4

Hash tables

A hash table maps keys to slots so that lookup examines only a small part of the stored data. Collisions require a storage rule; expected costs additionally require a probability model.

4.1 Load, chaining and collisions

Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks

For nn stored keys and m>0m>0 slots, the load factor is α=n/m\alpha=n/m. A chain holds all keys with the same hash address, so chaining permits α>1\alpha>1. A collision is an equal address for two different keys, not equality of the keys themselves.

4.2 Hash functions and collision handling

Past papers · CS 2014–2026 (23 papers): 4 questions, 5 marks · DA 2024–2026 (3 papers): 1 question, 1 mark

Let kk be a key, mm the slot count and nn the number of stored keys. Chaining stores colliding keys in a list; open addressing stores all keys in the table and probes other slots. In either method, a collision means equal hash addresses for different keys.

MethodCollision patternDeletion / condition
ChainingLists may grow to length nnUnlink after locating key
Linear probingPrimary clustering: long contiguous runsUse tombstones or a proved repair of the cluster
Quadratic probingSame home slot gives same probe sequenceEmpty slots may exist outside the reached subset
Double hashingStep depends on key; reduces common probe sequencesNonzero step alone is insufficient for composite mm

For prime mm and probes h0+i2h_0+i^2, the first (m+1)/2(m+1)/2 probes are distinct (odd prime); load below 1/21/2 guarantees finding an empty slot. An unsuccessful search stops at a never-used empty slot, not a tombstone. An insertion can remember a tombstone but must continue far enough to rule out an existing duplicate if duplicate keys are forbidden.

The multiplication method is h(k)=⌊m{kA}⌋h(k)=\lfloor m\{kA\}\rfloor, where 0<A<10<A<1 is fixed and braces denote fractional part. A fixed division or multiplication function offers no general protection against adversarial keys.

4.3 Expected hashing costs

Past papers · CS 2014–2026 (23 papers): 5 questions, 7 marks · DA 2024–2026 (3 papers): 1 question, 1 mark

Probability statements require a model. Simple uniform hashing for chaining distributes each key independently among mm slots. Uniform open-address hashing assigns each key an independent uniform permutation of the slots; it is not the same model as linear probing.

Uniform probing with exactly n<mn<m occupied slots has the more precise unsuccessful mean (m+1)/(m−n+1)(m+1)/(m-n+1): the first empty position in a random permutation is the first of m−nm-n uniformly placed empty slots. Universal hashing controls expected collisions for any fixed key set; it does not prohibit all collisions or protect against an adversary who knows the chosen function and adapts keys to it.

Last-minute recall

  • Chaining permits more keys than slots.
  • Occupancy probabilities describe initial hash addresses.
  • Colliding pairs and displaced keys are different counts.
  • A double-hash step must be coprime to the table size.
  • Tombstones preserve search chains.
  • State the random model before using a hash expectation.

Revise offline: the whole Algorithms handbook as one PDF

52 pages · 2.6 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections