## Online Pattern Matching for String Edit Distance with Moves /star

Yoshimasa Takabatake , Yasuo Tabei , and Hiroshi Sakamoto 1 2 1

1 Kyushu Institute of Technology { takabatake,hiroshi @donald.ai.kyutech.ac.jp } 2 PRESTO, Japan Science and Technology Agency tabei.y.aa@m.titech.ac.jp

Abstract. Edit distance with moves (EDM) is a string-to-string distance measure that includes substring moves in addition to ordinal editing operations to turn one string to the other. Although optimizing EDM is intractable, it has many applications especially in error detections. Edit sensitive parsing (ESP) is an efficient parsing algorithm that guarantees an upper bound of parsing discrepancies between different appearances of the same substrings in a string. ESP can be used for computing an approximate EDM as the L 1 distance between characteristic vectors built by node labels in parsing trees. However, ESP is not applicable to a streaming text data where a whole text is unknown in advance. We present an online ESP (OESP) that enables an online pattern matching for EDM. OESP builds a parse tree for a streaming text and computes the L 1 distance between characteristic vectors in an online manner. For the space-efficient computation of EDM, OESP directly encodes the parse tree into a succinct representation by leveraging the idea behind recent results of a dynamic succinct tree. We experimentally test OESP on the ability to compute EDM in an online manner on benchmark datasets, and we show OESP's efficiency.

## 1 Introduction

Streaming text data appears in many application domains of information retrieval. Social data analysis faces a problem for analyzing continuously generated texts. In computational biology, recent sequencing technologies enable us to sequence individual genomes in a short time, which resulted in generating a large collection of genome data. There is therefore a strong incentive to develop a powerful method for analyzing streaming texts on a large-scale.

Edit distance with moves (EDM) is a string-to-string distance measure that includes substring moves in addition to insertions and deletions to turn one string to the other in a series of editing operations. The distance measure is motivated in error detections, e.g., insertions and deletions on lossy communication channels [13], typing errors in documents [8] and evolutionary changes in biological sequences [9]. Computing an optimum solution of EDM is intractable,

/star This work was supported by JSPS KAKENHI(24700140,26280088) and the JST PRESTO program

Table 1. Summary of recent pattern matching methods for EDM. The table summaries upper bound for the approximation ratio of EDM, computation time and space for each method. The space for ESP and OESP is presented in bits. N is the length of an input string; σ is the alphabet size; n is the number of variables in CFG; α ∈ (0 , 1] is a parameter for a hash table; lg ∗ is the iterated logarithm; lg stands for log 2 .

|                                          | Appro. ratio                                  | Time                                                   | Space                                                    | Algorithm               |
|------------------------------------------|-----------------------------------------------|--------------------------------------------------------|----------------------------------------------------------|-------------------------|
| SNN [16] Shapira and Storer [21] ESP [7] | O (lg N lg ∗ N ) O O (lg N ) O (lg N lg ∗ N ) | ( N O (1) + N polylog( N )) O ( N 2 ) O ( N lg ∗ N/α ) | O ( N O (1) ) O ( N lg N ) N lg σ + n ( α +3)lg( n + σ ) | Offline Offline Offline |
| OESP                                     | O (lg 2 N )                                   | O ( N lg N lg n α lg lg n )                            | n ( α +1)lg( n + σ ) + n lg ( αn ) +5 n + o ( n )        | Online                  |

since the problem is known to be NP-complete [16]. Therefore, researchers have paid considerable efforts to develop efficient approximation algorithms that are only applicable to an offline case where a whole text is given in advance (Table 1). Early results include the reversal model [12,1] which takes a substring of unrestricted size and replaces it by its reverse in one operation. Muthukrishnan and Sahinalp [16] proposed an approximate nearest neighbor considered as a sequence comparison with block operations. Recently, Shapira and Storer proposed a polylog time algorithm with O (lg N lg ∗ N ) approximation ratio for the length N of an input text.

Edit sensitive parsing (ESP) [7] is an efficient parsing algorithm developed for approximately computing EDM between strings in an offline setting. ESP builds from a given string a parse tree that guarantees upper bounds of parsing discrepancies between different appearances of the same substring, and then it represents the parse tree as a vector each dimension of which represents the frequency of the corresponding node label in a parse tree. L 1 distance between such characteristic vectors for two strings can approximate the EDM. Although ESP has an efficient approximation ratio O (lg N lg ∗ N ) and runs fast in O N ( lg ∗ N/α ) time for a parameter α ∈ (0 , 1] for hash tables, its applicability is limited to an offline case. For applications in web mining and Bioinformatics, computing an EDM of massive streaming text data has ever been an important task. An open challenge, which is receiving increased attention, is to develop a scalable online pattern matching for EDM.

We present an online pattern matching for EDM. Our method is an online version of ESP named online ESP (OESP) that (i) builds a parse tree for a streaming text in an online manner, (ii) computes characteristic vectors for a substring at each position of the streaming text and a query, and (iii) computes the L 1 distance between each pair of characteristic vectors. The working space of our method does not depend on the length of text but the size of a parse tree. To make the working space smaller, OESP builds a parse tree from a streaming text and directly encodes it into a succinct representation by leveraging the idea behind recent results of an online grammar compression [15,14] and a dynamic succinct tree [18]. Our representation includes a novel succinct representation of a tree named post-order unary degree sequence (POUDS) that is built by the post-order traversal of a tree and a unary degree encoding. To guarantee the approximate EDM computed by OESP, we also prove an upper bound of the approximation ratio between our approximate EDM and the exact EDM.

Experiments using standard benchmark texts revealed OESP's efficiencies.

## 2 Preliminaries

## 2.1 Basic notation

Let Σ be a finite alphabet forming texts, and σ = | Σ | . Σ ∗ denotes the set of all texts over Σ , and Σ /lscript denotes the set of all texts of length /lscript over Σ , i.e. Σ /lscript = { S ∈ Σ ∗ || S | = /lscript } . We assume a recursively enumerable set X of variables such that Σ ∩X = φ and all elements in Σ ∪X are totally ordered. A sequence of symbols from Σ ∪ X is called a string. The length of string S is denoted by | S | , and the cardinality of a set C is similarly denoted by | C | . A pair and triple of symbols from Σ ∪ X are called digram and trigram, respectively. Strings x and z are said to be the prefix and suffix of the string S = xyz , respectively, and x, y, z are called substrings of S . The i -th symbol of S is denoted by S i [ ] (1 ≤ i ≤ | S | ). For integers i and j with 1 ≤ i ≤ j ≤ | S | , the substring of S from S i [ ] to S j [ ] is denoted by S i, j [ ]. N denotes the length of a text S and it can be variable in an online setting.

## 2.2 Context-free grammar

A context-free grammar (CFG) is a quadruple G = ( Σ,V,D,Z s ) where V is a finite subset of X , D is a finite subset of V × ( V ∪ Σ ) ∗ of production rules, and Z s ∈ V represents the start variable. D is also called a phrase dictionary . Variables in V are called nonterminals. The set of strings in Σ ∗ derived from Z s by G is denoted by L G ( ). A CFG G is called admissible if for any Z ∈ X there is exactly one production rule Z → γ ∈ D . We assume | γ | = 2 or 3 for any production rule Z → γ .

The parse tree of G is represented as a rooted ordered tree with internal nodes labeled by variables in V and leaves labeled by elements in Σ , and the label sequence of its leaves are equal to an input string. Any internal node Z ∈ V in a parse tree corresponds to a production rule in the form of Z → γ in D . The height of Z is the height of the subtree whose root is Z .

## 2.3 Phrase and reverse dictionaries

For a set V of production rules, a phrase dictionary D is a data structure for directly accessing the phrase S ∈ ( Σ ∪ V ) ∗ for any given Z ∈ V if Z → S ∈ D . A reverse dictionary D -1 : ( Σ ∪ V ) ∗ → V is a mapping from a given sequence of symbols to a variable. D -1 returns a variable Z associated with a string S if Z → S ∈ D ; otherwise, it creates a new variable Z ′ / V ∈ and returns Z ′ . For example, if D = { Z 1 → abc, Z 2 → cd } , D -1 ( a, b, c ) returns Z 1 , while D -1 ( b, c ) creates Z 3 and returns it.

## 2.4 Problem definition

In order to describe our method we first review the notion of EDM. The EDM d S, Q ( ) between two strings S and Q is the minimum number of edit operations defined below to transform S into Q :

- 1. Insertion: A character a at position i in S is inserted, which generates S [1 , i -1] aS i S i [ ] [ +1 , N ],
- 2. Deletion: A character a at position i in S is deleted, which generates S [1 , i -1] S i [ +1 , N ],
- 3. Replacement: A character at position i is replaced by a , which generates S [1 , i -1] aS i [ +1 , N ],
- 4. Substring move: A substring S i, j [ ] is moved and inserted at the position k , which generates S [1 , i -1] S j [ +1 , k -1] S i, j S k, N [ ] [ ].

Problem 1 (Online pattern matching for EDM) For a streaming text S ∈ Σ ∗ , a query Q ∈ Σ ∗ , and a distance threshold k ≥ 0 , find all i ∈ [1 , | S | ] such that the EDM between a substring S i, i [ + | Q | ] and Q is at most k , i.e. d S i, i ( [ + | Q ,Q | ] ) ≤ k .

Cormode and Muthukrishnan [7] presented an offline algorithm for computing EDM. In their algorithm, a special type of derivation tree called ESP is constructed for approximately computing EDM. We present an online variant of ESP. Our algorithm approximately solves Problem 1 and is composed of two parts: (i) an online construction of a parse tree space-efficiently and (ii) an approximate computation of EDM from the parse tree. Although our method is an approximation algorithm, it guarantees an upper bound for the exact EDM. We now discuss the two parts in the next section.

## 3 Online Algorithm

OESP builds a special form of CFG and directly encodes it into a succinct representation in an online manner. Such a representation can be used as spaceefficient phrase/reverse dictionaries, which resulted in reducing the working space. In this section, we first present a simple variant of ESP in order to introduce the notion of alphabet reduction and landmark . We then detail OESP and approximate computations of the EDM in an online manner. In the next section, we present an upper bound of the approximate EDM for the exact EDM.

## 3.1 ESP

Given an input string S ∈ Σ ∗ , we decompose the current S into digrams WX or trigrams WXY associated with variables as production rules, and iterate this process while | S | &gt; 1 for the resulting S .

In each iteration, ESP uniquely partitions S into maximal non-overlapping substrings such that S = S S 1 2 · · · S /lscript and each S i is categorized into one of three types, i.e., type1: a repetition of a symbol, type2: a substring not including a type1 substring and of length at least /ceilingleft lg | S |/ceilingright , and type3: a substring being neither type1 nor type2 substrings.

At one iteration of parsing S i , ESP builds two kinds of subtrees from digram WX and trigram WXY , respectively. The first type is a 2-tree corresponding to a production rule in the form of Z → WX . The second type is a 3-tree corresponding to Z → WXY .

ESP parses S i according to its type. In case S i is a type1 or type3 substring, ESP performs the left aligned parsing where 2-trees are built from left to right in S i and a 3-tree is built for the last three symbols if | S i | is odd, as follows:

- -If | S i | is even, ESP builds Z → S i [2 j -1 2 ], , j j = 1 , ..., | S i | / 2,
- -Otherwise, it builds Z → S i [2 j -1 2 ] for , j j = 1 , ..., ( /floorleft| S / i | 2 /floorright -1), and builds Z → S i [2 j -1 2 , j +1] for j = /floorleft| S / i | 2 . /floorright

In case S i is type2, ESP further partitions S i = s s ...s 1 2 /lscript (2 ≤ | s j | ≤ 3) by the alphabet reduction described below, and builds Z → s j for j = 1 , ..., /lscript .

After parsing all S i to S ′ i , ESP continues this process for the resulted string by concatenating all S ′ i ( i = 1 , . . . , /lscript ) at the next level.

Alphabet reduction: Alphabet reduction is a procedure for partitioning a string of type2 into digrams and trigrams. Given S of type2, consider each S i [ ] represented as binary integers. Let p be the position of the least significant bit in which S i [ ] differs from S i [ -1], and let bit ( p, S i [ ]) ∈ { 0 1 , } be the value of S i [ ] at the p -th position, where p starts at 0. Then, L i [ ] = 2 p + bit ( p, S i [ ]) is defined for any i ≥ 2. Since S contains no repetition (i.e., S is type2), the string L defined by L = L [2] L [3] . . . L [ | S | ] is also type2. We note that if the number of different symbols in S is m , denoted by [ S ] = m , clearly [ L ] ≤ 2 lg m . Then, S i [ ] is called landmark if (i) L i [ ] is maximal such that L i [ ] &gt; max { L i [ -1] , L i [ +1] } or (ii) L i [ ] is minimal such that L i [ ] &lt; min { L i [ -1] , L i [ +1] } and not adjacent to any other maximal landmark.

Because L is type2 and [ L ] ≤ lg | S | , any substring of S longer than lg | S | must contain at least one landmark. After deciding all landmarks, if S i [ ] is a landmark, we replace S i [ -1 , i ] by a variable X and update the current dictionary with X → S i [ -1 , i ]. After replacing all landmarks, the remaining substrings are replaced by the left aligned parsing.

## 3.2 Post-order CFG

OESP builds a post-order partial parse tree (POPPT) and directly encodes it into a succinct representation. A partial parse tree defined by Rytter [20] is the ordered tree formed by traversing a parse tree in a depth-first manner and pruning out all descendants under every node of nonterminal symbols appearing no less than twice.

Definition 1 (POPPT and POCFG [15]). A post-order partial parse tree (POPPT) is a partial parse tree whose internal nodes have post-order variables. A post-order CFG (POCFG) is a CFG whose partial parse tree is a POPPT.

Fig. 1. Example of a POCFG, the parse tree of a POCFG, a post-order partial parse tree (POPPT).

![image](https://ac.marcodsn.me/arxiv-markdown-images/1408.0467v2-figure-1646a45a-a779-4a40-b57f-117a4796a7cd.jpg)

Note that the number of nodes in the POPPT is at most 3 n for a POCFG of n variables, because the right-hand sides consist of digrams or trigrams in the production rules and the numbers of internal nodes and leaves are n and at most 2 n , respectively.

Examples of a POCFG and POPPT are shown in Figure 1-i) and iii), respectively. The POPPT is built by traversing the parse tree in Figure 1-ii) in depth-first manner and pruning out all the descendants under the node having the second X 3 . The resulted POPPT in Figure 1-iii) consists of internal nodes having post-order variables.

Amajor advantage of POPPT is that we can directly encode it into a succinct representation which can be used as a phrase dictionary. Such a representation enables us to reduce the working space of OESP by using it in a combination with a reverse dictionary.

## 3.3 Online construction of a POCFG

OESP builds from a given input string a POCFG that guarantees upper bounds of parsing discrepancies between the same substrings in the string. The basic idea of OESP is to (i) start from symbols in an input text, (ii) replace as many as possible of the same digrams or trigrams in common substrings by the same nonterminal symbols, and (iii) iterate this process in a bottom-up manner until it generates a complete POCFG. The POCFG is built in an online manner and the POPPT corresponding to it consists of nodes having two or three children.

OESP builds two types of subtrees in a POPPT from strings XY and WXY . The first type is a 2-tree corresponding to a production rule in the form of Z → XY . The second type is a 3-tree corresponding to a production rule in the form of Z → WXY .

OESP builds a 2-tree or 3-tree from a substring of a limited length. Let u be a string of length m . A function L : ( Σ ∪ V ) m × [ m ] →{ 0 1 , } classifies whether or not the i -th position of u has a landmark, i.e., the i -th position of u has a landmark if L ( u, i ) = 1. L ( u, i ) is computed from a substring u i [ -1 , i + 2] of length four. OESP builds a 3-tree from a substring u i [ +1 , i +3] of length three if the i -th position of u does not have a landmark; otherwise, it builds a 2-tree from a substring u i [ + 2 , i + 3] of length two. The landmarks on a string are

Algorithm 1 Online construction of ESP. D is phrase dictionary, D -1 is reverse dictionary, and q k is queue at level k .

```
Algorithm 1 Online construction of ESP. D is phrase dictionary, D-1 is reverse
dictionary, and q_k is queue at level k.

1:  function OESP
2:      D := 0; initialize queues q_k
3:      while reading a new character c from an input text do
4:         PROCESSSYMBOL(q1, c)
5:      end while
6: end function
7: function PROCESSSYMBOL(qk, X)
8:      qk.enqueue(X)
9:      if qk.size() = 4 then
10:           if L(qk, 2) = 0 then                                                                                                                                                                                                        
1:                    Z := D-1(qk[3], qk[4]); D := D \cup {Z \rightarrow qk[3]qk[4]}
12:                                                                                                                                                                                                       

                                                                                                                                                                                                        }
                                                                                                                                                                                                        */
                                                                                                                                                                                                       */

                                                                                                                                                                                                       */
```

decided such that they are synchronized in long common subsequences to make the parsing discrepancies as small as possible.

The algorithm uses a set of queues, q k , k = 1 , ..., m , where q k processes the string at k -th level of a parse tree of a POCFG and builds 2-trees and 3-trees at each k . Since OESP builds a balanced parse tree, the number m of these queues is bounded by lg N . In addition, landmarks are decided on strings of length at most four, and the length of each queue is also fixed to five. Algorithm 1 consists of the functions OESP and ProcessSymbol .

The main function is OESP which reads new characters from an input text and gives them to the function ProcessSymbol one by one. The function ProcessSymbol builds a POCFG in a bottom-up manner. There are two cases according to whether or not a queue q k has a landmark. For the first case of L ( q k , 2) = 0, i.e. q k does not have a landmark, the 2-tree corresponding to a production rule Z → q k [3] q k [4] in a POCFG is built for the third and fourth elements q k [3] and q k [4] of the k -th queue q k . For the other case, the 3-tree corresponding to a production rule Z → q k [3] q k [4] q k [5] is built for the third, fourth and fifth elements q k [3], q k [4] and q k [5] of the k -th queue q k . In both cases, the reverse dictionary D -1 returns a nonterminal symbol replacing a sequence of symbols. The generated symbol Z is given to the higher q k +1 , which enables the bottom-up construction of a POCFG in an online manner.

The computation time and working space depend on implementations of phrase and reverse dictionaries. The phrase dictionary for a POCFG of n variables can be implemented using a standard array of at most 3 n lg ( n + σ ) bits of space and O (1) access time. In addition, the reverse dictionary can be implemented using a chaining hash table and a phrase dictionary implemented as an array. Thus, the working space of OESP using these data structures is at most

Fig. 2. Succinct representation of a POCFG for a phrase dictionary.

![image](https://ac.marcodsn.me/arxiv-markdown-images/1408.0467v2-figure-51220149-50fa-4abf-9b35-c730ce050314.jpg)

n (4 + α ) lg ( n + σ ) bits. In the following subsections, we present space-efficient representations of phrase/reverse dictionaries.

## 3.4 Compressed phrase dictionary

OESP directly encodes a POCFG into a succinct representation that consists of bit strings B P , and a label sequence L . A bit string B is built by traversing a POPPTandputting c 0s and 1 for a node having c children in the post-order. The final 0 in B represents the super node. We shall call the bit string representation of a POPPT posterior order unary degree sequence (POUDS) . To dynamically build a tree and access any node in the POPPT, we index B by using the dynamic range min/max tree [18]. Our POUDS supports two tree operations: child B, i, j ( ) returns the j -th child of a node i ; num child B, i ( ) returns the number of children for a node i . They are computed in O (lg m/ lg lg m ) time while using 2 m + (1) o bits of space for a tree having m nodes.

A bit string P is built by traversing a POPPT and putting 1 for a leaf and 0 for an internal node in the post-order. P is indexed by the rank/select dictionary [10,17]. The label sequence L stores symbols of leaves in a POPPT.

We can access any element in L as a child of a node i in the following. First, we compute c = num child B, i ( ) and children nodes p = child B, i, j ( ) for j ∈ [1 , c ]. Then, we can compute the positions in L corresponding to the positions of these children as q = rank 1 ( P, p ) that returns the number of occurrences of 1 in P [0 , p ] in O (1) time. We obtain leaf labels as L q [ ]. For a POCFG of n nonterminal symbols, we can access the right-hand side of symbols from the lefthand side of a symbol of a production rule in O (lg n/ lg lg n ) time while using at most n lg ( n + σ ) + 5 n + ( ) bits of space. o n

## 3.5 Compressed reverse dictionary

We implement a reverse dictionary using a chaining hash table that has a load factor α ∈ (0 , 1] in a combination with a phrase dictionary. The hash table has αn entries and each entry stores a list of integers i representing the left-hand side X i of a rule. For the rule X i → S , the hash value is computed from the righthand side S . Then, the list corresponding to the hash value is scanned to search for X i while checking elements referred to as S in a phrase dictionary. Thus, the expected access time is O (1 /α ). The space for a POCFG with n nonterminal symbols is αn lg( n + σ ) bits for the hash table and n lg( n + σ ) bits for the lists, which resulted in n α ( +1)lg( n + σ ) bits in total.

A crucial observation in OESP is that indexes i for nonterminal symbols X i are created in a strictly increasing order. Thus, we can organize each list in a hash table as a strictly increasing sequence of the indexes of nonterminal symbols. We insert a new index i into a list in the hash table, and we append it at the end of the list. Each list in the hash table consists of a strictly increasing sequence of indexes. To make each index smaller, we compute the difference between an index i and the previous one j , and we encode it by the delta code, which resulted in the difference i -j being encoded in 1 + /floorleft lg ( i -j ) /floorright + 2 lg 1 + lg ( /floorleft /floorleft i -j ) /floorright/floorright bits. For all n nonterminal symbols, the space for the lists is upper bounded by n (1 + lg ( αn ) + 2 lg lg ( αn )). bits The space for the hash table is αn lg ( n + σ + n (1+lg( αn ) +2 lg lg ( αn )) bits in total, resulting in αn lg( n + )+ σ n (1+lg( αn )) bits by multiplying the original α by a constant.

Since the reverse dictionary is implemented using the chaining hash and the phrase dictionary, its total space is at most n α ( +1)lg( n + σ ) + n (5+lg ( αn )) + o n ( ) bits. We can obtain the following result.

Lemma 1. For a string length N , OESP builds a POCFG of n nonterminal symbols and its phrase/reverse dictionaries in O ( N lg n α lg lg n ) expected time using at most n α ( +1)lg( n + σ ) + n lg ( αn ) + 5 n + ( ) o n bits of space.

## 3.6 Online pattern matching with EDM

We approximately solve problem 1 by using OESP. First, the parse tree is computed from a query Q by OESP. Let T Q ( ) be a set of node labels in the parse tree for Q . We then compute a vector V ( Q ) each dimension V ( Q e )( ) of which represents the frequency of the corresponding node label e in T Q ( ).

OESP builds another parse tree for a streaming text S in an online manner. T S ( )[ i, i + | Q | ] is a set of node labels included in the subtree corresponding to a substring S i, i [ + | Q | ] from i to i + | Q | in T S ( ). V ( S )[ i, i + | Q | ] can be constructed for each i ∈ [1 , | S | - | Q | ] by adding the node labels corresponding to S i, i [ + | Q | ] and subtracting the node labels not included in T S ( )[ i, i + | Q | ] from V ( S )[ i, i + | Q ]], which can be performed in lg | S | time.

L 1 -distance approximates the EDM between V ( S )[ i, i + | Q | ] and V ( Q ), and it is computed as || V ( S )[ i, i + | Q | ] -V ( Q ) || = ∑ e ∈ ( T S ( )[ i,i + | Q | ∪ ] T Q ( )) | V ( S )[ i, i + | Q | ]( e ) -V ( Q e )( ) . | We obtain the results with respect to computational time and space for computing the L 1 distance from lemma 1 as follows.

Theorem 1. For a streaming text S of length N , OESP approximately solves the problem 1 in O ( N lg N lg n α lg lg n ) expected time using at most n α ( +1)lg( n + σ ) + n lg ( αn ) + 5 n + ( ) o n bits of space.

## 4 Upper Bound of Approximation

We present an upper bound of the approximate EDM in this section.

Fig. 3. Computation time in seconds for the length of text.

![image](https://ac.marcodsn.me/arxiv-markdown-images/1408.0467v2-figure-b04e7e4c-3e9e-4c6e-ae95-f9898309a7f7.jpg)

Theorem 2. || V ( S ) -V ( Q ) || = O (lg 2 m d S, Q ) ( ) for any S, Q ∈ Σ ∗ and m = max {| S , | | Q |} .

/negationslash

/negationslash

Proof. Let e , e 1 2 , . . . , e d be a shortest series of editing operations such that S k +1 = S k ( e k ) where S 1 = S , S d ( e d ) = Q , and d = d S, Q ( ). It is sufficient to prove the assumption: there exists a constant c such that || V ( S ) -V ( Q ) || ≤ c lg 2 m for R e ( ) = S S i . ( ) denotes the string resulted by the i -th iteration of ESP where S (0) = S . Let p , q i i be the smallest integers satisfying S i ( )[ p i ] = Q i ( )[ p i ] and S i ( )[ | S | ( ) i -q i ] = Q i ( )[ | Q i ( ) | -q i ], respectively. We show that q i -p i ≤ lg m +1 for each height i . This derives || V ( S ) -V ( Q ) || ≤ 2 lg m (lg m +1) because i ≤ lg m .

We begin with the case that e is an insertion of a symbol. Clearly, it is true for i = 0 since q 0 -p 0 ≤ 1. We assume the hypothesis on some height i . Let S i ( )[ p ′ ] be the closest landmark from S i ( )[ p i ] with p ′ &lt; p i and S i ( )[ q ′ ] be the closest landmark from S i ( )[ q i ] with q i &lt; q ′ . For the next height, let S i ( + 1) = S S S 1 2 3 such that the tail of S 1 derives S i ( )[ p i ] and the tail of S 2 derives S i ( )[ q i ], and let Q i ( +1) = Q Q Q 1 2 3 such that | Q 1 | = | S 1 | and | Q 3 | = | S 3 | . On any iteration of ESP, the left aligned parsing is performed from a landmark to its closest landmark. It follows that, for S 1 , S 1 [ j ] = Q j 1 [ ] except their tails, for S 2 , | S 2 | ≤ /floorleft 1 2 ( q i -p i ) /floorright ≤ /floorleft 1 2 (lg m +1) , and for /floorright S 3 , we can estimate S 3 [ j ] = Q j 3 [ ] for any j &gt; /floorleft 1 2 lg m /floorright . Thus, q i +1 -p i +1 ≤ 1+ /floorleft 1 2 (lg m +1) + /floorright /floorleft 1 2 lg m /floorright ≤ lg m +1. Since d S, Q ( ) = d Q, S ( ), this bound is true for the deletion of any symbol. The case that e is a replacement is similar.

Moreover, the bound holds for the case of insertion or deletion of any string of length at most lg m . Using this, we can reduce the case of move operation of a substring u as follows. Without loss of generality, we assume u is a type2 substring and let u = xyz such that x/z are the shortest prefix/suffix of u that contain a landmark, respectively. Then, we note that the y inside of u is transformed to a same string for any occurrence of u . Therefore, the case of moving u from S to obtain Q is reduced to the case of deleting x, z at some positions and inserting them into other positions. Since | x , | | z | ≤ lg m , the case of moving u is identical to the case of inserting two symbols and deleting two symbols, i.e., || V ( S ) -V ( Q ) || ≤ 8 lg m (lg m +1).

From theorem 1 and 2, we obtain the following main theorem.

Theorem 3. EDM is O (lg 2 N ) -approximable by the proposed online algorithm with O ( N lg N lg n α lg lg n ) expected time and n α ( +1)lg( n + σ ) + n (5 + lg ( αn )) + o n ( ) bits of space.

![image](https://ac.marcodsn.me/arxiv-markdown-images/1408.0467v2-figure-aad809e0-6382-435e-8ffe-92e7ae7e5af6.jpg)

![image](https://ac.marcodsn.me/arxiv-markdown-images/1408.0467v2-figure-9c112f8f-47cd-4aba-9570-a04ef63b27ec.jpg)

length of text

Fig. 4. Working space of dictionary and hash table for the length of text.

![image](https://ac.marcodsn.me/arxiv-markdown-images/1408.0467v2-figure-3e26ac13-083d-4495-b0f9-2078ef5f78b1.jpg)

![image](https://ac.marcodsn.me/arxiv-markdown-images/1408.0467v2-figure-469da70c-2ebc-4b23-8ab6-0014129bf48f.jpg)

Fig. 5. Working space of a POUDS (B), a label sequence (L) and a bit string (P) which organizes a dictionary.

Fig. 6. The number of substrings whose EDM to a query is no more than each threshold.

![image](https://ac.marcodsn.me/arxiv-markdown-images/1408.0467v2-figure-39e76cec-9c63-4ec3-9bb2-f3492e8e0b9f.jpg)

![image](https://ac.marcodsn.me/arxiv-markdown-images/1408.0467v2-figure-3875ea97-3228-4a57-8df3-59de57c93769.jpg)

Proof. By the theorem 2, we obtain the bound || V ( S i, i [ + | Q | ] -V ( Q ) || = O (lg 2 | Q d S i, i | ) ( [ + | Q ,Q | ] ) for any i ∈ [1 , | S | - | Q | ]. The time complexity is proved by the theorem 1. Thus, for the strings S and Q with N = | S | ≥ | Q | , the result is concluded.

## 5 Experiments

We evaluated OESP on one core of an eight-core Intel Xeon CPU E7-8837 (2.67GHz) machine with 1024GB memory. We used two standard benchmark texts dna.200MB and english.200MB downloadable from http://pizzachili.dcc.uchile.cl/texts.html . We sampled texts of length 100 from these texts as queries. We also used computation time and working space as evaluation measures.

Figure 3 shows computation time for increasing the length of text. The computation time increased linearly for the length of text.

Figure 4 shows working space for increasing the length of text. The space of dictionary was much smaller than that of hash table. The dicionary used 115MB for dna.200MB and 121MB for english.200MB, while the hash table used 368MB for dna.200MB and 382MB for english.200MB.

Table 2. Space for POUDS B , label sequence P and bit string P organizing a dictionary on dna.200MB and english.200MB.

| L[MB]         |         | B[MB]   | P[MB]   |
|---------------|---------|---------|---------|
| dna.200MB     | 89 . 95 | 17 . 62 | 7 . 73  |
| english.200MB | 95 . 72 | 14 . 99 | 8 . 22  |

Figure 5 shows space of a POUDS, a label sequence and a bit string organizing a dictionary for increasing the length of text. The space of dictionary and bit string was much smaller than that of the label sequence for dna.200MB and english.200MB. Table 2 details those space.

Figure 6 shows the number of substring whose EDM to a query is at most a threshold. There were thresholds where the number of substrings dramatically increases. The results showed the applicability of OESP to streaming texts.

## 6 Conclusion

We have presented an online pattern maching for EDM. Our method named OESP is an online version of ESP. A future work is to apply OESP to real world streaming texts.

## References

- 1. V. Bafna and P. A. Pevzner. Genome rearrangements and sorting by reversals. SIAM Jour. on Comp. , 25:272-289, 1996.
- 2. R. Clifford, K. Efremenko, B. Porat, and E. Porat. A black box for online approximate pattern matching. In CPM , pages 143-151, 2008.
- 3. R. Clifford, M. Jalsenius, E. Porat, and B. Sach. Pattern matching in multiple streams. In CPM , pages 97-109, 2012.
- 4. R. Clifford, M. Jalsenius, E. Porat, and B. Sach. Space lower bounds for online pattern matching. Theor. Comp. Sci. , 483:68-74, 2013.
- 5. R. Clifford and B. Sach. Online approximate matching with non-local distances. In CPM , pages 142-153, 2009.
- 6. R. Clifford and B. Sach. Pattern matching in pseudo real-time. JDA , 9:67-81, 2011.
- 7. G. Cormode and S. Muthukrishnan. The string edit distance matching problem with moves. TALG , 3:2:1-2:19, 2007.
- 8. M. Crochemore and W. Rytter. Text Algorithms . Oxford University Press, 1994.
- 9. R. Durbin, S. Eddy, A. Krogh, and G. Mitchison. Biological sequence analysis: probabilistic models of proteins and nucleic acids . Cambridge University Press, 1998.
- 10. G. Jacobson. Space-efficient static trees and graphs. In Proc. of FOCS , pages 549-554, 1989.
- 11. M. Jalsenius, B. Porat, and B. Sach. Parameterized matching in the streaming model. In STACS , pages 400-411, 2013.
- 12. J. D. Kececioglu and D. Sankoff. Exact and approximation algorithms for the inversion distance between two chromosomes. In Proc. of CPM , pages 87-105, 1993.
- 13. V. I. Levenshtein. Binary codes capable of correcting deletions, insertions and reversals. Soviet Physics Doklady , 10:707-710, 1996.
- 14. S. Maruyama and Y. Tabei. Fully-online grammar compression in constant space. In Proc. of DCC , pages 218-229, 2014.
- 15. S. Maruyama, Y. Tabei, H. Sakamoto, and K. Sadakane. Fully-online grammar compression. In Proc. of SPIRE , pages 218-229, 2013.
- 16. S Muthukrishnan and S. C. Sahinalp. Approximate nearest neighbors and sequence comparison with block operations. In Proc. of STOC , pages 416-424, 2000.
- 17. G. Navarro and E. Providel. Fast, small, simple rank/select on bitmaps. In Proc. of SEA , pages 295-306, 2012.
- 18. G. Navarro and K. Sadakane. Fully-functional static and dynamic succinct trees. TALG , 2012. Accepted. A preliminary version appeared in SODA 2010.
- 19. B. Porat and E. Porat. Exact and approximate pattern matching in the streaming model. In FOCS , pages 315-323, 2009.
- 20. W. Rytter. Application of Lempel-Ziv factorization to the approximation of grammar-based compression. Theor. Comp. Sci. , 302(1-3):211-222, 2003.
- 21. D. Shapira and J. A. Storer. Edit distance with move operations. JDA , 5:380-392, 2007.
Yoshimasa Takabatake Yasuo Tabei Hiroshi Sakamoto
Online Pattern Matching for String Edit Distance with Moves
Edit distance with moves (EDM) is a string-to-string distance measure that
includes substring moves in addition to ordinal editing operations to turn one
string to the other. Although optimizing EDM is intractable, it has many
applications especially in error detections. Edit sensitive parsing (ESP) is an
efficient parsing algorithm that guarantees an upper bound of parsing
discrepancies between different appearances of the same substrings in a string.
ESP can be used for computing an approximate EDM as the L1 distance between
characteristic vectors built by node labels in parsing trees. However, ESP is
not applicable to a streaming text data where a whole text is unknown in
advance. We present an online ESP (OESP) that enables an online pattern
matching for EDM. OESP builds a parse tree for a streaming text and computes
the L1 distance between characteristic vectors in an online manner. For the
space-efficient computation of EDM, OESP directly encodes the parse tree into a
succinct representation by leveraging the idea behind recent results of a
dynamic succinct tree. We experimentally test OESP on the ability to compute
EDM in an online manner on benchmark datasets, and we show OESP's efficiency.