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 stored keys and slots, the load factor is . A chain holds all keys with the same hash address, so chaining permits . 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 be a key, the slot count and 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.
| Method | Collision pattern | Deletion / condition |
|---|---|---|
| Chaining | Lists may grow to length | Unlink after locating key |
| Linear probing | Primary clustering: long contiguous runs | Use tombstones or a proved repair of the cluster |
| Quadratic probing | Same home slot gives same probe sequence | Empty slots may exist outside the reached subset |
| Double hashing | Step depends on key; reduces common probe sequences | Nonzero step alone is insufficient for composite |
For prime and probes , the first probes are distinct (odd prime); load below 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 , where 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 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 occupied slots has the more precise unsuccessful mean : the first empty position in a random permutation is the first of 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.