1. Gyanastra
  2. Database Systems
  3. Hash Functions and Double Hashing
  4. Where this fits
Free lesson, no account needed 6 min read

Database Systems · Hash Functions and Double Hashing

Where this fits

A hash table stores a record at an address computed from its key: h(k)h(k) gives the home bucket. Two independent problems must be solved:

  1. Which function hh? — it must spread keys uniformly over [0,M−1][0, M-1] and be cheap to compute.
  2. What if two keys land in the same bucket? — collision resolution: chaining, or open addressing (linear probing, quadratic probing, double hashing).

This chapter covers the hash-function choice and double hashing in full, plus the clustering that probing causes.


Notation (state it once, use it everywhere)

SymbolMeaning
MMnumber of buckets (table size); valid addresses 0,…,M−10,\dots,M-1
kkthe key (integer, or a string mapped to an integer)
h(k)h(k)home address, 0≤h(k)≤M−10 \le h(k) \le M-1
h1(k)h_1(k)primary hash function (gives the home slot)
h2(k)h_2(k)secondary/step hash function (gives the probe step)
iiprobe number, i=0,1,2,…i = 0,1,2,\dots
α\alphaload factor =n/M= n/M (nn = keys stored)

Convention warning (0-based vs 1-based). Standard CS texts number buckets 0..M−10..M-1 and probes i=0,1,2,…i=0,1,2,\dots. Unless a question says otherwise, use 0-based, probe starting at i=0i=0. If a question numbers slots 1..M1..M, subtract 1, compute, add 1 back.


3.1 Division method (the GATE favourite)

h(k)=k mod Mh(k) = k \bmod M

Rule: choose MM prime, and not close to a power of 2 or 10. Reason: if M=2pM = 2^p, then h(k)h(k) depends only on the low pp bits of kk — keys differing only in high bits collide. If M=10pM = 10^p, only the last pp decimal digits matter. A prime MM with no small factors mixes all digits.

Worked example 3.1. M=13M = 13 (prime), keys 25,41,64,9025, 41, 64, 90.

  • 25 mod 1325 \bmod 13: 13×1=1313\times1=13, 25−13=1225-13=12 → 12
  • 41 mod 1341 \bmod 13: 13×3=3913\times3=39, 41−39=241-39=2 → 2
  • 64 mod 1364 \bmod 13: 13×4=5213\times4=52, 64−52=1264-52=12 → 12
  • 90 mod 1390 \bmod 13: 13×6=7813\times6=78, 90−78=1290-78=12 → 12

Three keys collide at 12 — a bad key set, not a bad function; with M=13M=13 prime the residues are still spread over the full range for random keys.

Worked example 3.2 (why prime matters). M=16=24M = 16 = 2^4, keys 2,18,34,502, 18, 34, 50 (all ≡2 mod 16\equiv 2 \bmod 16): every one gives h=2h = 2. With M=17M = 17: 2→22\to2, 18→118\to1, 34→034\to0, 50→1650\to16 — all distinct. Exam line: "division method needs prime MM; power-of-two MM is a trap."

3.2 Mid-square method

Square the key, take the middle rr bits/digits as the address, where 2r≈M2^r \approx M.

Worked example 3.3. Key k=3121k = 3121, table of M=1000M = 1000 buckets (so take the middle 3 decimal digits). 31212=3121×31213121^2 = 3121 \times 3121. Compute: 3121×3000=9,363,0003121\times3000 = 9{,}363{,}000; 3121×121=377,6413121\times121 = 377{,}641; total =9,740,641= 9{,}740{,}641. 9740641 has 7 digits, so the middle three are digits 3–5: 406 → h=406h = 406. (Texts differ on how to centre when the digit count is odd; state your convention.)

Worked example 3.4 (binary). k=13=11012k = 13 = 1101_2, M=24=16M = 2^4 = 16 so r=4r = 4 bits. 132=169=10101001213^2 = 169 = 10101001_2 (8 bits). Middle 4 bits = 1010 =10= 10 → h=10h = 10.

3.3 Folding method

Split the key into equal parts and add the parts (shift folding), or reverse every alternate part before adding (boundary folding), then take  mod  M\bmod\ M.

Worked example 3.5. k=123456789k = 123456789, parts of 3 digits: 123, 456, 789123,\ 456,\ 789. Sum =123+456+789=1368= 123+456+789 = 1368. With M=1000M = 1000: 1368 mod 1000=3681368 \bmod 1000 = 368 → h=368h = 368.

Worked example 3.6 (boundary folding). Same key; reverse alternate parts: 123, 654, 789123,\ 654,\ 789. Sum =123+654+789=1566=123+654+789 = 1566; 1566 mod 1000=5661566 \bmod 1000 = 566 → h=566h = 566.

3.4 Hashing strings / non-numeric keys

A string must first be turned into an integer. Two standard ways:

(a) Sum of character codes — weak (anagrams collide: "abc" and "cab" give the same sum).

(b) Polynomial / Horner's rule — the standard good method: H(s)=(∑j=0L−1cj⋅B L−1−j) mod MH(s) = \left(\sum_{j=0}^{L-1} c_j \cdot B^{\,L-1-j}\right) \bmod M where cjc_j is the code of the jj-th character, LL the length, BB a base (often 31, 33, or 128). Evaluate left-to-right with Horner: H←(H⋅B+cj) mod MH \leftarrow (H\cdot B + c_j) \bmod M — this keeps numbers small.

Worked example 3.7. s=s = "CAT", ASCII C=67C=67, A=65A=65, T=84T=84, base B=31B=31, M=100M=100. Horner: start H=0H=0.

  • H=(0⋅31+67) mod 100=67H = (0\cdot31 + 67) \bmod 100 = 67
  • H=(67⋅31+65) mod 100=(2077+65) mod 100=2142 mod 100=42H = (67\cdot31 + 65) \bmod 100 = (2077+65) \bmod 100 = 2142 \bmod 100 = 42
  • H=(42⋅31+84) mod 100=(1302+84) mod 100=1386 mod 100=86H = (42\cdot31 + 84) \bmod 100 = (1302+84) \bmod 100 = 1386 \bmod 100 = 86

→ h("CAT")=86h(\text{"CAT"}) = 86. (Check the polynomial form: 67⋅312+65⋅31+84=67⋅961+2015+84=64387+2099=6648667\cdot31^2 + 65\cdot31 + 84 = 67\cdot961 + 2015 + 84 = 64387 + 2099 = 66486; 66486 mod 100=8666486 \bmod 100 = 86. ✓)

Worked example 3.8 (anagram trap). "ABC" codes 65,66,6765,66,67: sum =198=198; "CBA" codes 67,66,6567,66,65: sum =198=198 — same under method (a). Under Horner with B=31B=31: "ABC" =(65⋅31+66)⋅31+67=(2015+66)⋅31+67=2081⋅31+67=64511+67=64578= (65\cdot31+66)\cdot31+67 = (2015+66)\cdot31+67 = 2081\cdot31+67 = 64511+67 = 64578; "CBA" =(67⋅31+66)⋅31+65=(2077+66)⋅31+65=2143⋅31+65=66433+65=66498= (67\cdot31+66)\cdot31+65 = (2077+66)\cdot31+65 = 2143\cdot31+65 = 66433+65 = 66498. Different. ✓ Use polynomial hashing for strings.


Pages 1–2 of 2