Hash tables, functions, collision resolution, and applications
Section 2 of 4
Deep dive into different hash function implementations and their characteristics
h(k) = k mod mThe division method is the simplest hash function. It takes the key value and returns the remainder when divided by the table size.
Prime numbers help distribute keys more evenly and reduce clustering.
Powers of 2 can lead to poor distribution for certain key patterns.
Analyze your key patterns to choose appropriate table sizes.
h(k) = ⌊m(kA mod 1)⌋
where A ≈ 0.618 (golden ratio)The multiplication method multiplies the key by a constant A (0 < A < 1), extracts the fractional part, and multiplies by the table size.
h(k) = ((ak + b) mod p) mod m
where a, b are random, p is primeUniversal hashing uses a family of hash functions with random coefficients, providing theoretical guarantees against worst-case behavior.
Choose random values for a and b at hash table creation time.
Select prime p larger than the universe of possible keys.
Even with collisions, expected performance remains optimal.
| Method | Computation | Distribution | Best Use Case |
|---|---|---|---|
| Division | Fastest | Variable | Simple applications with prime table sizes |
| Multiplication | Medium | Good | General purpose with flexible table sizes |
| Universal | Slowest | Guaranteed | Security-critical or adversarial environments |