Abstract 1 Introduction 2 Approximate Hamming Distance 3 Approximate Edit Distance References

Asymmetric Streaming Approximate
Pattern Matching

Wojciech Janczewski111During the preparation of this paper, Wojciech Janczewski was a postdoctoral researcher at DI ENS, École normale supérieure, Paris, PSL University, France. ORCID Institute of Computer Science, University of Wrocław, Poland    Tatiana Starikovskaya ORCID DIENS, École normale supérieure de Paris, PSL Research University, France
Abstract

We study the space complexity of pattern matching in the asymmetric streaming model, focusing on approximate pattern matching under the Hamming and edit distances. In this problem, we are given an m-length pattern and an n-length text and must compute, for every position of the text, the smallest distance between the pattern and a substring of the text which ends at this position. In the asymmetric streaming model, we assume to have constant-time random access to the pattern, while the text arrives as a stream, one letter at a time.

It is known that computing all distances exactly in the asymmetric streaming model requires Ω(m) space (for the edit distance see Li and Zheng [FSTTCS 2021]). Hence, to achieve sublinear space, a relaxation of the problem is necessary. One possible variant is to consider the small distance regime, where the algorithm must compute only those distances that are bounded by a small integer parameter k. In this case, existing algorithms in a more restrictive fully streaming model (Kociumaka, Clifford, Porat [SODA’19], Bhattacharya, Koucký [ICALP’23]) straightforwardly imply the existence of poly(k,logn)-space asymmetric streaming algorithms. Another possible relaxation is computing all distances approximately. For this variant, we don’t have small-space algorithms in the fully streaming model: the best known algorithm solves pattern matching under the Hamming distance (1+ϵ)-approximately using 𝒪~(ϵ2m) space (Starikovskaya, Svagerka, Uznański [APPROX’20]).222Hereafter, 𝒪~() hides factors polylogarithmic in n. For the edit distance, no efficient approximation algorithms are known.

In this work, we show approximation algorithms for pattern matching under the Hamming and edit distances in the asymmetric streaming model for any constant ϵ>0:

  1. 1.

    We show that there is a simple randomised asymmetric streaming algorithm that solves approximate pattern matching under the Hamming distance (1+ϵ)-approximately using 𝒪(ϵ3log3n) bits.

  2. 2.

    As our second and main contribution, we extend the result of Cheng et al. [ICALP 2021] and show that for any integer k there is a deterministic asymmetric streaming algorithm that solves pattern matching under the edit distance (2k1+ϵ)-approximately using 𝒪~(m1/k) space.

Keywords and phrases:
Asymmetric streaming, Pattern matching, Approximation, Edit distance, Hamming distance
Copyright and License:
[Uncaptioned image] © Wojciech Janczewski and Tatiana Starikovskaya; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Streaming, sublinear and near linear time algorithms
; Theory of computation Approximation algorithms analysis ; Theory of computation Pattern matching
Funding:
Partially funded by a grant ANR-20-CE48-0001 from the French National Research Agency and by the Polish National Science Centre grant number 2023/51/B/ST6/01505.
Editors:
Philip Bille and Nicola Prezza

1 Introduction

In this work, we study the space complexity of pattern matching in the streaming setting. We focus on approximate pattern matching under the Hamming and edit distances. For the Hamming distance and a pattern of length m, the algorithm should output the Hamming distance to the pattern for each length-m window of the text. For the edit distance, we should find, for every position j of text T, the distance between pattern P and a substring T[i,j] of T for a position i minimising this value.

In the fully streaming setting, we assume that P arrives first, the algorithm preprocesses it, and then receives T as a stream. We account for all the space used to store the information about the pattern (which is not available via random access) and the text. This work is focused on the more recent asymmetric streaming model proposed by Saks and Seshadhri [18] in the context of the longest common subsequence problem (see also the work of Andoni, Krauthgamer, and Onak [3]).

In this model, we are given constant-time random access to the pattern P, which arrives first, and the text T arrives as a stream. Thus, we have read-only access to the pattern and do not account for the space used to store it. This is a natural variant of the fully streaming setting, as two parts of the input (pattern/text) are inherently different and it is easy to imagine some fixed relatively short pattern residing in working memory, while fully storing the whole text is out of reach. As such, the asymmetric streaming model represents a practically interesting intermediate point between the read-only setting (where both the pattern and the text are read-only) and the fully streaming setting.

It is known that computing all distances exactly in the approximate pattern matching problem, by information-theoretic argument, requires Ω(m) space, even in the asymmetric streaming model. Hence, to achieve sublinear space one must consider a relaxation of the problem.

One possible variant is to consider the small distance regime, where one must compute only those distances that are bounded by a small integer parameter k. In such a case, efficient fully streaming algorithms exist, and they carry on to the asymmetric streaming model. The study of fully streaming algorithms for pattern matching under the Hamming distance in the small-distance regime was initiated by Porat and Porat [17], who showed that the problem can be solved in 𝒪~(k3) space and 𝒪~(k2) time per letter of the text. Subsequent improvements [7, 14] led to the current best solution [8], which solves the problem in 𝒪(klogn) space and 𝒪~(k) time per letter of the text. [13] studied trade-offs between streaming space and time. For the edit distance, two algorithms are known [15, 4], the best [4] by Bhattacharya and Koucký taking 𝒪~(k2) space and time in the streaming model.

Another possible relaxation is computing all distances approximately. For this variant, we don’t have algorithms using polylogarithmic space in the fully streaming model: the best known algorithm solves pattern matching under the Hamming distance (1+ϵ)-approximately using 𝒪~(ϵ2n) space [9, 19]. For the edit distance, no efficient approximation algorithms are known.

1.1 Our results

In this work, we study the approximate pattern matching and show that the asymmetric streaming model does allow for space-efficient algorithms.

Hamming distance.

As our first result, we give an efficient algorithm for the approximate pattern matching under the Hamming distance, in the asymmetric streaming model:

Theorem 1.

Let ϵ>0 be a constant, P be a read-only pattern of length m, and T be a streaming text of length n on a polynomial-size alphabet. There is an asymmetric streaming algorithm that solves approximate pattern matching under the Hamming distance (1+ϵ)-approximately using 𝒪(ϵ3log3n) bits and polynomial time. The algorithm is randomised and is correct with high probability.333By high probability we mean probability that is bigger than 11/nc for a constant c1.

By the lower bound for counting ones in a sliding window [10] (see also [9]), any asymmetric streaming algorithm for this problem must use Ω(ϵ1log2m) bits.

The main idea of our algorithm is to divide the text, for every natural klogm, into blocks of varying length but each at a distance roughly 2k from the pattern. For any level k and each of the ϵ1 latest blocks on this level, we store a sketch which uses 𝒪(ϵ2log2n) bits and allows to compute (1+ϵ)-approximation of the Hamming distance. We then show that if the Hamming distance between P and T[jm+1,j] is at most ϵ12k, where T[j] is the latest arrived letter of the text, then whole T[jm+1,j] is covered by the stored blocks on level k, and we can use the sketches to compute an approximation of the Hamming distance, with some additive error which is reduced by the chosen parameters.

Edit distance.

As our second and main contribution, we show a space-efficient asymmetric streaming algorithm for approximating the edit distances between the pattern and text substrings. Namely, for each position j of the text T, we must compute an approximate value of the smallest possible edit distance between P and some substring T[i,j].

Asymmetric query complexity for edit distance was first considered in [3], with free access to one string and the goal of minimising the number of queries for letters in the other string. Saks and Seshadhri [18] furthered this idea and developed the asymmetric streaming model, along with the first asymmetric streaming algorithm for the longest common subsequence problem (LCS). The merged paper of Cheng, Farhadi, Hajiaghayi, Jin, Li, Rubinstein, Seddighin, and Zheng [5] (full versions [11], [6]) revisited the problem of computing the edit distance, and showed that there is (i) a deterministic asymmetric streaming algorithm which computes 𝒪(2k)-approximation of the edit distance between two strings using 𝒪~(km1/k) space and polynomial time, and (ii) (1+ϵ)-approximation of the edit distance between two strings using 𝒪~(ϵ1n) space for any constant ϵ>0. Li and Zheng [16] improved the space complexity of the 𝒪(2k)-approximation algorithm to 𝒪~(kd1/k), where d is the edit distance between the input strings. They also showed that any asymmetric streaming algorithm (even randomised) computing the edit distance exactly must use Ω(n) bits, and that computing the edit distance (1+ϵ)-approximately requires Ω(ϵ1) bits.

For the problem of approximate pattern under the edit distance, we show the following:

Theorem 2.

Let ϵ>0 be a constant, k a constant integer, P a read-only pattern of length m, and T a streaming text on a polynomial-size alphabet. There is an asymmetric streaming algorithm that solves approximate pattern matching under the edit distance (2k1+ϵ)-approximately, using 𝒪~(m1/k) bits and polynomial time. The algorithm is deterministic.

Therefore, we are able to extend the result of [5] from string-to-string edit distance to approximate pattern matching under the edit distance, in the same space complexity 𝒪~(m1/k), losing only an additional ϵ factor in the (large) constant-factor approximation.

The structure of our solution for the edit distance is similar to that for the Hamming distance; in particular, we still divide text into blocks, for multiple levels parametrised by distances. However, unlike the Hamming distance, there are no small-space sketches for approximating the edit distance between strings, which is the main source of difficulty. In their solution for string-to-string edit distance computation, [5] suggested approximating the blocks of the text with the closest substrings of the pattern (to which we have constant-time random access). We extend their techniques to pattern matching. There are some challenges in this approach: first, [5] works with a static decomposition of a fixed-length string, while we must maintain the decomposition for appropriate suffixes of the streamed longer text. To this end, we maintain a number of the latest static blocks in a queue, but then a substring T[i,j], where j is the latest arrived letter of the text, is rarely fully aligned with the chosen blocks. The first and last blocks need to be trimmed, introducing a small additive error, which nevertheless can be transformed into a multiplicative one.

Open questions.

Our results lead to intriguing open problems. First, what is the best time complexity one can achieve for approximate pattern matching under the Hamming distance while using polylogarithmic space? Ideally, the time per letter of the text would also be polylogarithmic, but in our solution complexity is dominated by computing distances to all substrings of given length, which takes quadratic time per letter due to sketch properties. Second, can pattern matching under the Hamming distance be solved with a deterministic algorithm more efficiently than under the edit distance? Our space-efficient algorithm uses randomised sketches.

For the pattern matching with edits, can we achieve (1+ϵ)-approximation in 𝒪~(ϵ1) space? Note that even for only string-to-string edit distance, the best existing solution takes 𝒪~(ϵ1n) bits, so the first step would be to match this for pattern matching. Finally, can randomisation help to achieve a better space or time complexity for the edit distance? The time complexity of known deterministic algorithms is large.

1.2 Preliminaries

Throughout the paper, we index strings starting from 1. S[i,j] is a substring of S from index i to j, which is the same as S[i,j+1) or S(i1,j]. S1S2 denotes the concatenation of two strings. ED(S1,S2) is the edit distance between two strings, that is, the minimal number of single-letter insertions, deletions, or substitutions necessary to transform S1 into S2. HD(S1,S2) denotes the Hamming distance between a pair of strings of equal length, that is, the number of letter substitutions necessary to transform S1 into S2. By k-approximation of value v we will always mean a number in the range [v,kv].

The distance of string S from pattern P is the minimum distance between S and any substring of P.

2 Approximate Hamming Distance

We first assume that the alphabet is binary and show how to lift this assumption later. For the Hamming distance, one can adapt randomised so-called tug-of-war sketches of polylogarithmic size, developed for estimating frequency moments in the stream [2, 12]. We will use the following sketches, designed by Achlioptas:

Corollary 3 ([1, Theorem 1.1], see also [9, 19]).

There is a sketching function f:{0,1}n{0,1}𝒪(ϵ2log2n), such that for any two strings S1,S2 of equal lengths at most n, we can with high probability obtain (1+ϵ)-approximation of HD(S1,S2) using f(S1),f(S2), in 𝒪(ϵ2logn) time. The sketch f(S) of a string S{0,1}n can be maintained in streaming using 𝒪(ϵ2log2n) bits and in 𝒪(ϵ2logn) time per letter.

Let us define an approximate block to be a tuple storing a Hamming distance sketch for some text substring T[i,j], length ji+1, as well as a pair of indices describing a pattern substring P[i,j] (which will be close to T[i,j]). We say that such a block is an approximation of T[i,j].

Queues Construction.

For each value 2t, where 0tlogm, we create a queue Qt storing up to ϵ1 approximate blocks. Assume that the last block in the queue Qt is an approximation of a substring of T ending at a position i1 (if Qt is empty, set i=1). We maintain the Hamming distance sketch of a substring starting at the index i. When a new letter T[j] arrives, we update the sketch and use it to compute the minimum of approximate Hamming distances from T[i,j] to substrings of P of length ji+1. If this minimum is at least (1+ϵ)2t, we create a new approximate block, where the stored indices are the endpoints of a substring of P realising the minimum approximate distance to T[i,j], and add the block to Qt. By Corollary 3, with high probability T[i,j] is at distance at least 2t from P, and the distance between the block and the stored substring of P is in range [2t,(1+ϵ)2t]. It might also be the case that we read m letters from the stream without achieving the threshold of distance from P, if the substring of the text is very similar to the pattern; then we also finalise a block. If the size of Qt exceeds ϵ1, the oldest block is removed from the queue.

Note that the last processed index of T is usually further than the end of the last block in Qt, as the new approximate block covering the current suffix of the streamed text has not yet been created. Nevertheless, the algorithm maintains a sketch of this suffix. We have:

Definition 4.

The index k in T is covered by the queue of approximate blocks Q if the first block in Q is an approximation of T[i,j] and ik.

Proposition 5.

Let the last processed index of T be j, and i=jm+1. For any t, if HD(P,T[i,j])<ϵ12t, then with high probability i is covered by Qt.

The above follows from the fact that Qt holds up to ϵ1 last blocks, and each block is an approximation of some T[q1,q2] at distance at least 2t from P. Assume that HD(P,T[i,j])<ϵ12t, then we observe that Qt cannot contain ϵ1 such blocks starting at indices larger than i. This means the first block in Qt starts before i, and T[i,j] is covered by Qt.

Approximating the distance.

Let T=T[i,j] be the current m-length suffix of T, so i=jm+1. Assume that ϵ12t1HD(P,T)<ϵ12t, so i is covered by Qt. Let B[1,b] be a substring of P stored in a block in Qt approximating T[q1,q2] with q1i and maximal q1. For all blocks following B in Qt and the last unfinished block, we have sketches and strings lengths, therefore we can obtain (1+ϵ)-approximation of HD(P[q2i+2,m],T[q2i+2,m]) by partitioning P[q2i+2,m] into substrings with lengths equal to consecutive blocks lengths, computing sketches of these substrings, and finally summing approximate block-to-substring distances. Let the resulting value be D. For the prefix of T there is no sketch available, but letting k=q2i+1, by construction B(bk,b] is at a distance at most (1+ϵ)2t from T[1,k], thus by the triangle inequality:

HD(P[1,k],T[1,k])(1+ϵ)2tHD(P[1,k],B(bk,b])HD(P[1,k],T[1,k])+(1+ϵ)2t

We can compute HD(P[1,k],B(bk,b])+(1+ϵ)2t exactly, and finally obtain that

HD(P,T)D+HD(P[1,k],B(bk,b])+(1+ϵ)2t
(1+ϵ)HD(P[q2i+2,m],T[q2i+2,m])+HD(P[1,k],T[1,k])+2(1+ϵ)2t
(1+ϵ)HD(P,T)+2(1+ϵ)2t<(1+6ϵ)HD(P,T),

since ϵ12t1HD(P,T), and for ϵ<1/2.

Complexity analysis.

We use sketches of length 𝒪(ϵ2log2n), for every of the 𝒪(logm) queues, each storing ϵ1 blocks, resulting in space complexity of 𝒪(ϵ3log3n) bits. With high probability, the output is correct for every alignment. Time complexity is 𝒪~(m2ϵ3) per letter, dominated by using sketches to compute distances to all substrings of fixed lengths.

Extension to polynomial-size alphabets.

To handle any polynomial-size alphabet, we can use a known method described for example in [19]. For the alphabet Σ={0,1,,k} we use mapping h:Σ{0,1}k+1 such that h(x)=0x10kx, which exactly doubles the Hamming distance. Given string S over Σ, we instead run the algorithm on h(S) over the binary alphabet. Then in Corollary 3 sketches use Θ(ϵ2log2(n|Σ|)) bits, which is asymptotically the same for a polynomial-size alphabet. Time complexity is not affected either, due to the sketching algorithm performing operations only for letters 1 in the text, see [19].

3 Approximate Edit Distance

In this section, we prove Theorem 2. We will use the following basic facts:

Proposition 6.

For any integer z, consider arbitrary partitions of two strings into z substrings as S1=A1A2Az and S2=B1B2Bz. It holds that ED(S1,S2)l=1zED(Al,Bl). Moreover, there exists a partition of S2 into S2=B1B2Bz such that ED(S1,S2)=l=1zED(Al,Bl).

We will also use redefined approximate blocks:

Definition 7.

k-approximate block for a text substring T[i,j] and the pattern P is a pair of indices describing P[i,j] and an additive value d such that for any substring P of the pattern it holds that ED(P,T[i,j])ED(P,P[i,j])+dkED(P,T[i,j]).

In other words, by leveraging the read-only pattern, we can store just 𝒪(logn) bits, providing us with a k-approximation of the edit distance between some text substring and any substring of the pattern. For brevity, we will often say that some P[i,j] is an approximate block, omitting stored additive factor d. The following observation using the triangle inequality is a cornerstone of algorithms for asymmetric streaming edit distance:

Proposition 8 ([11]).

Let P be a substring of P with the smallest edit distance to some text substring T[i,j]. Then P with an additive value of ED(P,T[i,j]) is a 3-approximate block for T[i,j]. (See Figure 1.)

Figure 1: We have ddd+p3d, so the closest substring in P provides 3-approximation for edit distance d between T and substrings of P. We will not compute the edit distance to strings outside of P.

For any string S, we will refer to a substring of P with the smallest edit distance to S as its closest substring. Recall that the distance of S from P is the distance between S and its closest substring. We will also use:

Fact 9 ([5, Theorem 3]).

For any constants δ<1/2 and ϵ<1, there is an algorithm that outputs (1+ϵ)-approximation of edit distance between two given read-only strings using 𝒪~(nδ/ϵ) space and polynomial time.

3.1 First Step: (𝟑+ϵ)-approximation in 𝓞~(𝒎𝟏/𝟐) space

We start by showing the following result:

Theorem 10.

There is an asymmetric streaming algorithm providing a (3+ϵ)-approximation for pattern matching with edits in 𝒪~(m1/2) space and polynomial time.

Theorem 2 is then obtained by applying this algorithm recursively, as described in the next subsection. Let us set r=m1/2. The goal is to maintain a queue Q of up to r 3-approximate blocks, each at the distance Θ(r) from P.

To this end, consider some index i in T, initially i=1, and read T[i,i+r) from the stream. Then compute, in space 𝒪~(r), the closest substring P along with the full list of d edit operations necessary to transform P into T[i,i+r). Next, remove T[i,i+r) from the working space and read T[i+r,i+2r). Observe that we still have access to all letters of T[i,i+2r), via P and the list of edits. Therefore, we can compute the closest substring P′′ of T[i,i+2r), again with the full list of edit operations. If ED(P′′,T[i,i+2r))>r, we stop and place P′′ in the queue Q as a 3-approximate (by Proposition 8) block for the processed fragment of text, clearly at the distance of Θ(r) from P. In the other case, the process continues iteratively until the closest substring is finally at a distance larger than r. Then we push the block on the queue, removing the oldest element from Q if the queue already contained r blocks. Until the block is finished, we retain full access to the corresponding suffix of T. This is presented in Algorithm 1. See also Figure 2.

Algorithm 1 Computing a single block in a queue.

The set of edit operations O can be stored sorted by indices, allowing access to SS while using 𝒪(rlogm) bits. We can compute the edit distance in line 7 using 𝒪(rlogm) bits, even though the two strings can be much longer. This is since the distance cannot be larger than 2r, thus computing 𝒪(r) central diagonals in the standard dynamic programming for the edit distance is enough:

Proposition 11.

For two read-only strings S1,S2, if ED(S1,S2)=𝒪(r), we can compute ED(S1,S2) using only 𝒪~(r) additional space.

The set of edit operations can be obtained similarly. Let us now consider a useful property of the computed queue.

Figure 2: A queue stores up to r approximate blocks. The last read letter of the text is T[j]. A suffix T[i,j], marked red, is currently being processed and is not yet represented in the queue as a full block. For T[s,i), the queue stores 3-approximate blocks representing its consecutive fragments. For example, a substring F of T[s,i) is represented by its closest substring in the pattern P (of possibly different length, and marked blue). T[1,s), marked orange, is not covered anymore as the queue stores no more than r blocks, but any index larger than s is covered.
Proposition 12.

Let the last processed index of T be j. For any ij, if ED(P,T[i,j])r2=m, then i is covered by Q.

Proof.

The above follows from the fact that Q holds up to r latest blocks, and each block is an approximation of some T[q1,q2] at distance from P exceeding r.

Assume ED(P,T[i,j])r2=m, and i is not covered by Q, thus the queue must be full. For the k-th block in Q for k[1,r], denote that it stores P[ak,ak] as an approximation of T[bk,bk+1). As i is not covered, we have b1>i. By Proposition 6, consider a partition of P=P0P1Pr+1 such that ED(P,T[i,j])=ED(P0,T[i,b1))+k=1rED(Pk,T[bk,bk+1))+ED(Pr+1,T[br+1,j]). By definition of blocks stored in Q, they are at distance larger than r from P, so for any k ED(Pk,T[bk,bk+1))>r. But this contradicts ED(P,T[i,j])r2=m, as there are r blocks at a distance larger than r.

It remains to bound the approximation we can obtain using the queue. For some index i, assume that ED(P,T[i,j])m, with the last processed index of T being j. By Proposition 12, T[i,j] is covered by Q. We consider a string T constructed as follows, in order to approximate T[i,j] (which is not stored explicitly) with blocks from the queue.

Let B=[1,b] be a block in Q being approximation of T[q1,q2] with q1i and maximal q1. Recall B is a substring of P. Set T=B(iq1,b], as B is an approximate block for a substring of T extending too far to the left of i. Since we are interested in the edit distance to just T[i,q2], we drop a prefix of B of length iq1, using the fact that removing the first letters from two strings does not increase the edit distance between them. That is, ED(S1,S2)ED(S1[q,|S1|],S2[q,|S2|]), for any q<|S1|,|S2|. Note that we need to know q1, but it can be stored for any approximate block without affecting space complexity (we omit this detail in the pseudocode).

Then, append to T all blocks following B in the queue, and finally append the suffix of T which is still being processed by the queue procedure. Let Dl be an additive value for the l-th concatenated full approximate block, and D=Dl. See Figure 3.

We do not build T explicitly, as it can be much more than r letters. It is just represented as a sequence of indices to the read-only pattern, and for the unprocessed suffix, additionally a list of edit operations and a buffer of the most recent letters. Next, we show that T can be used to approximate distance between T[i,j] and arbitrary substring of the pattern:

Figure 3: We want to approximate edit distance between T[i,j] and P, a suffix of T processed so far and an arbitrary substring of P, using Q which covers i. T[i,j] is naturally split into z+2 fragments according to blocks from Q. P can then be optimally split into z+2 fragments such that ED(P,T[i,j]) is obtained by summing edit distances between corresponding pairs of substrings, for example between T[i1,i2) and P1. Text fragments are approximated by blocks stored in Q, so B1 is 3-approximate block for T[i1,i2), allowing us to compare T[i1,i2) (not stored in the memory) to P1. Block B extends to the left of i and will be trimmed (the orange part). After the last block Bz, the next block is not yet constructed, but this means we can access all letters of T[iz+1,j] (the red part). All blocks are directly represented as substrings of P, with stored additive values.
Lemma 13.

Let the last processed index of T be j, P be any substring of P such that ED(P,T[i,j])=dm, and ED(P,T)=d. We can compute in polynomial time (1+ϵ)-approximation of d, and a value v such that dd+v3d+4r.

Proof.

Recall that B=[1,b] is a block in Q being approximation of T[q1,q2] with q1i and maximal q1. Let i1,i2,,iz+1 be the sequence of indices such that l-th block Bl in Q after initial block B is an approximation of T[il,il+1). Note that:

T=B(iq1,b]B1BzT[iz+1,j].

By Proposition 6, P can be partitioned into consecutive substrings P0,,Pz+1 such that ED(P,T[i,j])=ED(P0,T[i,i1))+l=1zED(Pl,T[il,il+1))+ED(Pz+1,T[iz+1,j]). Note this is not necessarily a partition optimal for computing ED(P,T) with selected blocks.

We have ED(T[i,i1),B(iq1,b])2r, which means that:

ED(S,T[i,i1))2rED(S,B(iq1,b])ED(S,T[i,i1))+2r (1)

for any string S, by the triangle inequality. We cannot claim 3-approximation here, as B(iq1,b] is not the closest substring of T[i,i1), but only a trimmed closest substring of T[q1,q2], which was at distance at most 2r.

Moreover, from definition of 3-approximate blocks, for any l it holds that ED(Pl,T[il,il+1))ED(Pl,Bl)+Dl3ED(Pl,T[il,il+1)). We want to avoid guessing the optimal partition of P necessary for block-by-block computation, so instead need to tie ED(P,T), relatively easy to compute, to ED(P,T[i,j])=d. The algorithm will return value ED(P,T)+2r+D. As an upper bound for this estimate of d, we get:

Claim 14.

ED(P,T)+2r+D3d+4r.

Proof.

ED(P,T)+2r+D (2)
ED(P0,B(iq1,b])+2r+l=1z(ED(Pl,Bl)+Dl)+ED(Pz+1,T[iz+1,j]) (3)
ED(P0,T[i,i1))+4r+l=1z3ED(Pl,T[il,il+1))+ED(Pz+1,T[iz+1,j]) (4)
3d+4r. (5)

(3) holds as we partition P into P0,P1,, and T into blocks. (4) is by using (1) and the blocks being 3-approximate. (5) follows just by the chosen partition of P optimal for computing ED(P,T[i,j]).

To obtain a lower bound for the returned estimate, let us consider another partition of P into z+2 consecutive substrings P0,,Pz+1 such that ED(P,T)=ED(P0,B(iq1,b])+l=1zED(Pl,Bl)+ED(Pz+1,T[iz+1,j]).

Claim 15.

dED(P,T)+2r+D.

Proof.

d ED(P0,T[i,i1))+l=1zED(Pl,T[il,il+1))+ED(Pz+1,T[iz+1,j]) (6)
ED(P0,B(iq1,b])+2r+l=1z(ED(Pl,Bl)+Dl)+ED(Pz+1,T[iz+1,j]) (7)
=ED(P,T)+2r+D (8)

(6) follows from P0,P1, being a partition of P. (7) is by using (1) and the blocks being 3-approximate. (8) holds by the chosen partition of P optimal for computing ED(P,T).

By Claims 14 and 15, dED(P,T)+2r+D3d+4r. The algorithm can compute (1+ϵ)-approximation of ED(P,T), using Fact 9, as we have access to all letters of T. 2r+D is known to the algorithm.

By Lemma 13, for any constant ϵ we can compute (3+ϵ)-approximation of ED(P,T[i,j]) but with an additive factor 𝒪(r). To get rid of it, we create an additional queue Q0 storing blocks at a constant distance from P, augmented with the full list of edit operations transforming a substring of P into a substring of T; so these are in fact not approximate blocks, but provide exact access to substrings of T. This can be done by a variant of Algorithm 1, replacing the inequality in line 7 with <1 instead of <r, and additionally reading only a single letter into the buffer. This ensures that the distance of a block from P is always 1. Q0 stores up to rlogm augmented blocks, and we have that:

Proposition 16.

Let the last processed index of T be j. For any i, if ED(P,T[i,j])rlogm, then i is covered by Q0.

This allows us to compute the edit distance exactly for suffixes of T close to the pattern. Otherwise, we can assume that ED(P,T[i,j])=drlogm. By Lemma 13 and Proposition 12, we can obtain estimate w such that dw<(1+ϵ)(3d+4r)(1+ϵ)3d(1+4/(3logm)). As 1/logm=o(1), Algorithm 2 can compute (3+ϵ)-approximation of miniED(T[i,j],P) for any constant ϵ, for m large enough.

The total space used by the algorithm is 𝒪(m1/2log2m) bits, as Q0 uses 𝒪(rlog2m) bits for storing blocks and lists of edit operations. Q uses a smaller number of 𝒪(rlogm) bits to store the blocks and one list of edit operations for the unfinished block. 𝒪(rlogm) bits is also enough to compute this list of edit operations by Proposition 11. Using Fact 9 requires asymptotically fewer bits. This finishes the proof of Theorem 10.

Algorithm 2 Algorithm to obtain approximate edit distance for the best suffix.

3.2 Second Step: (𝟐𝒌𝟏+ϵ)-approximation in 𝓞~(𝒎𝟏/𝒌) space

In this section, we apply the previous approach iteratively, arriving at our main result of Theorem 2. We need the following extension of Proposition 8:

Proposition 17.

Let d be the distance from some text substring T[i,j] to its closest substring in P. Let P be a substring of P, and v be a value for which ED(P,T[i,j])vgd holds, for some g. Then P with an additive value v is (2g+1)-approximate block for T[i,j].

Proof.

Let P be any substring of P, by assumption ED(P,T[i,j])d. ED(P,T[i,j])v+ED(P,P) by the triangle inequality. On the other hand, v+ED(P,P)v+ED(P,T[i,j])+ED(P,T[i,j])2v+ED(P,T[i,j])(2g+1)ED(P,T[i,j]).

For an integer k, set r=m1/k as roughly the space complexity we are aiming for. We assume r is a power of two, otherwise we can take the largest power of two smaller than m1/k. The algorithm maintains the set of queues Qt for t[0,k), each queue holding up to rlogm approximate blocks (assume logm is an integer). The index t is called a level of the queue Qt. We will be using Fact 9 with some small constant ϵ to be specified later.

The base of the algorithm is formed by Q0, constructed exactly as in the previous section, and providing access to the suffix of the streamed text which is very close to the pattern. The queues on further levels cannot be constructed in the same manner, since we do not want to maintain in a buffer a suffix of T which is at a distance larger than r from P. Instead, we will maintain Qt using Qt1, so there are k steps in the inductive structure. Q1 could also be built directly, but not any of the further queues, so we choose not to distinguish Q1. We construct queues with the following properties:

Lemma 18.

Let 0t<k, and q=(1+ϵ)(1+8/r), for some ϵ to be defined later depending on the desired final approximation factor. The following holds for Qt:

  1. 1.

    Qt stores qt(2t+11)-approximate blocks, up to rlogm of them.

  2. 2.

    For any block in Qt, which is an approximation of some text substring T[i,j], the distance from T[i,j] to P is in range [rt,2rtqt(2t+11)].

  3. 3.

    If the last processed index of T is j, for any i, if ED(P,T[i,j])(rlogm)rt, then i is covered by Qt.

Proof.

Let At=qt(2t+11). We will proceed by induction on levels. As in Lemma 13, Qt can be used to compute distance to any substring of P. For some long suffix of T we possibly can no longer compute the closest substring in P, but still can obtain an approximate closest substring, compounding the approximation factor for each consecutive level, by Proposition 17. This is described in Algorithm 3; Q0 works as in the previous section.

For the ease of presentation, in the pseudocode each queue moves the current index j in T, while in fact this process should be synchronised between all queues processing the next letter from the stream, starting from level 0. The details of the algorithm in lines 5 or 9 are described below. A temporary block tempBt is a substring of P with the smallest approximate distance to the current suffix of T on level t; only in Q0 we could afford direct access to this suffix. As previously, we create a new full block only when the (approximate) distance from P exceeds some threshold.

Algorithm 3 Computing a single block for queue Qt, t>0.

We need to inductively bound the approximation factor, as in the lemma statement. All properties hold for the base of t=0, so assume they hold for t and consider Qt+1. Qt+1 is constructing blocks using Qt and its At-approximate blocks. Let the current suffix be T[i,j], meaning the last block in Qt+1 ended at i1 and the current index is j, and consider any substring P of the pattern.

The algorithm can approximate ED(P,T[i,j])=d as follows, similarly to Lemma 13. Let B[1,b] be a block in Qt being approximation of T[q1,q2] with q1i and maximal q1. Such a block exists since i is covered by Qt, and recall that B is a substring of P. Set T=B(iq1,b], as B is an approximate block for a substring of T extending too far to the left. Then, append to T all blocks following B from Qt, and finally append tempBt, the temporary block approximating the suffix which is still being processed by the queue procedure in Qt. Again, T is not built explicitly, but as it is a concatenation of fragments of P, we can access any of its letters. Let Dl be the additive value for the l-th concatenated full approximate block Bl, and D=l=1zDl. Let i1,i2,,iz+1 be the sequence of indices such that the l-th block in the Qt after B is an approximation of T[il,il+1).

Recall At=qt(2t+11) is the approximation ratio of blocks in Qt. By Proposition 6, P can be partitioned into z+2 consecutive substrings P0,P1,,Pz+1 such that d=ED(P0,T[i,i1))+l=1zED(Pl,T[il,il+1))+ED(Pz+1,T[iz+1,j]). From the second property of Qt we have ED(T[i,i1),B(iq1,b])2rtAt, thus by the triangle inequality for any string S it holds that:

ED(S,T[i,i1))2rtAtED(S,B(iq1,b])ED(S,T[i,i1))+2rtAt.

For any l, ED(Pl,T[il,il+1))ED(Pl,Bl)+DlAtED(Pl,T[il,il+1)), by the first property and the definition of approximate blocks. By Line 7 of Algorithm 3, we have that ED(tempBt,T[iz+1,j])2rtAt is enforced directly, so again for any S:

ED(S,tempBt)2rtAtED(S,T[iz+1,j])ED(S,tempBt)+2rtAt.

Since the algorithm have no access to T[i,j], it estimates ED(P,T[i,j]) with value of ED(P,T)+4rtAt+D. We can obtain the following upper bound:

ED(P,T)+4rtAt+D
ED(P0,B(iq1,b])+4rtAt+l=1z(ED(Pl,Bl)+Dl)+ED(Pz+1,tempBt)
ED(P0,T[i,i1))+8rtAt+l=1zAtED(Pl,T[il,il+1))+ED(Pz+1,T[iz+1,j])
Atd+8rtAt,

where steps are analogous to those in Lemma 13. To get a lower bound, consider another partition of P into substrings P0,,Pz+1 with ED(P,T)=ED(P0,B(iq1,b])+l=1zED(Pl,Bl)+ED(Pz+1,tempBt). We then have:

d ED(P0,T[i,i1))+l=1zED(Pl,T[il,il+1))+ED(Pz+1,T[iz+1,j])
ED(P0,B(iq1,b])+4rtAt+l=1z(ED(Pl,Bl)+Dl)+ED(Pz+1,tempBt)
=ED(P,T)+4rtAt+D.

Therefore, dED(P,T)+4rtAt+DAtd+8rtAt. Algorithm 3 uses ED(P,T)+4rtAt+D as d, with ED(P,T) approximated using Fact 9, incurring additional 1+ϵ error.

Consider the second property of the lemma. For T[i,j] at distance at most rt+1 from P, by the third property i is covered by Qt when j is the current index. Moreover, for any P with d=ED(P,T[i,j])<rt+1, using T as above we have:

(1+ϵ)ED(P,T)+4rtAt+D<(1+ϵ)Atd+9rtAt<rt+1At(1+ϵ+9/r)<2rt+1At

assuming r>10 and ϵ<1/10. All of this ensures that the block in Qt+1 is not created until the distance from T[i,j] to P is at least rt+1.

For the first property, we can now assume that Qt+1 creates blocks only for suffixes at distance from P at least rt+1. Consider P with rt+1ED(P,T[i,j])=d, and so we have Atd+8rtAtAtd(1+8/r). We use Fact 9 to obtain a (1+ϵ)-approximation of ED(P,T). Now if we take P with the minimal reported approximate distance to T[i,j], since 2At(1+ϵ)(1+8/r)+1=2qt(2t+11)(1+ϵ)(1+8/r)+1<qt+1(2t+21)=At+1, by Proposition 17 we obtain At+1-approximate block, satisfying the first property.

Regarding the third property, the proof is analogous to the Proposition 12, as any T[i,j] at distance at most rt+2logm from P can be partitioned into rlogm parts each at distance at most rt+1 from P, and by the second property blocks in Qt+1 approximate substrings at distances at least rt+1 from P. This finishes the proof of the lemma.

Now, to obtain Theorem 2, we try all the queues and indices covered by them, then take the minimum reported edit distance. Consider T[i,j] at distance d from P. Qt will provide desired approximation for d(rtlogm,rt+1logm]. Indeed, for values in this range Qt covers i, thus we can construct T and compute a value which is at most

(1+ϵ)Atd+9rtAt Atd(1+ϵ+9/logm)
<(2k1)(1+ϵ)k(1+8/r)kd(1+ϵ+9/logm).

Let δ=ϵ/2k. We can set ϵ=k1ln(1+δ/4) so (1+ϵ)k<1+δ/4. For m large enough, (1+8m1/k)k is smaller than 1+δ/4 for any constant ϵ. Similarly (1+ϵ+9/logm)<1+δ/4, for m large enough and ϵ as above. Overall, we will get (2k1)(1+δ)<(2k1+ϵ)-approximate solution.

As r=m1/k, space complexity of the algorithm is 𝒪(m1/klog2m) bits with a constant number of queues, each storing rlogm blocks, and time complexity for each letter read remains polynomial in m. Parameters in Fact 9 can be set so that it has space complexity 𝒪~(m1/(k+1)ϵ1), which is asymptotically smaller for any constant ϵ.

We note that the space complexity of Theorem 2 can be achieved for the Hamming distance, as the triangle inequality holds and we have a trivial small-space algorithm computing the distance of two read-only strings. This gives a solution much less efficient than Theorem 1, but deterministic.

References

  • [1] Dimitris Achlioptas. Database-friendly random projections: Johnson-Lindenstrauss with binary coins. J. Comput. Syst. Sci., 66(4):671–687, 2003. doi:10.1016/S0022-0000(03)00025-4.
  • [2] Noga Alon, Yossi Matias, and Mario Szegedy. The space complexity of approximating the frequency moments. J. Comput. Syst. Sci., 58(1):137–147, 1999. doi:10.1006/JCSS.1997.1545.
  • [3] Alexandr Andoni, Robert Krauthgamer, and Krzysztof Onak. Polylogarithmic approximation for edit distance and the asymmetric query complexity. In FOCS, pages 377–386. IEEE Computer Society, 2010. doi:10.1109/FOCS.2010.43.
  • [4] Sudatta Bhattacharya and Michal Koucký. Streaming k-edit approximate pattern matching via string decomposition. In ICALP, volume 261 of LIPIcs, pages 22:1–22:14. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2023. doi:10.4230/LIPIcs.ICALP.2023.22.
  • [5] Kuan Cheng, Alireza Farhadi, MohammadTaghi Hajiaghayi, Zhengzhong Jin, Xin Li, Aviad Rubinstein, Saeed Seddighin, and Yu Zheng. Streaming and small space approximation algorithms for edit distance and Longest Common Subsequence. In ICALP, volume 198 of LIPIcs, pages 54:1–54:20. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2021. doi:10.4230/LIPIcs.ICALP.2021.54.
  • [6] Kuan Cheng, Zhengzhong Jin, Xin Li, and Yu Zheng. Space efficient deterministic approximation of string measures. CoRR, abs/2002.08498, 2020. arXiv:2002.08498.
  • [7] Raphaël Clifford, Allyx Fontaine, Ely Porat, Benjamin Sach, and Tatiana Starikovskaya. The k-mismatch problem revisited. In SODA, pages 2039–2052. SIAM, 2016. doi:10.1137/1.9781611974331.CH142.
  • [8] Raphaël Clifford, Tomasz Kociumaka, and Ely Porat. The streaming k-mismatch problem. In SODA, pages 1106–1125. SIAM, 2019. doi:10.1137/1.9781611975482.68.
  • [9] Raphaël Clifford and Tatiana Starikovskaya. Approximate Hamming distance in a stream. In ICALP, volume 55 of LIPIcs, pages 20:1–20:14. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2016. doi:10.4230/LIPIcs.ICALP.2016.20.
  • [10] Mayur Datar, Aristides Gionis, Piotr Indyk, and Rajeev Motwani. Maintaining stream statistics over sliding windows. SIAM J. Comput., 31(6):1794–1813, 2002. doi:10.1137/S0097539701398363.
  • [11] Alireza Farhadi, MohammadTaghi Hajiaghayi, Aviad Rubinstein, and Saeed Seddighin. Asymmetric streaming algorithms for edit distance and LCS. CoRR, abs/2002.11342, 2020.
  • [12] Joan Feigenbaum, Sampath Kannan, Martin Strauss, and Mahesh Viswanathan. An approximate L1-difference algorithm for massive data streams. SIAM J. Comput., 32(1):131–151, 2002. doi:10.1137/S0097539799361701.
  • [13] Shay Golan, Tomasz Kociumaka, Tsvi Kopelowitz, and Ely Porat. The streaming k-mismatch problem: Tradeoffs between space and total time. In CPM, volume 161 of LIPIcs, pages 15:1–15:15. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2020. doi:10.4230/LIPIcs.CPM.2020.15.
  • [14] Shay Golan, Tsvi Kopelowitz, and Ely Porat. Towards optimal approximate streaming pattern matching by matching multiple patterns in multiple streams. In ICALP, volume 107 of LIPIcs, pages 65:1–65:16. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2018. doi:10.4230/LIPIcs.ICALP.2018.65.
  • [15] Tomasz Kociumaka, Ely Porat, and Tatiana Starikovskaya. Small-space and streaming pattern matching with k edits. In FOCS, pages 885–896. IEEE, 2021.
  • [16] Xin Li and Yu Zheng. Lower bounds and improved algorithms for asymmetric streaming edit distance and Longest Common Subsequence. In FSTTCS, volume 213 of LIPIcs, pages 27:1–27:23. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2021. doi:10.4230/LIPIcs.FSTTCS.2021.27.
  • [17] Benny Porat and Ely Porat. Exact and approximate pattern matching in the streaming model. In FOCS, pages 315–323. IEEE Computer Society, 2009. doi:10.1109/FOCS.2009.11.
  • [18] Michael E. Saks and C. Seshadhri. Space efficient streaming algorithms for the distance to monotonicity and asymmetric edit distance. In SODA, pages 1698–1709. SIAM, 2013. doi:10.1137/1.9781611973105.122.
  • [19] Tatiana Starikovskaya, Michal Svagerka, and Przemyslaw Uznanski. Lp pattern matching in a stream. In APPROX-RANDOM, volume 176 of LIPIcs, pages 35:1–35:23. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2020. doi:10.4230/LIPIcs.APPROX/RANDOM.2020.35.