factograf
EN
Sign in

Algorithmic technique using hashing

Locality-sensitive hashing

Locality-sensitive hashing

In computer science, locality-sensitive hashing (LSH) is a fuzzy hashing technique that hashes similar input items into the same "buckets" with high probability. The number of buckets is much smaller than the universe of possible input items. Since similar items end up in the same buckets, this technique can be used for data clustering and nearest neighbor search. It differs from conventional hashing techniques in that hash collisions are maximized, not minimized. Alternatively, the technique can be seen as a way to reduce the dimensionality of high-dimensional data; high-dimensional input items can be reduced to low-dimensional versions while preserving relative distances between items.

Hashing-based approximate nearest-neighbor search algorithms generally use one of two main categories of hashing methods: either data-independent methods, such as locality-sensitive hashing (LSH); or data-dependent methods, such as locality-preserving hashing (LPH).

Locality-preserving hashing was initially devised as a way to facilitate data pipelining in implementations of massively parallel algorithms that use randomized routing and universal hashing to reduce memory contention and network congestion.

Definitions

A finite family {\displaystyle {\mathcal {F}}} of functions {\displaystyle h\colon M\to S} is defined to be an LSH family for

  • a metric space {\displaystyle {\mathcal {M}}=(M,d)},
  • a threshold {\displaystyle r>0},
  • an approximation factor {\displaystyle c>1},
  • and probabilities {\displaystyle p_{1}>p_{2}}

if it satisfies the following condition. For any two points {\displaystyle a,b\in M} and a hash function {\displaystyle h} chosen uniformly at random from {\displaystyle {\mathcal {F}}}:

  • If {\displaystyle d(a,b)\leq r}, then {\displaystyle h(a)=h(b)} (i.e., a and b collide) with probability at least {\displaystyle p_{1}},
  • If {\displaystyle d(a,b)\geq cr}, then {\displaystyle h(a)=h(b)} with probability at most {\displaystyle p_{2}}.

Such a family {\displaystyle {\mathcal {F}}} is called {\displaystyle (r,cr,p_{1},p_{2})}-sensitive.

LSH with respect to a similarity measure

Alternatively it is possible to define an LSH family on a universe of items U endowed with a similarity function {\displaystyle \phi \colon U\times U\to [0,1]}. In this setting, a LSH scheme is a family of hash functions H coupled with a probability distribution D over H such that a function {\displaystyle h\in H} chosen according to D satisfies {\displaystyle Pr[h(a)=h(b)]=\phi (a,b)} for each {\displaystyle a,b\in U}.

Amplification

Given a {\displaystyle (d_{1},d_{2},p_{1},p_{2})}-sensitive family {\displaystyle {\mathcal {F}}}, we can construct new families {\displaystyle {\mathcal {G}}} by either the AND-construction or OR-construction of {\displaystyle {\mathcal {F}}}.

To create an AND-construction, we define a new family {\displaystyle {\mathcal {G}}} of hash functions g, where each function g is constructed from k random functions {\displaystyle h_{1},\ldots,h_{k}} from {\displaystyle {\mathcal {F}}}. We then say that for a hash function {\displaystyle g\in {\mathcal {G}}}, {\displaystyle g(x)=g(y)} if and only if all {\displaystyle h_{i}(x)=h_{i}(y)} for {\displaystyle i=1,2,\ldots,k}. Since the members of {\displaystyle {\mathcal {F}}} are independently chosen for any {\displaystyle g\in {\mathcal {G}}}, {\displaystyle {\mathcal {G}}} is a {\displaystyle (d_{1},d_{2},p_{1}^{k},p_{2}^{k})}-sensitive family.

To create an OR-construction, we define a new family {\displaystyle {\mathcal {G}}} of hash functions g, where each function g is constructed from k random functions {\displaystyle h_{1},\ldots,h_{k}} from {\displaystyle {\mathcal {F}}}. We then say that for a hash function {\displaystyle g\in {\mathcal {G}}}, {\displaystyle g(x)=g(y)} if and only if {\displaystyle h_{i}(x)=h_{i}(y)} for one or more values of i. Since the members of {\displaystyle {\mathcal {F}}} are independently chosen for any {\displaystyle g\in {\mathcal {G}}}, {\displaystyle {\mathcal {G}}} is a {\displaystyle (d_{1},d_{2},1-(1-p_{1})^{k},1-(1-p_{2})^{k})}-sensitive family.

Applications

LSH has been applied to several problem domains, including:

Methods

Bit sampling for Hamming distance

One of the easiest ways to construct an LSH family is by bit sampling. This approach works for the Hamming distance over d-dimensional vectors {\displaystyle \{0,1\}^{d}}. Here, the family {\displaystyle {\mathcal {F}}} of hash functions is simply the family of all the projections of points on one of the {\displaystyle d} coordinates, i.e., {\displaystyle {\mathcal {F}}=\{h\colon \{0,1\}^{d}\to \{0,1\}\mid h(x)=x_{i}{\text{ for some }}i\in \{1,\ldots,d\}\}}, where {\displaystyle x_{i}} is the {\displaystyle i}th coordinate of {\displaystyle x}. A random function {\displaystyle h} from {\displaystyle {\mathcal {F}}} simply selects a random bit from the input point. This family has the following parameters: {\displaystyle P_{1}=1-R/d}, {\displaystyle P_{2}=1-cR/d}. That is, any two vectors {\displaystyle x,y} with Hamming distance at most {\displaystyle R} collide under a random {\displaystyle h} with probability at least {\displaystyle P_{1}}. Any {\displaystyle x,y} with Hamming distance at least {\displaystyle cR} collide with probability at most {\displaystyle P_{2}}.

Min-wise independent permutations

Suppose U is composed of subsets of some ground set of enumerable items S and the similarity function of interest is the Jaccard index J. If π is a permutation on the indices of S, for {\displaystyle A\subseteq S} let {\displaystyle h(A)=\min _{a\in A}\{\pi (a)\}}. Each possible choice of π defines a single hash function h mapping input sets to elements of S.

Define the function family H to be the set of all such functions and let D be the uniform distribution. Given two sets {\displaystyle A,B\subseteq S} the event that {\displaystyle h(A)=h(B)} corresponds exactly to the event that the minimizer of π over {\displaystyle A\cup B} lies inside {\displaystyle A\cap B}. As h was chosen uniformly at random, {\displaystyle Pr[h(A)=h(B)]=J(A,B)\,} and {\displaystyle (H,D)\,} define an LSH scheme for the Jaccard index.

Because the symmetric group on n elements has size n!, choosing a truly random permutation from the full symmetric group is infeasible for even moderately sized n. Because of this fact, there has been significant work on finding a family of permutations that is "min-wise independent" — a permutation family for which each element of the domain has equal probability of being the minimum under a randomly chosen π. It has been established that a min-wise independent family of permutations is at least of size {\displaystyle \operatorname {lcm} \{\,1,2,\ldots,n\,\}\geq e^{n-o(n)}}, and that this bound is tight.

Because min-wise independent families are too big for practical applications, two variant notions of min-wise independence are introduced: restricted min-wise independent permutations families, and approximate min-wise independent families. Restricted min-wise independence is the min-wise independence property restricted to certain sets of cardinality at most k. Approximate min-wise independence differs from the property by at most a fixed ε.

Open source methods

Nilsimsa Hash

Nilsimsa is a locality-sensitive hashing algorithm used in anti-spam efforts. The goal of Nilsimsa is to generate a hash digest of an email message such that the digests of two similar messages are similar to each other. The paper suggests that the Nilsimsa satisfies three requirements:

  1. The digest identifying each message should not vary significantly for changes that can be produced automatically.
  2. The encoding must be robust against intentional attacks.
  3. The encoding should support an extremely low risk of false positives.

Testing performed in the paper on a range of file types identified the Nilsimsa hash as having a significantly higher false positive rate when compared to other similarity digest schemes such as TLSH, Ssdeep and Sdhash.

TLSH

TLSH is locality-sensitive hashing algorithm designed for a range of security and digital forensic applications. The goal of TLSH is to generate hash digests for messages such that low distances between digests indicate that their corresponding messages are likely to be similar.

An implementation of TLSH is available as open-source software.

Random projection

The random projection method of LSH due to Moses Charikar called SimHash (also sometimes called arccos) uses an approximation of the cosine distance between vectors. The technique was used to approximate the NP-complete max-cut problem.

The basic idea of this technique is to choose a random hyperplane (defined by a normal unit vector r) at the outset and use the hyperplane to hash input vectors.

Given an input vector v and a hyperplane defined by r, we let {\displaystyle h(v)=\operatorname {sgn} (v\cdot r)}. That is, {\displaystyle h(v)=\pm 1} depending on which side of the hyperplane v lies. This way, each possible choice of a random hyperplane r can be interpreted as a hash function {\displaystyle h(v)}.

For two vectors u,v with angle {\displaystyle \theta (u,v)} between them, it can be shown that

{\displaystyle Pr[h(u)=h(v)]=1-{\frac {\theta (u,v)}{\pi }}.}

Since the ratio between {\displaystyle {\frac {\theta (u,v)}{\pi }}} and {\displaystyle 1-\cos(\theta (u,v))} is at least 0.439 when {\displaystyle \theta (u,v)\in [0,\pi]}, the probability of two vectors being on different sides of the random hyperplane is approximately proportional to the cosine distance between them.

Stable distributions

The hash function {\displaystyle h_{\mathbf {a},b}({\boldsymbol {\upsilon }}):{\mathcal {R}}^{d}\to {\mathcal {N}}} maps a d-dimensional vector {\displaystyle {\boldsymbol {\upsilon }}} onto the set of integers. Each hash function in the family is indexed by a choice of random {\displaystyle \mathbf {a} } and {\displaystyle b} where {\displaystyle \mathbf {a} } is a d-dimensional vector with entries chosen independently from a stable distribution and {\displaystyle b} is a real number chosen uniformly from the range [0,r]. For a fixed {\displaystyle \mathbf {a},b} the hash function {\displaystyle h_{\mathbf {a},b}} is given by {\displaystyle h_{\mathbf {a},b}({\boldsymbol {\upsilon }})=\left\lfloor {\frac {\mathbf {a} \cdot {\boldsymbol {\upsilon }}+b}{r}}\right\rfloor }.

Other construction methods for hash functions have been proposed to better fit the data. In particular k-means hash functions are better in practice than projection-based hash functions, but without any theoretical guarantee.

Semantic hashing

Semantic hashing is a technique that attempts to map input items to addresses such that closer inputs have higher semantic similarity. The hashcodes are found via training of an artificial neural network or graphical model.

Read next

In 7 languages

Text from Wikipedia, CC BY-SA 4.0 · Source article