Sub topic 4
Hash functions as random orderings
This chapter assumes you know what a hash function is, how to read short Python programs, and the basic idea of a random permutation (a shuffled list). The next chapter uses the idea developed here to define the MinHash value.
Why orderings matter
MinHash is built on one operation: look at a set and pick its first element under a random ordering of all possible elements. Different random orderings pick different elements, and how often two sets agree on the pick tells us how similar they are. This chapter covers the ordering part. It does not cover the "pick the first element" part or the matching probability.
An ordering of a universe U (every element that could ever appear in a set) is a rule that ranks the elements from first to last. A random permutation is an ordering chosen uniformly from all possible orderings.
Orderings as permutations
Take a universe of four elements: a, b, c, d. One permutation ranks them c, a, d, b. Another ranks them b, d, a, c. There are 4! = 24 possible orderings. For a universe of n elements there are n! orderings.
Each ordering is a different way of asking "which element comes first?". Applying an ordering to a set means ranking only the set's members in that order. The ordering is defined on the whole universe, so every set sees the same ranking.
That shared ranking is the property we need. If two sets both contain an element, that element has the same rank in both.
Why we cannot store permutations
The direct approach is to generate a random permutation and keep it in memory. This fails at scale. If elements are 64-bit identifiers, the universe has 2^64 elements. Storing one permutation would need an array of that size. A MinHash scheme needs many permutations, so this is impossible.
We need something that behaves like a random ordering but takes almost no space.
A hash function as an ordering
A hash function h maps each element to a number. Sort the elements by their hash values, and you get an ordering:
- Smaller hash value means earlier in the ordering.
- Two elements with the same hash value are tied (a collision).
- The same function always gives the same ordering, so all sets share it.
The ordering is never stored. Computing h(x) for an element tells you where x sits relative to others, and you only need to compare numbers.
Seeing it in code
The next program builds three hash functions of the form (a·x + b) mod P and prints the ordering each one gives to the same five words. The a and b values come from a seeded random generator, so the output is repeatable.
import random
P = (1 << 61) - 1 # a Mersenne prime
def make_hash(rng):
a = rng.randrange(1, P)
b = rng.randrange(0, P)
return lambda x: (a * x + b) % P
universe = ["apple", "banana", "cherry", "date", "elder"]
ids = {word: i for i, word in enumerate(universe)}
rng = random.Random(1)
for _ in range(3):
h = make_hash(rng)
order = sorted(universe, key=lambda w: h(ids[w]))
print(order)Each line is a different ordering of the same five words. Each hash function acts as one random permutation. The program uses small integer ids to keep the example short. In practice you convert each element to an integer first, for example with zlib.crc32, as the implementation chapter does.
How good is the approximation?
A hash function is only an approximation of a random permutation. Three differences matter.
- Not every permutation is reachable. The family (a·x + b) mod P has roughly P² members. A universe of n elements has n! permutations, which is far more for any large n. The family covers a tiny fraction of them.
- Ties can happen. If h(x) = h(y) for two different elements, the ordering does not rank them. Using a large P makes this rare, but it is still possible. When the input ids are already 32-bit values, reducing modulo a 61-bit prime keeps the map one-to-one for the multiplier steps in a practical sense, because a is nonzero modulo a prime.
- The ideal property is only approximate. The theory wants every element of a set to be equally likely to come first. Universal hash families give this closely enough for the error bounds in later chapters, but not exactly.
The cost of these differences is small in practice. Production MinHash systems rely on such families.
What to carry forward
- A permutation of the universe is an ordering of its elements.
- One hash function stands in for one random permutation.
- Many independent hash functions stand in for many independent permutations.
- All sets share each ordering, so a shared element has the same rank everywhere.
The next chapter uses this: for a given ordering, the MinHash value of a set is the member that comes first.
Exercise
A universe has 6 elements. How many distinct orderings (permutations) exist, and why is it impractical to store one random permutation when the universe has 2^64 elements?
Show answer
There are 6! = 720 orderings. For a universe of 2^64 elements, one stored permutation would need an array with 2^64 entries, which is far too large for memory. A hash function replaces the stored permutation with a few numbers (a and b) and a short formula that is computed on demand.
Exercise
Why must two sets being compared use the same hash functions, with the same a and b values?
Show answer
Each hash function defines one ordering of the universe. If the two sets were ranked by different functions, a shared element could have different ranks in each, and the results could not be compared. Using the same functions gives every element one rank for both sets.