Two documents are near-duplicates when the Jaccard coefficient $J$ of their sets of $k$-word shingles is high. Two twelve-word sentences that differ in two words score only $1/3$ with $k = 3$, since each edit destroys up to $k$ shingles. MinHash estimates $J$ from $m$ minimum hash values: a random ordering puts a shared shingle first with probability exactly $J$, as proved here, so the standard error is $\sqrt{J(1-J)/m}$. Cutting the sketch into $b$ bands of $r$ values makes a pair a candidate with probability $1 - (1 - J^r)^b$; with 20 bands of 10, that is $0.9998$ at $J = 0.9$ and $0.019$ at $J = 0.5$. Simhash bits agree with probability about $1 - \theta/\pi$, for $\theta$ the angle between word vectors. On 2,000 sampled pairs of 82 pages of this site, the largest 4-shingle coefficient was $0.042$.
Two sentences differ in two words:
- $d_1$ = the crawler fetches each page and follows its links to new pages
- $d_2$ = the crawler fetches every page and follows its links to other pages
A reader sees at once that they are nearly the same. A crawler's exact test, which compares hashes of the whole content, sees two unrelated documents. How can a program measure "nearly the same", and how can it do so for billions of pages without comparing every pair? The measure developed below is the Jaccard coefficient of the documents' sets of short word sequences, estimated from small sketches. The way around comparing every pair is to hash the sketches so that similar ones land together, and compare only those.
Near-duplicate detection is the task of finding documents whose content largely repeats other documents. The web needs it because the web repeats itself. The same news story runs on dozens of sites, the same documentation is mirrored on many servers, and the same page is reachable under several URLs. Manning, Raghavan, and Schütze (2008, §19.6) cite estimates that as many as 40 percent of web pages duplicate other pages. Each copy has a cost:
- indexing every copy wastes storage;
- following the links of every copy wastes crawling;
- showing every copy in a result list wastes the searcher's time.
That is why web search removes duplicates before showing results. This article gives the full treatment of the methods, and later articles, such as monitoring websites and result diversification, build on it.
Here is the route. First we see why exact fingerprints miss the two sentences. Then we turn "nearly the same" into a number, the Jaccard coefficient of their shingle sets. Next we estimate that number cheaply with MinHash, and avoid comparing all pairs by banding the estimates. Then we meet simhash, a single short fingerprint that measures similarity by angle. Then we test all of it on this site's own pages, and close with worked problems. The two sentences, $d_1$ and $d_2$, are worked by hand at every step.
Exact duplicates
An exact copy can be found with a fingerprint: a hash of the page's content, such as 64 bits of a cryptographic hash. Two pages with different fingerprints certainly differ. Two pages with the same fingerprint are, with overwhelming probability, identical. The crawler's "content seen?" test keeps the fingerprints of all fetched pages and checks each new page against them.
Exact fingerprints are brittle. A copy that differs only in a date, an advertisement, or a navigation menu gets an entirely different hash. The two example sentences, which share ten of their twelve words, have unrelated fingerprints, so an exact test calls them different documents. Broder, Glassman, Manasse, and Zweig (1997) found many such near-copies in a large web crawl, and exact matching alone misses them. What is needed is a measure that changes a little when the text changes a little.
Shingling
Shingling (Broder, Glassman, Manasse, and Zweig, 1997) compares documents by their overlapping word sequences. The idea in one sentence: two documents that share most of their short phrases share most of their text. The $k$-shingles of a document are its sequences of $k$ consecutive words. Manning, Raghavan, and Schütze (2008, §19.6) illustrate with $k = 4$: the document a rose is a rose is a rose has three distinct 4-shingles, a rose is a, rose is a rose, and is a rose is. Each document is represented by its set of shingles, and two documents are compared with the Jaccard coefficient of their sets:
$$J(S_1, S_2) = \frac{\lvert S_1 \cap S_2 \rvert}{\lvert S_1 \cup S_2 \rvert}.$$
Here $S_1$ and $S_2$ are the two shingle sets: the coefficient is the number of shingles they share divided by the number in either. Identical documents score 1 and documents with no shingles in common score 0. Pages whose coefficient exceeds a threshold, such as 0.9, are treated as near-duplicates.
The running example
Example (ten shared words, a resemblance of one third). The two sentences share 10 of their 12 words, yet the Jaccard coefficient of their sets of 3-shingles is exactly 1/3, far nearer 0.3 than 0.8. Each sentence has twelve words and so ten 3-shingles. Together they have 15 distinct shingles, listed here in alphabetical order and numbered for use in the next section:
| # | Shingle | In $d_1$ | In $d_2$ |
|---|---|---|---|
| 0 | and follows its | yes | yes |
| 1 | crawler fetches each | yes | |
| 2 | crawler fetches every | yes | |
| 3 | each page and | yes | |
| 4 | every page and | yes | |
| 5 | fetches each page | yes | |
| 6 | fetches every page | yes | |
| 7 | follows its links | yes | yes |
| 8 | its links to | yes | yes |
| 9 | links to new | yes | |
| 10 | links to other | yes | |
| 11 | page and follows | yes | yes |
| 12 | the crawler fetches | yes | yes |
| 13 | to new pages | yes | |
| 14 | to other pages | yes |
Five shingles are shared and the union has 15, so $J(d_1, d_2) = 5/15 = 1/3$. The documents share 10 of 12 words, yet their resemblance is only a third.
Why so low? Each changed word destroys every shingle that contains it, which is up to $k$ shingles. With $k = 3$, two changed words remove five of each sentence's ten shingles. On a sentence this short the effect is drastic. On a page of 2,000 words, the same two edits would remove at most 6 of about 2,000 shingles, and the coefficient would stay near 1. Shingling measures how much text two documents share in the same order, and short documents are fragile under it.
The choice of $k$
The length of a shingle sets how strict the test is. A second pair shows the effect. Take $d_3$ = a rose is a rose is a rose, the textbook's sentence, and $d_4$ = a rose is a flower which is a rose, with $k = 3$. The document $d_3$ has 3 distinct shingles and $d_4$ has 7. They share 3 (a rose is, rose is a, is a rose), and their union has 7, so $J = 3/7 \approx 0.43$. With $k = 2$ the coefficient is $3/6 = 0.5$.
Shorter shingles make documents look more alike, since short word sequences are common to many texts. Longer ones make the test stricter. Manning, Raghavan, and Schütze (2008, §19.6) call $k = 4$ a typical value for detecting near-duplicate web pages. Problem 1 below recomputes the running example with $k = 2$ and $k = 4$.
Estimating resemblance with MinHash
The Jaccard coefficient is the right measure, but computing it exactly means comparing the full shingle sets of every pair of pages. For billions of pages that is out of the question. Broder (1997) showed how to estimate the coefficient from a small, fixed-size sketch of each document.
Here is the idea in one sentence: shuffle all the shingles at random, and the chance that both documents have the same shingle first is exactly their Jaccard coefficient. A hash function does the shuffling.
The formal version. Let $h$ be a random reordering of all possible shingles: a function that gives every shingle a distinct value, with each of the possible orderings equally likely. Apply $h$ to every shingle of both documents and take the minimum value of each set. The probability that the two minimums are equal is exactly the Jaccard coefficient:
$$P\left[\min_{s \in S_1} h(s) = \min_{s \in S_2} h(s)\right] = J(S_1, S_2).$$
This is Theorem 19.1 of the course text (Manning, Raghavan, and Schütze, 2008, §19.6), due to Broder (1997). The proof takes three steps.
- Every shingle of the union is equally likely to come first. Let $U = S_1 \cup S_2$, with $n$ shingles, and let $s^*$ be the shingle of $U$ with the smallest value. Take any two shingles $u$ and $v$ of $U$. Exchanging the values of $u$ and $v$ turns each ordering in which $u$ is first in $U$ into exactly one ordering in which $v$ is first in $U$, and back again. The two events therefore contain the same number of orderings, and all orderings are equally likely, so $P[s^* = u] = P[s^* = v]$. The $n$ probabilities are equal and sum to 1, so each is $1/n$. Shingles outside $U$ play no part.
- If $s^*$ is in both sets, the minimums are equal. Each set is contained in $U$, so no shingle of either set has a smaller value than $s^*$. Since $s^*$ belongs to both, it is the minimum of each.
- If $s^*$ is in only one set, the minimums differ. Say $s^*$ is in $S_1$ and not in $S_2$. Then the minimum of $S_1$ is $h(s^*)$. The minimum of $S_2$ is the value of some other shingle, and values are distinct, so it is not $h(s^*)$. If instead $s^*$ is in $S_2$ and not in $S_1$, the minimum of $S_2$ is $h(s^*)$ and the minimum of $S_1$ is the value of some other shingle, so again the two differ.
So the minimums agree exactly when $s^*$ falls in the intersection. By step 1 that has probability
$$\frac{\lvert S_1 \cap S_2 \rvert}{n} = \frac{\lvert S_1 \cap S_2 \rvert}{\lvert S_1 \cup S_2 \rvert} = J(S_1, S_2).$$
In the running example, $s^*$ is one of 15 shingles, and the minimums agree when it is one of the 5 shared ones. The script confirms the counting on a case small enough to enumerate: for the sets $\{0, 1, 2\}$ and $\{1, 2, 3, 4\}$, the minimums agree in 48 of the $5! = 120$ orderings of five elements, and $48/120 = 2/5$ is their Jaccard coefficient.
The theorem is about a uniformly random ordering in which every shingle has a distinct value, and step 3 uses that distinctness. A practical hash function only approximates such an ordering, in two ways. Its values are not ordered exactly at random, so the agreement probability is only close to $J$. And two different shingles can receive the same value, which makes the minimums agree although the shingles differ, and so pushes the estimate up. With 64-bit values that is rare: among $n$ shingles the expected number of colliding pairs is about $n^2/2^{65}$, which for the $10^4$ shingles of two long pages is about $3 \times 10^{-12}$, negligible next to the sampling error. The course text first hashes each shingle to 64 bits and applies the random permutation to those values (Manning, Raghavan, and Schütze, 2008, §19.6), so strictly its theorem concerns the sets of hashed values.
MinHash by hand
To see the procedure on real numbers, number the 15 shingles as in the table. Use hash functions of the form $h(x) = (a x + b) \bmod 17$, where $x$ is the shingle's number and $a$ and $b$ are constants. Because 17 is prime and $a$ is not a multiple of it, each such function sends the 15 numbers to 15 different values. It therefore acts as a reordering of the shingles. Six functions give:
| $a$ | $b$ | Min over $d_1$ (shingle) | Min over $d_2$ (shingle) | Agree? |
|---|---|---|---|---|
| 1 | 0 | 0 (0) | 0 (0) | yes |
| 3 | 5 | 3 (5) | 0 (4) | no |
| 5 | 2 | 0 (3) | 1 (10) | no |
| 7 | 11 | 0 (13) | 2 (6) | no |
| 11 | 3 | 0 (9) | 1 (6) | no |
| 13 | 8 | 4 (1) | 0 (2) | no |
For the second row, $h(4) = (3 \cdot 4 + 5) \bmod 17 = 0$, so shingle 4, every page and, has the smallest value in the union. It belongs only to $d_2$, so $d_2$'s minimum is 0, while $d_1$'s smallest value is 3, from shingle 5. The first row is the identity ordering: shingle 0 is shared, so both minimums are 0.
The six functions agree once, an estimate of $1/6 \approx 0.17$ for a true value of $1/3$. That is not a flaw in the argument. Each function gives one yes-or-no answer, and six coin-like answers cannot pin down a probability. Over the whole family, all $16 \times 17 = 272$ choices of $a$ from 1 to 16 and $b$ from 0 to 16, the minimums agree 91 times, a fraction of $0.335$. That is close to $1/3$ and not equal to it. The theorem promises exactly $1/3$ only for the set of all orderings, and this family is a small part of that set. The next section shows how many functions are enough.
How many hash functions
With $m$ independent hash functions, the sketch of a document is its list of $m$ minimum values. The estimate of $J$ is the fraction of positions where two sketches agree. Each position agrees with probability $J$, independently of the others. So the number of agreements has a binomial distribution with $m$ trials and success probability $J$. Its mean is $mJ$ and its variance is $mJ(1-J)$. Dividing by $m$, the estimate has mean $J$, so it is unbiased, and standard error
$$\sqrt{\frac{J(1-J)}{m}}.$$
Example (fifty values or a hundred). For the running example, a sketch of 50 values is not enough to estimate $J = 1/3$ with a standard error below 0.05, and a sketch of 100 is, just. With 50 the standard error is $\sqrt{(1/3)(2/3)/50} = 0.067$, and with 100 it is 0.047. For comparison, $m = 10$ gives 0.149 and $m = 200$ gives 0.033 (all rounded to three places). Because the error shrinks with $\sqrt{m}$, halving it takes four times as many hash functions. The figure shows 30 independent runs of the estimate, each with its own random hash functions, as $m$ grows.
At $m = 200$, the median estimate over the 30 runs was 0.333 and the middle half of the runs lay between 0.315 and 0.345. Manning, Raghavan, and Schütze (2008, §19.6) use sketches of 200 values. A page of thousands of words is reduced to 200 numbers, and comparing two pages costs 200 comparisons regardless of their length. The sketch size depends on the precision wanted, not on the length of the documents.
Avoiding all pairs
Sketches make each comparison cheap, but there are still too many comparisons. Comparing all pairs is quadratic: a billion pages make about $5 \times 10^{17}$ pairs. Large systems avoid it by grouping sketches that share values, so that only pages likely to be similar are compared at all. The same obstacle arises for vectors, where Nearest Neighbor Search in High Dimensions shows that a full scan is the exact baseline and that tree indexes examine nearly every vector in a hundred dimensions. Hashing groups of sketch positions together is a form of locality-sensitive hashing.
One common scheme, the banding technique, cuts the sketch of $m$ values into $b$ bands of $r$ values each, with $m = br$. Each band is hashed as a whole, and pages whose bands hash to the same value are put in the same bucket. Two pages become a candidate pair if they match in every value of at least one band, and only candidate pairs are compared. The scheme and its analysis are set out by Leskovec, Rajaraman, and Ullman (2020, §3.4), and the general idea of hashing so that near items collide goes back to Indyk and Motwani (1998). Locality-sensitive hashing states their definition, proves their bound on the query time of such an index, and measures the index on word vectors with the random hyperplanes of the simhash section below.
Here is the idea of the analysis: a band is strict, since all of its values must agree, and the set of bands is lenient, since any one of them will do. Strictness suppresses dissimilar pairs, and leniency rescues similar ones.
The formal version. Let $s$ be the Jaccard coefficient of a pair of pages. By the theorem above, their sketches agree in any one position with probability $s$, and the $m$ positions use independent hash functions, so the positions agree or disagree independently of each other.
- A given band matches when all $r$ of its positions agree. By independence that has probability $s \cdot s \cdots s = s^r$.
- The band therefore fails to match with probability $1 - s^r$.
- Different bands use different positions, so they fail independently, and all $b$ bands fail with probability $(1 - s^r)^b$.
- The pair is a candidate when not all bands fail, which has probability
$$f(s) = 1 - (1 - s^r)^b.$$
The script confirms the formula by enumeration for $b = 2$ bands of $r = 2$: adding up the probabilities of the agreement patterns in which the first two or the last two positions all agree gives $1 - (1 - s^2)^2$.
With a 200-value sketch in 20 bands of 10, a pair with $J = 0.9$ is compared with probability $1 - (1 - 0.9^{10})^{20} = 0.9998$ and a pair with $J = 0.5$ with probability 0.019. The running example's pair, at $J = 1/3$, is compared with probability 0.0003, so it would almost never be examined.
Where the curve rises. The function $f$ increases from 0 at $s = 0$ to 1 at $s = 1$, and its graph is S-shaped: flat, then steep, then flat. Three numbers locate the steep part, and they lie close together.
- The approximation $(1/b)^{1/r}$. For any $x$ between 0 and 1, $(1 - x)^b \ge 1 - bx$ (Bernoulli's inequality), so $f(s) \le b\,s^r$. A pair is a candidate with probability at most $b\,s^r$, which is small as long as $s^r$ is well below $1/b$, that is, as long as $s$ is well below $(1/b)^{1/r}$. At $s = (1/b)^{1/r}$ itself, $s^r = 1/b$ and $f = 1 - (1 - 1/b)^b$, which is $0.642$ for $b = 20$ and approaches $1 - 1/e = 0.632$ as $b$ grows. So $(1/b)^{1/r}$ is the similarity at which a pair has a little under a two-in-three chance of being compared. This is the approximation Leskovec, Rajaraman, and Ullman give for the threshold (2020, §3.4.2).
- The halfway point. Solving $f(s) = 1/2$ gives $(1 - s^r)^b = 1/2$, so $s^r = 1 - 2^{-1/b}$ and $s = (1 - 2^{-1/b})^{1/r}$.
- The steepest point. The slope is $f'(s) = b\,r\,s^{r-1}(1 - s^r)^{b-1}$. Setting the derivative of $\ln f'(s) = \ln(br) + (r-1)\ln s + (b-1)\ln(1 - s^r)$ to zero gives $(r-1)/s = (b-1)\,r\,s^{r-1}/(1 - s^r)$, that is $(r-1)(1 - s^r) = (b-1)\,r\,s^r$, so $s^r = (r-1)/(br-1)$.
| Bands $b$ × values $r$ | $(1/b)^{1/r}$ | Halfway point | Steepest point |
|---|---|---|---|
| 40 × 5 | 0.478 | 0.444 | 0.458 |
| 20 × 10 | 0.741 | 0.713 | 0.734 |
| 10 × 20 | 0.891 | 0.874 | 0.889 |
The choice of $b$ and $r$ thus moves the threshold at which pairs start being compared.
Example (twenty bands of ten or forty of five). A system that wants to catch pairs sharing about half their shingles, $J = 0.5$, with the same 200 values should use 40 bands of 5, not 20 bands of 10. Shorter bands are easier to match in full, and there are more chances to match one. Such a pair is compared with probability 0.72 under 40 bands of 5, against 0.019 under 20 bands of 10. The price is more candidate pairs at low similarity: even the running example's pair, at $J = 1/3$, is compared with probability 0.15.
From candidate pairs to groups. Candidate pairs are checked against the full sketches, and the pairs that pass a threshold must still be turned into groups of pages, of which one member is kept. The course text does this with a union-find structure: each page starts in a set of its own, and every confirmed pair merges the sets of its two pages, so that pages linked by a chain of similar pairs end in one cluster (Manning, Raghavan, and Schütze, 2008, §19.6). It also describes a second way to cut down the pairs examined, without naming its source. The method comes from Broder, Glassman, Manasse, and Zweig (1997, §5.3). The values of each sketch are sorted and then shingled in turn, giving super-shingles, and the overlap of two sketches is computed only for pages that share a super-shingle. The course text presents this as a heuristic and gives no analysis of how often it misses a similar pair.
Simhash
MinHash needs a sketch of 200 values per page. One short fingerprint can do a similar job, with a coarser measure of similarity. That fingerprint is simhash (Charikar, 2002), which maps each document's weighted features to a single short bit string. It does so in such a way that documents with high cosine similarity get strings that differ in few bits. Manku, Jain, and Das Sarma (2007, §2) describe the computation for web pages:
- Extract the page's features, such as its words, each with a weight, such as its count.
- Hash each feature to an $f$-bit value, with $f = 64$ for example.
- Keep $f$ running totals, all starting at zero. For each feature and each bit position $i$, add the feature's weight to total $i$ if bit $i$ of the feature's hash is 1, and subtract it if the bit is 0.
- Bit $i$ of the simhash is 1 if total $i$ is positive, and 0 otherwise.
Each feature votes on every bit, with a strength equal to its weight. Two documents that share most of their features share most of the votes, so most of their totals have the same sign.
Simhash by hand
Back to $d_1$. Take its words as features, each with weight 1, and hash each of its 12 words to 64 bits by taking the first 64 bits of its MD5 hash. The first 8 bits of each word's hash, and the resulting totals, are:
| Word | Bits 1 to 8 |
|---|---|
| and | 10111110 |
| crawler | 11001101 |
| each | 10010011 |
| fetches | 11111110 |
| follows | 11010011 |
| its | 01011000 |
| links | 10000000 |
| new | 00100010 |
| page | 01110001 |
| pages | 10110011 |
| the | 10001111 |
| to | 00000001 |
| total | +4 −2 −2 +2 −2 −4 +2 +2 |
| simhash bits | 1 0 0 1 0 0 1 1 |
In the first position, 8 of the 12 words have a 1 and 4 have a 0, so the total is $8 - 4 = +4$ and the bit is 1. In the second, 5 words have a 1 and 7 a 0, so the total is $-2$ and the bit is 0. The same procedure over all 64 bits gives $d_1$ the fingerprint 93264c674404019d in hexadecimal and $d_2$ the fingerprint 91060e274484039d.
Bits that agree, and angles
The two fingerprints differ in 7 of their 64 bits. That is few: unrelated documents differ in about 32. Charikar (2002, §3) showed why the number of differing bits measures similarity, and what to compare it with.
The picture is a random plane through the origin. Two document vectors make an angle $\theta$. A random plane passes between them with probability $\theta / \pi$, and only then do they fall on opposite sides of it. If a bit is computed as the sign of the projection of a document's feature vector onto a random direction, the bit therefore agrees with probability
$$P[\text{bit agrees}] = 1 - \frac{\theta}{\pi}.$$
The argument in full. Let $u$ and $v$ be the two document vectors, at an angle $\theta$ between 0 and $\pi$, and let $w$ be a random direction whose coordinates are independent standard normal variables. The bit of $u$ is 1 when $w \cdot u > 0$, and likewise for $v$.
- Only the part of $w$ that lies in the plane spanned by $u$ and $v$ matters, because the part perpendicular to that plane contributes nothing to $w \cdot u$ or $w \cdot v$.
- The distribution of $w$ is the same in every direction: its density depends only on the length of $w$, so rotating the space does not change it. The same is true of its part in the plane. The direction $\varphi$ of that part is therefore uniform over the full circle of $2\pi$.
- Measure angles in the plane from $u$, so that $u$ is at angle 0 and $v$ at angle $\theta$. Then $w \cdot u > 0$ exactly when $\varphi$ is within a quarter turn of $u$, that is, between $-\pi/2$ and $\pi/2$. And $w \cdot v > 0$ exactly when $\varphi$ is between $\theta - \pi/2$ and $\theta + \pi/2$.
- The two bits differ when $\varphi$ lies in one of these half circles and not the other. That happens on two arcs, from $-\pi/2$ to $\theta - \pi/2$ and from $\pi/2$ to $\theta + \pi/2$, each of length $\theta$.
The bits differ with probability $2\theta / 2\pi = \theta/\pi$ and agree with probability $1 - \theta/\pi$. Leskovec, Rajaraman, and Ullman (2020, §3.7.2) give the same argument with a figure.
Hashing each feature to $\pm 1$ in every bit position, as step 3 of the simhash computation does, is projection onto a random direction whose coordinates are $\pm 1$ instead of normally distributed. The distribution of such a direction is not unchanged by rotations, so step 2 of the argument fails and the formula holds only approximately (Leskovec, Rajaraman, and Ullman, 2020, §3.7.3, give a four-dimensional example where the two differ). For the running example the exact value can be computed. The two sentences use 14 distinct words, so there are $2^{14} = 16{,}384$ ways to assign $\pm 1$ to them. Counting a zero total as bit 0, as step 4 does, the bits of $d_1$ and $d_2$ agree in 13,612 of them, a probability of $0.831$. The formula gives $0.814$ for the same pair, as computed next.
Now the check on the example. The word-count vectors of $d_1$ and $d_2$ share 10 of their 12 words, each with count 1. Their cosine similarity is $10/12 = 0.833$, and the angle between them is 33.6 degrees. The formula predicts that $1 - 33.6/180 = 0.814$ of the bits agree. The observed fraction, $57/64 = 0.891$, is within the variation expected from 64 random bits, whose standard deviation here is about 0.049. It is 1.6 standard deviations above the formula's value, and 1.3 above the exact value of $0.831$ for $\pm 1$ votes, whose standard deviation is 0.047.
Example (documents with no words in common). The sentence $d_3$ (a rose is a rose is a rose) shares no words with $d_1$, and about half of the 64 bits of their fingerprints, 32, should agree. With no words in common, the vectors are at 90 degrees, and $1 - 90/180 = 0.5$. A reader might expect unrelated documents to agree in almost no bits, but disagreement in every bit would mean opposite vectors, not unrelated ones. The actual fingerprints differ in 37 bits, so 27 of 64 agree, 0.42.
Manku, Jain, and Das Sarma (2007) used 64-bit simhash fingerprints to detect near-duplicates among 8 billion web pages. They treated two pages as near-duplicates if their fingerprints differed in at most 3 bit positions. By the formula above, 3 of 64 bits are expected to differ when the angle is $3/64 \times 180 \approx 8.4$ degrees, a cosine similarity near 0.99. The threshold is aimed at pages that are very similar indeed.
It is not a sharp cut at that angle. With only 64 bits, pages somewhat further apart sometimes fall within 3 bits, and pages somewhat closer sometimes do not. A single 64-bit fingerprint per page is far more compact than a sketch of 200 values, at the cost of a coarser measure of similarity.
Finding fingerprints that differ in a few bits
Banding answered the all-pairs problem for sketches. Simhash needs its own answer: given a fingerprint, find every stored fingerprint that differs from it in at most $k$ bits, without scanning all of them. Manku, Jain, and Das Sarma (2007, §3) build it on a counting argument.
Cut the 64 bits into $k + 1$ blocks. If two fingerprints differ in at most $k$ bits, the differing bits fall in at most $k$ of the blocks, so at least one block is identical in both. For $k = 3$, cut the fingerprint into four blocks of 16 bits. Any fingerprint within 3 bits of the query agrees with it exactly on at least one of the four blocks.
That turns a search for near matches into four searches for exact matches. Keep four copies of the table of fingerprints. In copy $i$, block $i$ is moved to the front and the copy is sorted. For a query, look up in each copy the stored fingerprints whose leading block equals the query's, which a sorted table finds by binary search, and compare each of those with the query in full. Nothing within 3 bits is missed, by the counting argument. Among 8 billion random fingerprints about $8 \times 10^9 / 2^{16} \approx 122{,}000$ share a given 16-bit block, so each of the four probes examines that many instead of 8 billion. Manku and colleagues describe this design and others that keep more tables so as to match on more leading bits, which leaves fewer fingerprints to check at each probe. They round 8 billion to $2^{34}$ (which is about 17 billion), and so quote at most $2^{34-16} = 2^{18}$, about 262,000, fingerprints per probe for this four-table design (2007, §3.1.1). The script also runs the method against a brute-force search on 3,300 fingerprints: the four lookups examine 212 candidates and find the same 136 fingerprints within 3 bits of the query.
Example (blocks of the two fingerprints). The fingerprints of $d_1$ and $d_2$ differ in 7 bits, so cut into 8 blocks of 8 bits they must agree on at least one block, and they agree on two. Written in bytes they are 93 26 4c 67 44 04 01 9d and 91 06 0e 27 44 84 03 9d, and the fifth and eighth bytes are equal. Cut into four blocks of 16 bits they agree on none, which is consistent with the argument: it promises a shared block only for fingerprints at most 3 bits apart.
Shingles or simhash
The two methods measure different things. The Jaccard coefficient of shingle sets is sensitive to word order and to every edit, as the running example showed. Simhash over words ignores order, and a short edit to a long page moves the cosine similarity only slightly. A page whose paragraphs have been rearranged keeps nearly the same simhash. Its shingle set, by contrast, changes at every boundary between moved paragraphs.
Duplicates on this site
The two sentences were built to be similar. What do the measures say about real pages that were never meant to be? The pages fetched in the crawl of this site were tested the same way. No two of the 82 pages, crawled before this topic was written, had the same exact fingerprint. For 2,000 randomly sampled pairs of pages, out of 3,321 possible, the 4-shingle Jaccard coefficient was:
| Text compared | Mean | Maximum |
|---|---|---|
| Whole page, including navigation and footer | 0.007 | 0.042 |
| Main content only | 0.005 | 0.036 |
The site has no near-duplicates, as expected for a set of distinct articles. The most similar sampled pair was two topic pages, Boolean Retrieval and Text Processing and Indexing, which share structural phrases such as their section headings. Their exact coefficient on main content was 0.036. A 200-value MinHash sketch estimated it as 0.045, within the expected error of about 0.013.
The navigation and footer raised the similarity of pairs only slightly on average, because they are short compared with the articles. Other sites are not built that way. On pages with long menus, sidebars and footers and short main content, such as product listings, shared boilerplate can make unrelated pages look alike. Extracting the main content before fingerprinting is then important. It is also the basis of the change detection in monitoring websites.
The worked examples above are computed by tools/experiments/crawling/nearduplicate_math.py in the site repository, and the site measurements by sitecrawl.py in the same folder.
Worked problems
Problem 2 is Exercise 19.8 of the course text (Manning, Raghavan, and Schütze, 2008), restated in our words. The others are posed on the running example and on the formulas of this article. The numbers are checked in nearduplicate_math.py.
Problem 1 (other shingle lengths). Compute the Jaccard coefficient of $d_1$ and $d_2$ for shingles of 1, 2 and 4 words.
The sentences have 12 words each and differ in the 4th word (each, every) and the 11th (new, other). A sentence of 12 words has $12 - k + 1$ shingles of $k$ words, and none repeats here. A shingle is shared exactly when it contains neither changed word.
- $k = 1$. The shingles are the words. Ten are shared, and the union has $12 + 12 - 10 = 14$, so $J = 10/14 = 0.714$.
- $k = 2$. Each sentence has 11 shingles. The 4th word lies in two of them (words 3 to 4 and 4 to 5) and the 11th in two (words 10 to 11 and 11 to 12). That leaves $11 - 4 = 7$ shared, a union of $11 + 11 - 7 = 15$, and $J = 7/15 = 0.467$.
- $k = 4$. Each sentence has 9 shingles. The 4th word lies in four of them (those starting at words 1, 2, 3 and 4). The 11th lies in only two (those starting at words 8 and 9), because no shingle starts after word 9. That leaves $9 - 6 = 3$ shared, a union of $9 + 9 - 3 = 15$, and $J = 3/15 = 0.2$.
With the article's value $1/3$ for $k = 3$, the coefficient falls from 0.714 to 0.467 to 0.333 to 0.2 as $k$ goes from 1 to 4. Longer shingles make the same two edits count for more.
Problem 2 (Exercise 19.8). Search engines $A$ and $B$ each crawl a random subset of the web, of the same size. Some pages are duplicates: exact copies at different URLs, and a duplicated page has exactly two copies. Duplicates are spread uniformly through the crawled pages. $A$ indexes every page it crawled. $B$ indexes only one copy of each duplicated page. If 45 percent of $A$'s indexed URLs are in $B$'s index, and 50 percent of $B$'s indexed URLs are in $A$'s index, what fraction of the web consists of pages without a duplicate?
Write $\lvert A \rvert$ and $\lvert B \rvert$ for the sizes of the two indexes. The URLs that are in both indexes can be counted from either side, and the two counts are the same set:
$$0.45\,\lvert A \rvert = 0.50\,\lvert B \rvert, \qquad \text{so} \qquad \lvert B \rvert = 0.9\,\lvert A \rvert.$$
Both engines crawled the same number of URLs, and $A$ indexed all of its crawl, so $B$'s index is 0.9 of its own crawl. $B$ discarded one tenth of its crawl, one URL for each duplicated page of which it held both copies.
The exercise's statement is loose at this point. It does not say whether a crawl that holds one copy of a duplicated page also holds the other, and the answer depends on it. Write $q$ for the fraction of the web's pages that have a duplicate.
Reading 1: the two copies are crawled together. If a crawl holds both copies of a duplicated page or neither, every duplicated pair in $B$'s crawl cost it one URL. The discarded tenth is one copy of each pair, so the pairs make up two tenths of the crawl. The crawl is a random sample of the web, so $q = 0.2$, and 80 percent of the web consists of pages without a duplicate. This is the shortest solution.
Reading 2: every URL is crawled independently. Let each engine crawl a random fraction $p$ of the web's URLs, independently of each other and of which pages are duplicates. A URL in $B$'s index is in $A$'s crawl with probability $p$, and the exercise says that fraction is 0.50, so $p = 0.5$. A URL in $A$'s crawl is in $B$'s crawl with probability $p$, and then it is in $B$'s index unless it is a duplicated page ($q$) whose other copy $B$ also crawled ($p$) and it is the copy $B$ dropped (one half). So
$$0.45 = p\left(1 - \frac{q\,p}{2}\right) = 0.5\left(1 - \frac{q}{4}\right), \qquad q = 0.4,$$
and 60 percent of the web consists of pages without a duplicate. As a check, $B$ drops one URL for each of the $qN/2$ pairs that falls wholly in its crawl, which has probability $p^2$: that is $0.2N \times 0.25 = 0.05N$ URLs from a crawl of $0.5N$, the tenth found above.
A simulation of each reading on a web of 400,000 pages reproduces the exercise's figures: 0.450 and 0.501 with copies crawled together and $q = 0.2$, and 0.451 and 0.501 with independent URLs and $q = 0.4$. Both readings fit the data, so the data alone cannot choose between them. The complete answer is 80 percent if duplicates are always crawled in pairs and 60 percent if each URL is sampled on its own.
Problem 3 (how large a sketch). How many hash functions are needed to estimate a Jaccard coefficient of 0.9 with a standard error of 0.02? How many for a coefficient of 0.5? What standard errors does the usual sketch of 200 give?
The standard error is $\sqrt{J(1-J)/m}$. Requiring it to be at most $e$ and solving for $m$ gives $m \ge J(1-J)/e^2$.
- For $J = 0.9$: $m \ge 0.9 \times 0.1 / 0.0004 = 225$.
- For $J = 0.5$: $m \ge 0.5 \times 0.5 / 0.0004 = 625$.
With $m = 200$ the standard errors are $\sqrt{0.09/200} = 0.021$ at $J = 0.9$ and $\sqrt{0.25/200} = 0.035$ at $J = 0.5$. The product $J(1-J)$ is largest at $J = 1/2$, so a sketch is least precise for pairs that share half their shingles and most precise near 0 and 1. Near-duplicates have $J$ close to 1, where 200 values go furthest.
Problem 4 (choosing the bands). A system has sketches of 200 values and wants to compare nearly all pairs with $J \ge 0.8$ and few pairs with $J \le 0.5$. Compare 20 bands of 10, 25 bands of 8 and 10 bands of 20.
Evaluate $f(s) = 1 - (1 - s^r)^b$ for each:
| Bands × values | $(1/b)^{1/r}$ | $f(0.5)$ | $f(0.7)$ | $f(0.8)$ | $f(0.9)$ |
|---|---|---|---|---|---|
| 25 × 8 | 0.669 | 0.093 | 0.773 | 0.990 | 1.000 |
| 20 × 10 | 0.741 | 0.019 | 0.436 | 0.897 | 0.9998 |
| 10 × 20 | 0.891 | 0.000 | 0.008 | 0.109 | 0.726 |
For example, with 25 bands of 8, $0.8^8 = 0.168$, so one band fails with probability $0.832$, all 25 fail with probability $0.832^{25} = 0.010$, and $f(0.8) = 0.990$.
Ten bands of 20 will not do: their threshold is near 0.89, and they compare only 11 percent of the pairs at 0.8. Twenty bands of 10 miss about one pair in ten at 0.8. Twenty-five bands of 8 catch 99 percent of them, at the price of comparing 9 percent of the pairs at 0.5 where twenty bands of 10 compare 2 percent. If missing a near-duplicate is the costlier error, 25 bands of 8 is the choice. The rule is to put the threshold $(1/b)^{1/r}$ below the similarity that must be caught, by enough that the curve has finished rising there.
Problem 5 (blocks for a larger distance). Fingerprints of 64 bits are to be searched for matches within $k = 7$ bits by the method of blocks. How many blocks are needed, how long is each, and how many of 8 billion random fingerprints share one block with a query?
The counting argument needs $k + 1 = 8$ blocks, so that 7 differing bits must leave one block untouched. Eight blocks of 64 bits have 8 bits each. A given 8-bit block is shared by a fraction $2^{-8}$ of random fingerprints, about $8 \times 10^9 / 256 = 31$ million of 8 billion, and there are 8 probes. Each probe returns 256 times as many candidates as a 16-bit block does. That is why the method suits small $k$, such as the 3 that Manku and colleagues used.
What we have learned
Shingling turns "nearly the same" into the Jaccard coefficient of $k$-word sequences, which scored the two sentences only 1/3 because every edit destroys up to $k$ shingles. MinHash estimates the coefficient from a fixed-size sketch, with standard error $\sqrt{J(1-J)/m}$, and banding the sketch compares only likely pairs, with probability $1 - (1 - J^r)^b$. Simhash packs a page into 64 bits whose agreement tracks the angle between word vectors, and cutting the bits into blocks finds the fingerprints a few bits away.
Duplicates are one way the web wastes a crawl. Change is another: a page fetched last month may be different today. Freshness and recrawling works out how often each page should be revisited when the budget of fetches is fixed.
References
- Broder, A. Z. (1997). On the resemblance and containment of documents. In Proceedings of Compression and Complexity of Sequences 1997, 21–29. IEEE. record · 3 pages
- Broder, A. Z., Glassman, S. C., Manasse, M. S., and Zweig, G. (1997). Syntactic clustering of the web. Computer Networks and ISDN Systems, 29(8–13), 1157–1166. record · 2 pages
- Charikar, M. S. (2002). Similarity estimation techniques from rounding algorithms. In Proceedings of STOC 2002, 380–388. record · 3 pages
- Indyk, P., and Motwani, R. (1998). Approximate nearest neighbors: Towards removing the curse of dimensionality. In Proceedings of STOC 1998, 604–613. record · 3 pages
- Leskovec, J., Rajaraman, A., and Ullman, J. D. (2020). Mining of Massive Datasets (3rd ed.), chapter 3. Cambridge University Press. Free online at mmds.org. record
- Manku, G. S., Jain, A., and Das Sarma, A. (2007). Detecting near-duplicates for web crawling. In Proceedings of WWW 2007, 141–150. record
- Manning, C. D., Raghavan, P., and Schütze, H. (2008). Introduction to Information Retrieval, section 19.6. Cambridge University Press. record · 102 pages
Reviewed: verified, 16 Nov 2026 · checked against sources, 23 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · edited since · 12 revisions