Lesson 34 +35 XP

Collision Resolution - Open Addressing, Chaining & Rehashing

Collision Resolution - Open Addressing, Chaining & Rehashing

A collision happens when two different keys hash to the same slot. Collisions cannot be avoided, so every hash table needs a collision-resolution technique. There are two families:

FamilyWhere colliding keys goMethods
Open addressing (closed hashing)Into another empty slot of the same table, found by probingLinear probing, quadratic probing, double hashing
Separate chaining (open hashing)Into a linked list hanging from the home slotChaining

1. Open Addressing

All keys live inside the table array, so the load factor λ = n / m can never exceed 1. When the home slot is full, the table is probed - slots are tried in a fixed order - until an empty slot is found. The i-th probe (i = 0, 1, 2, …) looks at slot H(k, i).

General insert and search

Algorithm OA_INSERT(T, M, KEY)
for I ← 0 to M - 1 do
    J ← H(KEY, I)               // I-th probe position
    if T[J] is EMPTY or DELETED then
        T[J] ← KEY
        return J
return OVERFLOW                 // table full

Deletion: a deleted slot cannot simply be emptied - that would create a gap that stops later searches too early and makes keys beyond it "disappear". Instead it is marked DELETED (a tombstone): searches pass over it, insertions may reuse it.

(a) Linear probing

H(k, i) = (h(k) + i) mod m - try the home slot, then the next slot, then the next, wrapping round to 0.

C
#define M 10
#define EMPTY -1
int table[M];                       /* all set to EMPTY at the start */

int insertLinear(int key) {
    int home = key % M;
    for (int i = 0; i < M; i++) {
        int j = (home + i) % M;
        if (table[j] == EMPTY) { table[j] = key; return j; }
    }
    return -1;                      /* table full */
}

Example: m = 10, h(k) = k mod 10; insert 89, 18, 49, 58, 9.

KeyHomeSlots probedPlaced atProbes
899991
188881
4999, 002
5888, 9, 0, 114
999, 0, 1, 224
DIAGRAM
 slot:   0    1    2    3    4    5    6    7    8    9
       +----+----+----+----+----+----+----+----+----+----+
       | 49 | 58 |  9 |    |    |    |    |    | 18 | 89 |
       +----+----+----+----+----+----+----+----+----+----+

Primary clustering: occupied slots form long runs. Any key hashing anywhere into a run lands at its end and makes the run longer, and neighbouring runs merge. The longer a run, the more likely it is to grow.

Expected probes (Knuth), load factor λ:

  • Successful search ≈ (1 / 2)(1 + (1 / 1 - λ))
  • Unsuccessful search ≈ (1 / 2)(1 + (1 / (1 - λ)2))

At λ = 0.5 that is 1.5 and 2.5 probes; at λ = 0.9 it is 5.5 and 50.5 probes. Keep λ ≤ 0.5-0.7 for open addressing.

Worked example with probe counts

Table size 11, h(k) = k mod 11; insert 20, 30, 2, 13, 25, 24, 10, 9.

Slot012345678910
Key9-2132524--302010
Probes31224111
  • Average successful search = (1 + 1 + 1 + 2 + 2 + 4 + 1 + 3) / 8 = 15 / 8 ≈ 1.88 probes.
  • Average unsuccessful search (start at each slot, count probes up to and including the first empty slot) = (2 + 1 + 5 + 4 + 3 + 2 + 1 + 1 + 5 + 4 + 3) / 11 = 31 / 11 ≈ 2.82 probes.

A long cluster

Table size 17, keys 6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45:

Slot0123-56789-10111213141516
Key34045-6237-281229113033

45 mod 17 = 11, but slots 11 → 16 and 0 → 1 are all full, so 45 needs 9 probes and lands in slot 2.

(b) Quadratic probing

H(k, i) = (h(k) + i2) mod m - jump 1, 4, 9, 16, … slots away from home.

Example: m = 10; insert 89, 18, 49, 58, 9.

KeyHomeSlots probedPlaced at
89999
18888
4999, 9 + 1 = 00
5888, 9, 8 + 4 = 12 → 22
999, 0, 9 + 4 = 13 → 33
  • Removes primary clustering: keys do not pile up in one run.
  • Secondary clustering: keys with the same home slot still follow the same probe sequence.
  • It may fail to find an empty slot even when one exists. If m is prime and λ &lt; 0.5, an empty slot is always found.

(c) Double hashing

H(k, i) = (h1(k) + i × h2(k)) mod m - the step size comes from a second hash function.

  • h2(k) must never be 0; a common choice is h2(k) = R - (k mod R) for a prime R &lt; m.

Example: m = 10, h1(k) = k mod 10, h2(k) = 7 − (k mod 7); insert 89, 18, 49, 58, 69.

Keyh1h2Slots probedPlaced at
899-99
188-88
4997 − 0 = 79, 16 → 66
5887 − 2 = 58, 13 → 33
6997 − 6 = 19, 10 → 00

49 and 69 both start at slot 9 but take different steps (7 and 1), so double hashing removes both primary and secondary clustering. It is the best open-addressing method but needs two hash computations.

Comparison of probing methods

BasisLinear probingQuadratic probingDouble hashing
Probe sequenceh, h + 1, h + 2, …h, h + 1, h + 4, h + 9, …h₁, h₁ + h₂, h₁ + 2h₂, …
Primary clusteringYesNoNo
Secondary clusteringYesYesNo
Finds an empty slotAlways (if one exists)Guaranteed only if m prime and λ &lt; 0.5If h2(k) and m are coprime
Speed of computingFastest; cache-friendlyFastTwo hash functions

2. Separate Chaining

Each slot holds a pointer to a linked list of all keys that hash to it. A collision just adds a node to the list.

Algorithm CHAIN_INSERT(T, KEY)
I ← h(KEY)
create node NEW with DATA ← KEY
NEW.NEXT ← T[I]                 // insert at the front of chain I
T[I] ← NEW

Algorithm CHAIN_SEARCH(T, KEY)
P ← T[h(KEY)]
while P ≠ NULL and P.DATA ≠ KEY do
    P ← P.NEXT
return P                        // NULL if not found

Example: keys 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 with h(k) = k mod 10 (new keys appended at the end of each chain for readability):

DIAGRAM
 [0] → 0
 [1] → 1 → 81
 [2]
 [3]
 [4] → 4 → 64
 [5] → 25
 [6] → 16 → 36
 [7]
 [8]
 [9] → 9 → 49

Analysis: the average chain length is λ = n / m (here 10 / 10 = 1).

  • Unsuccessful search: about 1 + λ (compute the hash, then scan a whole chain).
  • Successful search: about 1 + (λ / 2).
  • λ may be greater than 1; keep λ ≈ 1.
AdvantagesDisadvantages
Table never overflows; λ can exceed 1Extra memory for pointers
Deletion is ordinary list deletion - no tombstonesPoorer cache performance (nodes scattered)
No clustering; performance degrades gracefullyA long chain makes search slow (O(n) worst)

3. Open Addressing vs Separate Chaining

BasisOpen addressingSeparate chaining
Where keys are storedIn the table itselfIn linked lists outside the table
Load factorλ ≤ 1 (keep ≤ 0.5-0.7)λ can exceed 1 (keep ≈ 1)
Extra memoryNonePointers
ClusteringYes (linear/quadratic)No
DeletionNeeds DELETED markersSimple
Best whenNumber of keys known, memory tightNumber of keys unknown, many deletions

4. Rehashing

As the table fills, probe sequences or chains become long. Rehashing builds a bigger table (usually the next prime above twice the old size) and re-inserts every key using the new hash function.

PSEUDO
Algorithm REHASH(T, M)
NEWM ← smallest prime ≥ 2 × M
create table NEWT of size NEWM, all EMPTY
for each key K stored in T do
    insert K into NEWT using h(K) = K mod NEWM
T ← NEWT; M ← NEWM

When to rehash: when λ crosses a threshold (e.g. 0.5 or 0.7 for open addressing), when an insertion fails, or when there are too many DELETED markers.

Cost: one rehash is O(n), but since the table doubles, it happens rarely - the amortised cost per insertion stays O(1).

Example: table size 7 with h(k) = k mod 7 holds 13, 15, 24, 6 (λ = 4/7 ≈ 0.57). Rehash to size 17 (the next prime after 14): 13 → 13, 15 → 15, 24 → 7, 6 → 6.

5. Extendible Hashing (Dynamic Hashing)

Used for large files on disk where the data size changes.

  • A directory of 2d pointers is indexed by the first d bits of h(k) (d = global depth).
  • Each pointer leads to a disk bucket holding several records.
  • When a bucket overflows it is split into two, using one more bit; only if needed is the directory doubled.
  • A search needs at most two disk accesses (directory, then bucket), and the file grows without rehashing everything.
💡 Tip

Key points to remember - Linear: h + i (primary clustering). Quadratic: h + i² (secondary clustering). Double: h₁ + i × h₂ (no clustering). - Insert 89, 18, 49, 58, 9 into size 10: linear → 49:0, 58:1, 9:2; quadratic → 49:0, 58:2, 9:3. - Open addressing deletes with DELETED markers; keep λ ≤ 0.5-0.7. - Chaining: lists per slot, λ may exceed 1, easy deletion, extra pointer memory. - Rehash to about twice the size (a prime) when λ is too high.

🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. What is a collision? Explain linear probing, quadratic probing and double hashing. Insert 89, 18, 49, 58, 9 into a table of size 10. (10 Marks)
  • Write: the three probe formulas, the three insertion tables, primary and secondary clustering.
  • Q2. Explain separate chaining with an example. Compare it with open addressing. (7.5 Marks)
  • Write: the chain diagram for 0, 1, 4, …, 81, analysis 1 + λ, and the comparison table.
  • Q3. Insert 20, 30, 2, 13, 25, 24, 10, 9 into a table of size 11 by linear probing and find the average number of probes for successful and unsuccessful search. (7.5 Marks)
  • Q4. What is rehashing? When is it needed? (2.5 Marks)