Introduction

A hash function sends key kk to slot h(k)h(k) of a table T[0..m−1]T[0..m-1], and collisions (two keys with the same slot) cannot be avoided once the universe is larger than the table (see Hashing and Hash Functions). The cost analysis uses expectation and linearity of expectation from Random Variables, Expectation and Variance. A collision-resolution method decides where the second key goes and how a later search finds it.

There are two families. Chaining keeps a linked list in each slot, so a slot can hold any number of keys. Open addressing stores every key in the table itself and, on a collision, probes other slots in a fixed order until it finds a free one. This note gives the algorithms, traces all four standard methods on one input, and states the expected number of probes as a function of the load factor α=n/m\alpha = n/m.

Prerequisites

Core concepts

Chaining

Slot jj holds a pointer to a linked list of all keys with h(k)=jh(k) = j (NIL if there are none). New keys go at the head of their list.

Open addressing

Each slot holds one key, NIL (never used) or DELETED (used, then emptied). The hash function takes a second argument, the probe number i=0,1,…,m−1i = 0, 1, \ldots, m-1:

h(k,0), h(k,1), …, h(k,m−1)h(k, 0),\ h(k, 1),\ \ldots,\ h(k, m-1)

is the probe sequence of kk. INSERT takes the first slot in the sequence that is NIL or DELETED; SEARCH follows the same sequence until it finds kk or a NIL slot. For INSERT to succeed whenever the table has room, the probe sequence must be a permutation of 0,1,…,m−10, 1, \ldots, m-1. Because all keys live in the table, n≤mn \le m and so α≤1\alpha \le 1.

Let h′h' (and h1h_1, h2h_2) be ordinary hash functions into 0..m−10..m-1. For m≥2m\ge2, φ(m)\varphi(m) is Euler’s totient: the number of integers dd with 1≤d≤m−11\le d\le m-1 and gcd⁡(d,m)=1\gcd(d,m)=1. The three standard probe sequences are:

MethodProbe iiDistinct probe sequencesWeakness
Linear probing(h′(k)+i) mod m(h'(k) + i) \bmod mmmPrimary clustering: occupied runs grow and merge
Quadratic probing(h′(k)+c1i+c2i2) mod m(h'(k) + c_1 i + c_2 i^2) \bmod m, c2≠0c_2 \ne 0mmSecondary clustering: keys with the same home share the whole sequence; may not reach every slot
Double hashing(h1(k)+i h2(k)) mod m(h_1(k) + i\, h_2(k)) \bmod mmφ(m)m\varphi(m); Θ(m2)\Theta(m^2) for prime or power-of-two mmNone of these; needs h2(k)h_2(k) relatively prime to mm

Primary versus secondary clustering. In linear probing, any key whose home slot falls anywhere inside an occupied run lands at the run's end and lengthens it, so long runs grow faster than short ones (primary clustering). In quadratic probing, only keys with the same home slot follow the same sequence (secondary clustering), which is milder. Double hashing gives keys with the same home different step sizes h2(k)h_2(k), so it avoids both.

Double hashing condition. The sequence visits every slot exactly when gcd⁡(h2(k),m)=1\gcd(h_2(k), m) = 1. Two standard ways: mm prime and 1≤h2(k)<m1 \le h_2(k) < m, for example h2(k)=1+(k mod (m−1))h_2(k) = 1 + (k \bmod (m - 1)); or mm a power of 2 and h2(k)h_2(k) always odd.

Quadratic probing may not visit every slot. With h(k,i)=(h′(k)+i2) mod mh(k, i) = (h'(k) + i^2) \bmod m and m=11m = 11, the offsets i2 mod 11i^2 \bmod 11 for i=0,…,10i = 0, \ldots, 10 take only the 6 values 0,1,4,9,5,30, 1, 4, 9, 5, 3. With mm an odd prime, i2i^2 reaches exactly (m+1)/2(m+1)/2 residues, so an insertion can fail even though the table has free slots. It is guaranteed to succeed while the table is at most half full.

Algorithms and pseudocode

Pseudocode uses 0-based slots T[0..m−1]T[0..m-1], <- for assignment, x.key for the key of element x, and NIL and DELETED as markers.

Chaining

Text
CHAINED-HASH-INSERT(T, x)
1  insert x at the head of list T[h(x.key)]

CHAINED-HASH-SEARCH(T, k)
1  search list T[h(k)] for an element with key k

CHAINED-HASH-DELETE(T, x)
1  delete x from list T[h(x.key)]

INSERT is O(1)O(1) worst case if it does not check for duplicates. DELETE is O(1)O(1) worst case if it is given a pointer to the element and the lists are doubly linked (a singly linked list must first find the predecessor). SEARCH costs the length of one list.

Open addressing

Text
HASH-INSERT(T, k)
1  for i <- 0 to m - 1
2      j <- h(k, i)
3      if T[j] = NIL or T[j] = DELETED then
4          T[j] <- k
5          return j
6  error "hash table overflow"

HASH-SEARCH(T, k)
1  for i <- 0 to m - 1
2      j <- h(k, i)
3      if T[j] = NIL then
4          return NIL                // k is not in the table
5      if T[j] = k then
6          return j
7  return NIL

HASH-DELETE(T, k)
1  j <- HASH-SEARCH(T, k)
2  if j != NIL then
3      T[j] <- DELETED

Deletion leaves a marker. HASH-DELETE cannot set the slot to NIL: a later key whose probe sequence passed through this slot would then be cut off, because SEARCH stops at NIL. DELETED means "keep probing" to SEARCH and "free" to INSERT. With many deletions, search times depend on the number of markers as well as on α\alpha, which is why chaining is preferred when keys are deleted often.

Counting convention. A probe is one examination of a slot. A successful first-try insertion or search costs 1 probe. In chaining, the cost of a search is the number of list elements compared, plus the O(1)O(1) cost of computing hh.

Worked examples

Insert 54,26,93,17,77,31,44,2054, 26, 93, 17, 77, 31, 44, 20, in that order, into a table with m=11m = 11 and home slot h′(k)=k mod 11h'(k) = k \bmod 11. The home slots are 10,4,5,6,0,9,0,910, 4, 5, 6, 0, 9, 0, 9, so the first six keys go straight home and only 44 and 20 collide.

MethodProbes for 44Probes for 20Final table, slots 0 to 10 (dash = empty)Total probes
Chaining (insert at head)1 list1 listslot 0: 44 then 77; slot 9: 20 then 31not applicable
Linear, (h′(k)+i) mod 11(h'(k) + i) \bmod 110, 1: 2 probes9, 10, 0, 1, 2: 5 probes77 44 20 - 26 93 17 - - 31 5413
Quadratic, (h′(k)+i2) mod 11(h'(k) + i^2) \bmod 110, 1: 2 probes9, 10, 2: 3 probes77 44 20 - 26 93 17 - - 31 5411
Double, h2(k)=1+(k mod 10)h_2(k) = 1 + (k \bmod 10)0, 5, 10, 4, 9, 3: 6 probes9, 10, 0, 1: 4 probes77 20 - 44 26 93 17 - - 31 5416

The details that decide exam answers:

  • Linear. 20 starts at 9 (31), wraps from 10 (54) to 0 (77) and 1 (44), and stops at 2. The keys in slots 9, 10, 0, 1 and 2 now form one run of length 5 across the wrap-around.
  • Quadratic. 20 probes 9+0=99 + 0 = 9, 9+1=109 + 1 = 10, 9+4=13≡29 + 4 = 13 \equiv 2. It skips slots 0 and 1, which is why it needs fewer probes than linear probing here.
  • Double hashing. h2(44)=1+4=5h_2(44) = 1 + 4 = 5, so 44 probes 0,5,10,4,9,30, 5, 10, 4, 9, 3; h2(20)=1+0=1h_2(20) = 1 + 0 = 1, so 20 steps by 1 from slot 9. On a single small input double hashing can need more probes; its advantage is in the expected cost over many keys, not on every input.
Chaining and the three probing methods on one input
Figure 1. The keys 54 26 93 17 77 31 44 20 with home slot k mod 11k \bmod 11 under four methods: chaining (lists at slots 0 and 9), linear probing (20 wraps round to slot 2), quadratic probing (20 probes 9, 10, 2) and double hashing with h2(k)=1+(k mod 10)h_2(k) = 1 + (k \bmod 10) (44 probes 0, 5, 10, 4, 9, 3).

Where does the next key go under linear probing?

After the linear-probing insertions the free slots are 3, 7 and 8. If the next key's home slot is uniformly random over 0..100..10:

  • It lands in slot 3 if its home is 9, 10, 0, 1, 2 or 3: probability 6/116/11.
  • It lands in slot 7 if its home is 4, 5, 6 or 7: probability 4/114/11.
  • It lands in slot 8 only if its home is 8: probability 1/111/11.

The free slot just after the longest run is the most likely target, so the run grows further. This is primary clustering in numbers.

Which insertion orders give this table?

A table of size 10 with h′(k)=k mod 10h'(k) = k \bmod 10 and linear probing holds 42, 23, 34, 52, 46, 33 in slots 2 to 7, in that order, after six insertions into an empty table. Work out what each key needed when it was inserted.

  • 42, 23, 34 and 46 sit in their home slots (2, 3, 4, 6), so each only needed its home slot to be free.
  • 52 has home 2 but sits in slot 5, so 42, 23 and 34 were all inserted before it (otherwise it would have stopped earlier); 46 does not affect it, since 52 stops at slot 5 either way.
  • 33 has home 3 but sits in slot 7, so slots 3, 4, 5 and 6 were all occupied first: 33 came after 23, 34, 52 and 46, and so it was inserted last.

So 33 is last, 52 follows 42, 23 and 34 (in any of 3!=63! = 6 orders), and 46 can go into any of the 5 positions among those four keys: 6×5=306 \times 5 = 30 insertion orders produce the table. The order 46, 34, 42, 23, 52, 33 is one of them; 46, 42, 34, 52, 23, 33 is not (52 would take slot 3 before 23 arrived).

Expected bounds in this section assume insertion-only/no-tombstone occupancy models. With deletions, live load factor alone does not capture tombstone occupancy.

Mathematical explanation

Chaining under simple uniform hashing

Let α=n/m\alpha = n/m. The expected length of each list is α\alpha.

  • Unsuccessful search: examines one whole list, so the expected number of elements compared is α\alpha and the time is Θ(1+α)\Theta(1 + \alpha).
  • Successful search (key chosen uniformly from the nn stored keys, insertion at the head): the expected number of elements compared is
1+α2−α2n=1+n−12m,1 + \frac{\alpha}{2} - \frac{\alpha}{2n} = 1 + \frac{n-1}{2m},

because the keys examined before the target are those inserted after it into the same list, and each of the n−1n - 1 other keys is in the target's list with probability 1/m1/m and inserted after it in half the cases.

If n=O(m)n = O(m), then α=O(1)\alpha = O(1) and every chaining operation takes O(1)O(1) expected time.

Open addressing under uniform hashing

Uniform hashing assumes each key's probe sequence is equally likely to be any of the m!m! permutations of 0..m−10..m-1. For α<1\alpha < 1, the expected number of probes is:

OperationExpected probes (bound under uniform hashing)α=0.5\alpha = 0.5α=0.9\alpha = 0.9
Unsuccessful search, and insertionat most 11−α\dfrac{1}{1 - \alpha}210
Successful searchat most 1αln⁡11−α\dfrac{1}{\alpha} \ln \dfrac{1}{1 - \alpha}1.3862.558

Why 1/(1−α)1/(1 - \alpha). The first probe always happens. A slot is occupied with probability about α\alpha, so a second probe happens with probability at most α\alpha, a third with probability at most α2\alpha^2, and so on: 1+α+α2+⋯=1/(1−α)1 + \alpha + \alpha^2 + \cdots = 1/(1-\alpha). Inserting a key costs the same as an unsuccessful search for it. A successful search for the key inserted (i+1)(i+1)-th repeats that insertion, which cost at most 1/(1−i/m)1/(1 - i/m) probes; averaging over i=0..n−1i = 0..n-1 and bounding the sum by an integral gives the logarithm.

Linear probing is worse. Linear probing cannot satisfy the uniform hashing assumption, because it has only mm probe sequences. Its standard analysis assumes instead that each key's home slot h′(k)h'(k) is uniformly random and independent of the others (simple uniform hashing of the home slot). Under that assumption, and because of primary clustering, the classical approximations for large mm are about 12(1+1(1−α)2)\frac{1}{2}\left(1 + \frac{1}{(1-\alpha)^2}\right) probes for an unsuccessful search and 12(1+11−α)\frac{1}{2}\left(1 + \frac{1}{1-\alpha}\right) for a successful one: 2.5 and 1.5 at α=0.5\alpha = 0.5, but 50.5 and 5.5 at α=0.9\alpha = 0.9. Double hashing behaves close to the uniform-hashing bounds.

Expected probes against load factor
Figure 2. Expected probes against the load factor α\alpha from 0 to 0.95: chaining unsuccessful 1+α1 + \alpha (one hash evaluation plus α\alpha list elements), uniform-hashing upper bounds: unsuccessful 1/(1−α)1/(1-\alpha) and successful (1/α)ln⁡(1/(1−α))(1/\alpha)\ln(1/(1-\alpha)), and the linear-probing unsuccessful large-table approximation 12(1+1/(1−α)2)\frac{1}{2}(1 + 1/(1-\alpha)^2), which rises fastest.

Important properties and observations

PropertyChainingOpen addressing
Load factorany α\alpha, may exceed 1α≤1\alpha \le 1; performance collapses as α→1\alpha \to 1
Extra spaceone pointer per key plus mm headsnone beyond the table
Deletionsimple and exactneeds DELETED markers
Worst-case searchΘ(n)\Theta(n) (one long list)O(m)O(m) with tombstones; O(n+1)O(n+1) without them and with distinct-slot probing
Expected unsuccessful searchΘ(1+α)\Theta(1 + \alpha)at most 1/(1−α)1/(1 - \alpha) probes (uniform hashing)
Cache behaviourpointer chasinglinear probing scans adjacent slots

Common misconceptions.

  • "Quadratic probing always finds a free slot." It is guaranteed only while the table is at most half full (with mm prime); beyond that an insertion can fail with free slots left.
  • "Deletion in open addressing empties the slot." It must mark it DELETED, or later searches fail.
  • "Double hashing works with any h2h_2." h2(k)h_2(k) must never be 0 and must be relatively prime to mm; with h2(k)=0h_2(k) = 0 the sequence never leaves the home slot.
  • "The order of insertion does not matter." With open addressing the final table depends on the order; with chaining only the order inside each list does.

GATE examination perspective

  • PROBE TRACES. Insert the keys in the given order, write the probe sequence of every colliding key, and remember to wrap with  mod m\bmod m. Count probes using the question's convention (first probe counts 1 unless stated otherwise).
  • WHICH SEQUENCE GIVES THIS TABLE. For a given final linear-probing table, check each candidate order: a key can sit away from its home only if every slot from its home up to its position (cyclically) was occupied when it was inserted.
  • COUNTING ORDERS. To count the insertion orders that give a linear-probing table, turn each displaced key into "inserted after every key in the slots it passed over", then count the orders that satisfy those precedences (30 in the worked example).
  • NEXT-SLOT PROBABILITY. Under linear probing, the probability that the next key lands in free slot ss is (number of home slots from which probing reaches ss) divided by mm, as in the worked example.
  • EXPECTED COST FORMULAS. Chaining Θ(1+α)\Theta(1 + \alpha); open addressing at most 1/(1−α)1/(1-\alpha) unsuccessful and (1/α)ln⁡(1/(1−α))(1/\alpha)\ln(1/(1-\alpha)) successful. Plug in α\alpha exactly as n/mn/m.
  • DELETION TRAPS. A search after a deletion continues past DELETED slots.

Summary

Sources

  1. T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009: Chapter 11, Section 11.2 (hash tables with chaining and their analysis) and Section 11.4 (open addressing: linear probing, quadratic probing, double hashing and the uniform-hashing analysis).

Next steps