Locality-Sensitive Hashing
Locality-sensitive hashing is a randomized algorithmic framework introduced by Piotr Indyk and Rajeev Motwani in 1998 that hashes nearby high-dimensional points into the same buckets with high probability for sublinear approximate nearest-neighbor search.
Locality-Sensitive Hashing (LSH) is a randomized algorithmic framework in computation and algorithms and high-dimensional computational geometry for solving the (c, r)-Approximate Nearest Neighbor (ANN) search problem in sublinear query time O(d ยท nฯ) with exponent ฯ < 1. Formulated by Piotr Indyk and Rajeev Motwani in their seminal 1998 STOC paper "Approximate Nearest Neighbors: Towards Removing the Curse of Dimensionality," LSH reverses the design objective of classical and cryptographic hash function families: instead of scattering similar inputs uniformly across buckets to minimize collisions in a hash table, a locality-sensitive hash family H is explicitly constructed so that the probability of a hash collision PrhโH[h(p) = h(q)] increases monotonically as the distance d(p, q) between two points decreases.
In low dimensions (d โค 10), true nearest-neighbor search in an n-point dataset can be solved in O(log n) query time using spatial partitioning trees (k-d trees, R-trees, or Voronoi diagrams). However, as dimension d grows into hundreds or thousands (such as vector embeddings, genomic k-mer profiles, or image descriptors), the curse of dimensionality causes the volume of a high-dimensional sphere to concentrate near its boundary and causes spatial tree searches to degrade to an O(d ยท n) exhaustive linear scan. By relaxing true nearest-neighbor search to c-approximate search (for c > 1) and combining metric-specific LSH familiesโsuch as bit sampling for Hamming distance, MinHash for Jaccard distance, SimHash random hyperplanes for cosine similarity, and p-stable projections for Euclidean L2 distanceโLSH guarantees query time whose exponent ฯ = ln(1/P1) / ln(1/P2) depends only on the approximation ratio c and is independent of the ambient dimension d.
Formal (r1, r2, P1, P2)-Sensitivity and the Exponent ฯ
Definition of an LSH Family
Let (X, d) be a metric space and let U be a hash bucket universe. A family H = {h : X โ U} of hash functions Equipped with a probability distribution is called (r1, r2, P1, P2)-sensitive (with r1 < r2 and P1 > P2) if for every pair of points p, q โ X:
- High Collision Probability for Close Points: If d(p, q) โค r1, then PrhโH[ h(p) = h(q) ] โฅ P1.
- Low Collision Probability for Far Points: If d(p, q) โฅ r2 = c ยท r1, then PrhโH[ h(p) = h(q) ] โค P2.
The efficiency of an (r1, c r1, P1, P2)-sensitive family is governed by the dimensionless LSH quality exponent:
Because P1 > P2 โ (0, 1), we have 0 < ln(1/P1) < ln(1/P2), ensuring ฯ < 1.
IndykโMotwani AND-Then-OR Amplification Construction
A single hash function h โ H typically has a modest gap between P1 and P2 (for instance, P1 = 0.8 and P2 = 0.5). To amplify this probability gap into a sharp step function, Indyk and Motwani apply a two-stage (k, L) ANDโOR concatenation:
- AND Concatenation (Widening the Gap): Concatenate k independent base hash functions from H to form a compound vector hash function g(x) = (h1(x), h2(x), ..., hk(x)). Two points collide under g (g(p) = g(q)) if and only if they collide in all k coordinates, lowering the false-positive collision probability for far points (d(p, q) โฅ r2) to P2k. Choosing k = โ log1/P2 n โ = โ (ln n) / ln(1/P2) โ ensures that P2k โค 1 / n, so the expected number of far points (d โฅ r2) colliding with query q in a single table g is at most n ยท (1/n) = 1 point.
- OR Repetition (Restoring Recall): Because AND concatenation also lowers the true-positive collision probability for a close neighbor (d(p, q) โค r1) to P1k = P1(ln n)/ln(1/P2) = nโln(1/P1)/ln(1/P2) = nโฯ, the algorithm constructs L = โ nฯ โ independent hash tables g1, g2, ..., gL.
During a query for q, the algorithm probes the L buckets g1(q), ..., gL(q) and inspects at most 3L candidate points. The probability of finding a close neighbor p (d(p, q) โค r1) across the L tables is at least 1 โ (1 โ nโฯ)nฯ โฅ 1 โ 1/e โ 0.632 (boosted to 1 โ ฮด by scaling L by ln(1/ฮด)). Thus, LSH achieves query time O(d ยท nฯ log n) and space O(n1+ฯ + d ยท n).
Canonical LSH Families Across Metric Spaces
Each distance metric admits a mathematically tailored LSH family whose collision probability is a closed-form function of distance or similarity:
- Hamming Space ({0, 1}d, dH) โ Bit Sampling (Indyk & Motwani, 1998): Pick a random coordinate i โ {1, ..., d} and set hi(x) = xi. Then Pr[h(p) = h(q)] = 1 โ dH(p, q) / d, achieving exponent ฯ โค 1 / c.
- Jaccard Distance (J(A, B)) โ MinHash (Broder, 1997): For sets A, B โ U, hฯ(A) = minxโA ฯ(x) satisfies Pr[hฯ(A) = hฯ(B)] = J(A, B) = 1 โ dJ(A, B).
- Angular / Cosine Similarity (ฮธ(p, q)) โ Random Hyperplane SimHash (Charikar, 2002): Sample a unit Gaussian vector v ~ N(0, Id) and set hv(x) = sign(v ยท x) โ {โ1, +1}. Because the probability that a random hyperplane separates p and q is proportional to the angle ฮธ = arccos((p ยท q) / (โpโ2 โqโ2)) between them, the collision probability is:
- Euclidean & Lp Distance (p โ (0, 2]) โ p-Stable Projections E2LSH (Datar et al., 2004): Project x โ โd onto a vector a whose entries are drawn i.i.d. from a p-stable distribution (standard Gaussian N(0, 1) for L2 Euclidean distance; Cauchy distribution for L1 Manhattan distance) and quantize into bins of width w with random shift b ~ Uniform[0, w]:
achieving exponent ฯ โ 1 / c for L1 and ฯ โ 1 / c2 for L2 via Cross-Polytope / Spherical LSH (Andoni & Indyk, 2006; Andoni et al., 2015).
| Distance / Similarity Metric | Canonical LSH Hash Family | Exact Collision Probability Pr[h(p) = h(q)] | Optimal Exponent ฯ (c-ANN) | Primary Domain of Application |
|---|---|---|---|---|
| Hamming Distance dH(p, q) | Bit Sampling (hi(x) = xi) | 1 โ dH(p, q) / d | ฯ = 1 / c | Binary descriptors (ORB, BRIEF), error-correcting codes |
| Jaccard Similarity J(A, B) | MinHash (hฯ(A) = min ฯ(A)) | J(A, B) = |A โฉ B| / |A โช B| | ฯ = ln J1 / ln J2 | Web document deduplication, genomic k-mer search |
| Cosine / Angle ฮธ(p, q) | SimHash Random Hyperplanes (sign(v ยท x)) | 1 โ ฮธ(p, q) / ฯ | ฯ = ln(1 โ ฮธ1/ฯ) / ln(1 โ ฮธ2/ฯ) | Text/embedding similarity, Google web crawler (Manku et al., 2007) |
| Manhattan Distance L1 | Cauchy 1-Stable Projection โ(aยทx + b)/wโ | Integral over Cauchy density fC | ฯ = 1 / c + o(1) | Histogram & bag-of-words comparison |
| Euclidean Distance L2 | Gaussian E2LSH / Cross-Polytope LSH / Data-Dependent | Gaussian CDF / Spherical cap volume | ฯ = 1 / c2 (data-indep) or 1 / (2c2 โ 1) | High-dimensional embeddings, Reformer LSH attention |
Multi-Probe LSH, Data-Dependent LSH, and Neural Attention Applications
A practical drawback of basic (k, L) LSH is that constructing L = O(nฯ) separate hash tables requires significant memory when ฯ โ 0.3โ0.5. In 2007, Qin Lv, William Josephson, Zhe Wang, Moses Charikar, and Kai Li introduced Multi-Probe LSH, which observes that in quantized Euclidean LSH (ha,b(x) = โ(a ยท x + b)/wโ), a close neighbor p that misses the exact query bucket g(q) almost always lands in an adjacent bucket differing by ยฑ1 in one or two coordinates. By probing a sequence of perturbation vectors around g(q) within each table, Multi-Probe LSH reduces the required number of hash tables L by an order of magnitude (~90% memory reduction) at identical recall.
On the theoretical frontier, Alexandr Andoni, Piotr Indyk, Thijs Laarhoven, Ilya Razenshteyn, and Ludwig Schmidt (2015) proved that Cross-Polytope LSH (rotating x on the unit sphere via fast Hadamard transforms and snapping to the nearest vertex ยฑei of the cross-polytope) achieves the optimal data-independent Euclidean exponent ฯ = 1 / c2, and that data-dependent LSH reduces the exponent further to ฯ = 1 / (2c2 โ 1). In deep learning and bioinformatics, Cross-Polytope LSH powers the Reformer transformer architecture (Kitaev, Kaiser, & Levskaya, 2020)โreducing self-attention complexity from O(L2) to O(L log L) by computing attention only among query-key vectors that collide in the same LSH bucketโand accelerates long-read genome assembly (MHAP, Berlin et al., 2015) alongside Bloom filter and CountโMin sketch indexes.
Frequently Asked Questions
Why does locality-sensitive hashing require distinct hash families for different distance metrics?
In fact, a metric space (X, d) admits an LSH family if and only if (X, d) can be isometrically (or low-distortion) embedded into L1 or Hamming space. For example, no LSH family can exist for the general Lโ (Chebyshev) norm without polynomial distortion depending on d, because Lโ cannot be embedded into L1 without ฮฉ(log d) distortion. Consequently, each metric (L1, L2, Cosine, Jaccard) requires an LSH construction tailored to its specific geometric isometries.
How does Google's 64-bit SimHash detect near-duplicate web pages across billions of URLs?
In Moses Charikar's SimHash (deployed at Google by Manku, Jain, & Das Sarma, 2007), each weighted document feature f is hashed to a 64-bit string and adds +wf (for 1-bits) or โwf (for 0-bits) to a 64-element accumulator vector; taking the signs yields a 64-bit fingerprint. Two 64-bit fingerprints across an 8-billion-page corpus are tested for Hamming distance dH โค 3 in sub-millisecond time by splitting the 64 bits into blocks and probing a few permuted prefix tables.
How does LSH compare with graph-based ANN indexes like HNSW (Hierarchical Navigable Small World) in practice?
Graph-based proximity indexes (such as HNSW and DiskANN) and product quantization (FAISS IVF-PQ) achieve higher empirical recall-per-second on moderate-dimensional dense neural embeddings (d = 128โ1,536) by exploiting the low intrinsic dimensionality of real data manifolds, but require expensive O(n log n) index construction and lack worst-case theoretical guarantees. By contrast, LSH offers provable worst-case sublinear query bounds, O(1) streaming insertions/deletions without graph rebuilding, and unmatched speed for sparse/set representations (via MinHash) and on-the-fly dynamic bucketing (such as Reformer LSH attention).
References
- Indyk, P., & Motwani, R. (1998). "Approximate nearest neighbors: towards removing the curse of dimensionality." Proceedings of the Thirtieth Annual ACM Symposium on Theory of Computing (STOC '98), 604โ613. https://doi.org/10.1145/276698.276876
- Charikar, M. S. (2002). "Similarity estimation techniques from rounding algorithms." Proceedings of the Thirty-Fourth Annual ACM Symposium on Theory of Computing (STOC '02), 380โ388. https://doi.org/10.1145/509907.509965
- Datar, M., Immorlica, N., Indyk, P., & Mirrokni, V. S. (2004). "Locality-sensitive hashing scheme based on p-stable distributions." Proceedings of the Twentieth Annual Symposium on Computational Geometry (SoCG '04), 253โ262. https://doi.org/10.1145/997817.997857
- Lv, Q., Josephson, W., Wang, Z., Charikar, M., & Li, K. (2007). "Multi-probe LSH: efficient indexing for high-dimensional similarity search." Proceedings of the 33rd International Conference on Very Large Data Bases (VLDB '07), 950โ961. https://dl.acm.org/doi/10.5555/1325851.1325958
- Andoni, A., Indyk, P., Laarhoven, T., Razenshteyn, I., & Schmidt, L. (2015). "Practical and optimal LSH for angular distance." Advances in Neural Information Processing Systems (NeurIPS 28), 1225โ1233. https://proceedings.neurips.cc/paper/2015/file/2823f4797102ce1a1aec05359cc16dd9-Paper.pdf