k-Independence & Tabulation Hashing
How much randomness a hash function really needs.
A truly random function from 64-bit keys to slots would need a table with entries. Real hash tables run on functions described by a few random words, so the real question is how much randomness each application needs. Pairwise independence is enough for chaining, linear probing needs exactly five-wise, and a trick from 1970 computer chess, a few tables of random numbers combined by XOR, behaves like full randomness almost everywhere.
What you’ll learn
- Define -independence and prove that random polynomials of degree over a prime field are -independent.
- Prove what limited independence buys for chaining: a maximum load of from pairwise independence and from -independence.
- State the independence thresholds for linear probing (5 suffices, 4 does not) and explain why fast weak families fail on structured keys.
- Prove that simple tabulation is 3-independent but not 4-independent, and state what Pătraşcu and Thorup proved it achieves anyway.
Independence as a resource
A fully random function is -independent, but storing one takes bits, as large as a table indexed by every possible key. The art is to get the guarantee an application needs from a short random seed and a fast evaluation. -independence is the standard yardstick, and the classic construction is a random polynomial.
Fix distinct keys and targets . The coefficients that hit every target solve a Vandermonde system:
Its determinant is nonzero modulo the prime , so exactly one coefficient vector works: Lagrange interpolation over a field. Out of equally likely vectors,
Reducing modulo sends or residues to each slot, so each slot's probability lies within a factor of , independently for the keys:
import random
P = (1 << 61) - 1 # Mersenne prime
def make_k_independent(k, m):
coeffs = [random.randrange(P) for _ in range(k)] # the seed: k words
def h(x): # integer key, 0 <= x < P
v = 0
for c in coeffs: # Horner's rule: k multiplications
v = (v * x + c) % P
return v % m
return hSeed of words, multiplications per key. With the factor is indistinguishable from 1 for any realistic table. The speed is the problem: a degree-4 polynomial costs several times more than one multiplication.
What independence buys for chaining
With chaining, expected chain lengths need only universality: at most , as the Optimized lesson proved. The longest chain is another matter. For keys in slots, a fully random function makes the fullest slot hold keys with high probability. How much independence do you need to match that?
keys in one slot. A slot receives or more keys only if some set of keys all hash to . For each of the sets, -wise independence gives probability . A union bound over the sets and the slots, with :
Logarithmic independence. With and , the right side is for large . A family that is -independent for this therefore matches a fully random function:
Pairwise independence. Let count the colliding pairs of keys: for . A slot with keys contributes pairs on its own, so by Markov's inequality
You hash keys into slots with a merely pairwise independent function. Below what maximum load does the bound above guarantee, with probability at least ?
Show the answer
1415 keys
. Compare a fully random function, whose fullest slot holds about 8 keys at this size, and 15-independence, for which the union bound gives . The square-root scale is not slack in the proof: Alon, Dietzfelbinger, Miltersen, Petrank and Tardos (1999) showed that universality keeps the expected largest bucket below , and that for linear maps over a field of square order , some sets of keys always produce a bucket of keys.
Linear probing needs exactly five
Linear probing stores keys in the table itself and scans forward from to the first free slot. A lookup costs the length of the run of occupied slots through , and runs grow by merging, so a few unlucky keys slow down their neighbors: primary clustering. Knuth's classic analysis assumed a truly random function. How much independence does the expected cost really need?
The proof idea explains the number five. A run of length at least through forces one of a constant number of aligned intervals of length proportional to , near , to receive more keys than it has slots, although it expects only an fraction of that. A fourth-moment bound makes this probability . It needs the other keys to be 4-wise independent given , which is exactly 5-independence.
Over the scales , the expected run length is then . With only second moments, Chebyshev's tail is , every scale contributes , and the scales add up to a logarithm.
In the analysis of linear probing, why is 4-wise independence of the keys one short?
- Linear probing compares keys in groups of five
- Five is the smallest independence for which the load factor can exceed
- The fourth-moment bound for an interval near must hold conditioned on the query key's own slot, which uses one more degree of independence
- A 4-independent family cannot be evaluated in constant time
Show the answer
C. The fourth-moment bound for an interval near must hold conditioned on the query key's own slot, which uses one more degree of independence
The run through is located relative to , so the analysis fixes and then needs fourth moments of the other keys' counts: 4-wise independence given one more key's value, that is, 5-independence. Pătraşcu and Thorup showed the extra degree is truly needed by exhibiting 4-independent families with expected cost.
Simple tabulation: weak on paper, strong in practice
import random
class SimpleTabulation:
"""32-bit keys as 4 bytes; one table of 256 random 64-bit words per byte."""
def __init__(self):
self.T = [[random.getrandbits(64) for _ in range(256)] for _ in range(4)]
def __call__(self, x):
h = 0
for i in range(4):
h ^= self.T[i][(x >> (8 * i)) & 0xFF]
return h # use the top l bits for 2^l slotsA unique character frees a key. Suppose that in a set of keys, key is the only one with character in position . Then enters and no other hash in ; it is a fresh uniform word, so it makes uniform and independent of the other hashes of :
Three keys always peel. Let be distinct, and pick a position with . If , then is unique there; otherwise , and is unique. Peel that key off. The remaining two keys differ in some position, where both characters are unique. Hence, for all ,
Four keys need not peel. Take characters for position 1 and for position 2, with all other characters equal. In the four keys every table entry appears exactly twice, so the hashes always cancel, and the fourth is determined by the other three:
Think first How can a 3-independent function beat some 4-independent families at linear probing?
-independence is a worst-case promise over every family with that property, and a family built to be bad can place its few dependencies exactly where linear probing is fragile. The proofs for tabulation never use 4-independence. They use its structure: hashes become dependent only when keys share characters in a "paired" pattern such as the rectangle above, and few key sets have many such patterns, so a peeling argument recovers almost the same tail bounds as full randomness.
Fast families and the frontier
The fastest provably universal family is multiply-shift (Dietzfelbinger, Hagerup, Katajainen & Penttonen, 1997): for -bit keys and slots, draw a random odd -bit and keep the top bits of . Any two distinct keys collide with probability at most . Adding a random and computing in bits gives a strongly universal variant (Dietzfelbinger, 1996).
#include <cstdint>
// Multiply-shift: 64-bit key -> l-bit slot (1 <= l <= 63); a is a random odd 64-bit number.
inline uint64_t multiply_shift(uint64_t x, uint64_t a, unsigned l) {
return (a * x) >> (64 - l); // unsigned overflow is the "mod 2^64"
}One multiplication and one shift: unbeatable for chaining and for universal hashing in general. Consecutive keys land on an almost arithmetic progression of slots, the kind of structure that hurts linear probing.
Why do weak hash functions usually work anyway? Mitzenmacher and Vadhan (2008) showed that if the keys themselves carry enough randomness (entropy), even a 2-universal function behaves close to a truly random one. Real data is often random enough; adversarial and highly regular data are exactly where weak functions break. Much of practice runs on fast non-cryptographic functions such as MurmurHash and xxHash, which come with no theorem at all.
Key takeaways
- A -independent family makes any distinct keys land in independent uniform slots; a random polynomial of degree over a prime field achieves it with a seed of words.
- Chaining needs only pairwise independence for expected lookups, but then the longest chain can reach ; -independence brings it down to the fully random .
- Linear probing needs 5-independence for expected operations (Pagh, Pagh & Ružić), and some 4-independent families give (Pătraşcu & Thorup), as does multiply-shift on dense keys.
- Simple tabulation is only 3-independent, yet it gives linear probing expected time, Chernoff-style bucket loads and cuckoo hashing that fails with probability .
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
You hash with , the independent and uniform in , and . Up to the small rounding of the final , what is the largest for which is -independent?
- 3
- 4
- 5
Show the answer
B. 4
A random polynomial of degree is -independent: any distinct points determine it by interpolation. A degree-3 polynomial with four random coefficients is 4-independent, and not 5-independent, since its values on 4 keys determine its value on a fifth.
With simple tabulation on keys of two characters, , which statement about the keys , , , is true?
- Their four hashes are independent and uniform
- Their hashes always XOR to zero, so no three of them can be independent
- Any three of their hashes are independent, but the fourth is determined by the other three
- They always collide in pairs:
Show the answer
C. Any three of their hashes are independent, but the fourth is determined by the other three
Each table entry appears in exactly two of the four hashes, so their XOR is 0 and is fixed by the other three. Simple tabulation is 3-independent: among three distinct keys, some key always has a character unique in its position, which makes its hash independent of the others.
Which family guarantees expected time per operation for linear probing at load factor , on every key set?
- Multiply-shift, with odd
- A random degree-3 polynomial over (4-independent)
- The Carter–Wegman family
- A random degree-4 polynomial over (5-independent)
Show the answer
D. A random degree-4 polynomial over (5-independent)
Pagh, Pagh and Ružić proved that 5-independence suffices; Pătraşcu and Thorup showed that some 4-independent families give expected time on some key sets, so five is exactly the threshold for independence alone. Multiply-shift and Carter–Wegman are only about 2-independent.
You hash keys into slots with chaining. Which guarantee on the longest chain holds for every pairwise independent family?
- with constant probability, and no better in general
- with high probability
- in expectation
- with high probability
Show the answer
A. with constant probability, and no better in general
Counting colliding pairs gives a longest chain below with probability at least , and Alon et al. exhibited universal families (linear maps over a field of square order) with a bucket of keys on some inputs. needs about that much independence, or a cleverer family such as simple tabulation.
Further reading
- M. N. Wegman & J. L. Carter, "New hash functions and their use in authentication and set equality," Journal of Computer and System Sciences 22(3) (1981), 265–279.
- A. Pagh, R. Pagh & M. Ružić, "Linear probing with constant independence," SIAM Journal on Computing 39(3) (2009), 1107–1120 (STOC 2007).
- M. Pătraşcu & M. Thorup, "On the k-independence required by linear probing and minwise independence," ICALP (2010); journal version in ACM Transactions on Algorithms (2016).
- M. Pătraşcu & M. Thorup, "The power of simple tabulation hashing," Journal of the ACM 59(3) (2012), article 14 (STOC 2011).
- M. Thorup, "High speed hashing for integers and strings," lecture notes, arXiv:1504.06804 (2015).