โ† Wiki Index / Computation & Algorithms / Locality-Sensitive Hashing
๐Ÿ’ป Computation & Algorithms

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:

  1. High Collision Probability for Close Points: If d(p, q) โ‰ค r1, then PrhโˆˆH[ h(p) = h(q) ] โ‰ฅ P1.
  2. 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:

ฯ = ln(1 / P1) / ln(1 / P2) = ( ln P1 ) / ( ln P2 ) โˆˆ (0, 1)

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:

  1. 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.
  2. 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:
Prv[ sign(v ยท p) = sign(v ยท q) ] = 1 โˆ’ ฮธ(p, q) / ฯ€
  • 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]:
ha,b(x) = โŒŠ ( a ยท x + b ) / 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 MetricCanonical LSH Hash FamilyExact 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 / cBinary 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 J2Web 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 L1Cauchy 1-Stable Projection โŒŠ(aยทx + b)/wโŒ‹Integral over Cauchy density fCฯ = 1 / c + o(1)Histogram & bag-of-words comparison
Euclidean Distance L2Gaussian E2LSH / Cross-Polytope LSH / Data-DependentGaussian 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
๐Ÿงฌ Support Open Science
Explore interactive science games on LowKey Games or browse scientific apparel & accessories in the CRV Science Shop.
๐Ÿ’ก Spot an inaccuracy or have primary literature to expand this article?
Submit a citation or revision note using your shared CRV Science / LowKey Games profile ID.
Suggest Revision