Sub topic 2
Why exact comparison does not scale
This chapter assumes you know basic set operations (union and intersection), can read short Python programs, and have met Jaccard similarity in the previous chapter. You will see how the cost of comparing every pair of sets grows, and which parts of that cost MinHash is designed to remove.
The task
Suppose you have a collection of n documents. Each document has been turned into a set of shingles (small pieces of text, covered in a later chapter). You want every pair of documents whose Jaccard similarity is above some threshold, for example near-duplicate web pages or plagiarized essays.
The direct method is to compute the Jaccard similarity of every pair and keep the pairs above the threshold. This gives exact answers. It also becomes impractical quickly, for two separate reasons.
Cost 1: the number of pairs grows quadratically
A collection of n sets has n(n-1)/2 distinct pairs. Doubling the number of documents roughly quadruples the number of pairs.
| Documents | Pairs |
|---|---|
| 1,000 | 499,500 |
| 100,000 | about 5 billion |
| 1,000,000 | about 500 billion |
A million documents is a small collection by web standards. Search engines deal with billions of pages.
Cost 2: each comparison touches the whole sets
Computing len(a & b) / len(a | b) takes time proportional to the sizes of the sets. A document of 5,000 words produces about 5,000 three-word shingles, so each comparison costs thousands of operations. The total cost is roughly the number of pairs times the set size.
The following program estimates this cost. It assumes a single core can do 100 million basic set operations per second, which is an optimistic round number.
def pair_count(n):
return n * (n - 1) // 2
def brute_force_seconds(n, set_size, ops_per_second=1e8):
return pair_count(n) * set_size / ops_per_second
for n in (1_000, 100_000, 1_000_000):
seconds = brute_force_seconds(n, set_size=1000)
print(f"{n:>9,} docs {pair_count(n):>15,} pairs "
f"{seconds:>12,.1f} s {seconds / 86400:>7.2f} days")With 1,000 shingles per document, 1,000 documents take about 5 seconds. 100,000 documents take about 50,000 seconds, or 0.58 days. One million documents take about 5 million seconds, nearly 58 days on one core. Spreading the work over many machines shortens the wall-clock time, but the total work stays the same.
Cost 3: memory
Exact comparison needs every set available. One million documents with 1,000 shingles each, stored as 64-bit hash values, need at least 8 GB. Python string sets need several times more. If the sets do not fit in memory, comparisons require repeated disk or network reads, which is slower still.
Two problems, two techniques
The costs above separate into two problems:
- Each comparison is expensive because sets are large and vary in size.
- There are too many pairs to compare.
MinHash addresses the first problem. It replaces each set with a signature, a list of k numbers with k fixed in advance (often 100 to 500). Comparing two signatures takes k steps regardless of how large the original sets were. Signatures also reduce memory: a million signatures of 128 values at 8 bytes each take about 1 GB.
MinHash alone does not address the second problem. Comparing all pairs of signatures is still quadratic in n. A technique called locality-sensitive hashing (LSH) with banding handles that by only comparing pairs that are likely to be similar. It is covered near the end of this tutorial.
What you give up
Signatures do not recover the exact Jaccard similarity. They give an estimate with a random error that shrinks as k grows. For tasks like near-duplicate detection, an answer such as "about 0.5, give or take 0.03" is enough. It lets you rule out most pairs quickly, and you can compute the exact value for the few pairs that remain if you need it.
The next chapters build up to this in order. You first need a way to turn documents into sets, then a way to treat hash functions as random orderings, and then the MinHash value itself and why it carries information about Jaccard similarity.
Exercise
A collection has 10,000 documents. How many distinct pairs must a brute-force comparison examine? If the collection grows to 20,000 documents, by roughly what factor does the number of pairs increase, and why?
Show answer
There are 10,000 × 9,999 / 2 = 49,995,000 pairs. At 20,000 documents there are 20,000 × 19,999 / 2 = 199,990,000 pairs, about 4 times as many. The pair count is proportional to n squared, so doubling n multiplies it by 4.
Exercise
MinHash makes each comparison cost k steps, independent of set size. Why is this not enough on its own to handle a collection of one billion documents?
Show answer
The number of pairs is still about n squared divided by 2, which is around 5 × 10^17 for a billion documents. Even at k steps per pair, comparing every pair of signatures is far too much work. A second technique, LSH banding, is needed to avoid comparing most pairs.