Efficient Algorithms for Substring Near Neighbor Problem
Total Page:16
File Type:pdf, Size:1020Kb
Efficient Algorithms for Substring Near Neighbor Problem Alexandr Andoni Piotr Indyk [email protected] [email protected] MIT Abstract R from q. We will refer to this problem as an (R, c)-near In this paper we consider the problem of finding the ap- neighbor (NN) problem. proximate nearest neighbor when the data set points are The approximate near and nearest neighbor prob- the substrings of a given text T . Specifically, for a string T of length n, we present a data structure which does the lems have been studied for a long time. The approx- following: given a pattern P , if there is a substring of T imate nearest neighbor algorithms were first discov- within the distance R from P , it reports a (possibly dif- ered for the “low-dimensional” version of the problem, ferent) substring of T within distance cR from P . The + length of the pattern P , denoted by m, is not known in ad- where m is constant (see, e.g., [AMN 94] and the ref- vance. For the case where the distances are measured using erences therein). Later, a few results were obtained for the Hamming distance, we present a data structure which the “high-dimensional” case, where m is a parameter 1+1/c 1 1/c o(1) uses O˜(n ) space and with O˜ n + mn query (see, e.g., [Kle97, IM98, KOR98, DIIM04]). In partic- time. This essentially matches the earlier bounds of [Ind98], ular, the Locality-Sensitive Hashing (LSH) algorithm which assumed that the pattern length m is fixed in ad- 3 vance. In addition, our data structure can be constructed in of [IM98] solves the (R, c)-near neighbor problem using 1+1/c 1+1/c time O˜ n1+1/c + n1+o(1)M 1/3, where M is an upper bound O(mn ) preprocessing time, O(mn + n ) space 1/c for m. This essentially matches the preprocessing bound and O(mn ) query time. By using the dimensional- of [Ind98] as long as the term O˜ n1+1/c dominates the run- ity reduction of [KOR98], the query time can be further ning time, which is the case when, e.g., c < 3. reduced to O˜(m + n1/c), while the preprocessing time We also extend our results to the case where the dis- can be reduced to O˜(mn + n1+1/c). The LSH algorithm tances are measured according to the l1 distance. The query has been successfully used in several applied scenarios, time and the space bound are essentially the same, while the preprocessing time becomes O˜ n1+1/c + n1+o(1)M 2/3. including computational biology (cf. [BT01, Buh02] and the references therein, or [JP04], p. 414). 1 Introduction The bounds of the LSH algorithm can sometimes be even further reduced if the points in the set S The nearest neighbor problem is defined as follows: are not arbitrary, but instead are implicitly defined Rm given a set S of n points in , construct a data struc- by a (smaller) data set. This is the case for many of Rm ture that, given any q ∈ , quickly finds the point the applications of the approximate nearest neighbor p ∈ S that has the smallest distance to q. This problem problem. and its decision version (the R-near neighbor) are the Particularly interesting is the case in which S is central problems in computational geometry. Since the defined as the set of m-substrings of a sequence of exact problem is surprisingly difficult (for example, it numbers T [0 . n − 1]; we call the resulting problem an is an open problem to design an algorithm for m = 3 O(1) (R, c)-substring near neighbor (SNN) problem. (R, c)- which uses sub-quadratic space and has log n query SNN problem occurs, for example, in computational time), recent research has focused on designing efficient biology [Buh01, Buh02]. Its exact version (i.e., when approximation algorithms. Furthermore, the approxi- c = 1) has been a focus of several papers in the mate nearest neighbor is reducible to the approximate combinatorial pattern matching area (cf. [CGL04] and R-near neighbor [IM98], and, therefore, we primarily the references therein). concentrate on the latter problem. In the approximate 2 Obviously, one can solve (R, c)-SNN by reducing R-near neighbor problem , the data structure needs to it to (R, c)-NN. Specifically, we can enumerate all m- report a point within distance cR from q for some con- length substrings of T and use them as an input to the stant c > 1, but only if there exists a point at distance (R, c)-NN problem. Then, if one uses the LSH algorithm 1We use notation f(n) = O˜(g(n)) to denote f(n) = 3The bounds refer to the time needed to solve the problem in O(g(n) logO(1) n). the m-dimensional Hamming space {0, 1}m; slightly worse bounds 2The approximate nearest neighbor problem is defined in an are known for more general spaces. analogous way. to solve the near neighbor problem, then the space usage works just as well even if we stop the search at an can be reduced from O(nm+n1+1/c) to O(n1+1/c) (since arbitrary moment. one can represent the substrings implicitly). Moreover, Unfortunately, constructing the trie of strings 1+1/c the preprocessing time can be reduced from O(mn ) g1(p) . gL(p) cannot be accomplished using the ap- to O(log m · n1+1/c) by using FFT [Ind98]. proach of [Ind98]. In a naive algorithm, which explicitly A deficiency of this approach lies in the fact that the constructs the tries, constructing one trie would take query pattern size m must be fixed in advance. This O(Mn) time instead of the optimal O˜(n). We show assumption is somewhat restrictive in the context of how to reduce this time considerably, to O˜(M 1/3n). searching in sequence data. A straight-forward solution In order to reduce the query and preprocessing would be to build a data structure for each possible bounds even further, we redesign the LSH scheme. In m ∈ {0 ...M − 1}, where M is the maximum query the new scheme, the functions gi are not totally inde- size. However, the space and the preprocessing time pendent. Instead, they are obtained by concatenating would increase to O˜(n1+1/cM). tuples of a smaller number of independent hash func- In this paper, we give improved algorithms for tions. The smaller number of the “base” hash functions the approximate substring near neighbor problem for enables faster query time and preprocessing computa- unknown string length m. Our algorithms achieve tion. Using this approach, we achieve O˜ n1/c + mno(1) query time of O˜(n1/c + mno(1)), while keeping space of query time and O˜ n1+1/c + n1+o(1)M 1/3 preprocess- O˜(n1+1/c). Note that this essentially matches the query ing time. This part is the most involved part of the and the space bounds for the case where m is fixed algorithm. in advance. If the distances are measured according For the more general l1 norm, we assume that the to the Hamming metric, the preprocessing time is numbers are integers in the range Σ = {0 ... ∆}. One ˜ 1+1/c 1+o(1) 1/3 O n + n M . Thus, our preprocessing approach to solve the l1 case is to reduce it to the Ham- essentially matches the bound for the case of fixed m, ming metric case. Then, we replace each character from as long as c < 3. Σ by its unary representation: a character a is replaced If the distances are measured according to the l1 by a ones followed by ∆ − a zeros. Unfortunately, this norm, we achieve4 the same query and space bounds, as reduction multiplies the running time by a factor of ∆. 1+1/c 1+o(1) 2/3 well as preprocessing time of O˜ n + n M . To avoid this deficiency, we proceed by using For this algorithm, we need to assume that the alphabet locality-sensitive hash functions designed6 specifically Σ of the text is discrete; that is, Σ = {0 ... ∆}. for the l1 norm. In particular, we compute the value Although such an assumption is not very common of the hash function on a point in the m-dimensional in computational geometry, it is typically satisfied in space by imposing a regular grid in Rm, and shifting practice when the bounded precision arithmetic is used. it at random. Then each point is hashed to the grid cell containing it. We show that such a hash func- 1.1 Our Techniques. Our algorithms are based on tion is locality-sensitive. Moreover, we show that, by the Locality-Sensitive Hashing (LSH) algorithm. The using pattern-matching techniques (notably, algorithms basic LSH algorithm proceeds by constructing L = for the less-than-matching problem [AF95]), we can per- O(n1/c) hash tables. Each point p ∈ S is then hashed form the preprocessing in less than O(Mn) time per th into each table; the i table uses a hash function gi. function gi. We mention that less-than matching has The query point is hashed L times as well; the points been earlier used for a geometric problem in [EIV01]. colliding with the query are reported. For a more Finally, to achieve the stated bounds for l1, we detailed description of LSH, see the next section. apply the technique of reusable gi functions, as in the In order to eliminate the need to know the value case of the Hamming distance.