A Simple Algorithm for Solving for the Generalized Longest Common Subsequence (LCS) Problem with a Substring Exclusion Constraint

: This paper studies the string-excluding (STR-EC)-constrained longest common subsequence (LCS) problem, a generalized LCS problem. For the two input sequences, X and Y , of lengths n and m and a constraint string, P , of length r , the goal is to ﬁnd the longest common subsequence, Z , of X and Y that excludes P as a substring. The problem and its solution were ﬁrst proposed by Chen and Chao, but we found that their algorithm cannot solve the problem correctly. A new dynamic programming solution for the STR-EC-LCS problem is then presented in this paper, and the correctness of the new algorithm is proven. The time complexity of the new algorithm is O ( nmr ) .


Introduction
In this paper, we consider a generalized longest common subsequence problem.The longest common subsequence (LCS) problem is a well-known measurement for computing the similarity of two strings.This problem can be widely applied in diverse areas, such as file comparison, pattern matching and computational biology [1].
A sequence is an ordered list of characters over an alphabet, .A subsequence of a sequence, X, is obtained by deleting zero or more characters (not necessarily contiguous) from X.A substring of a sequence, X, is a subsequence of successive characters within X.
For a given sequence, X = x 1 x 2 • • • x n , of length n, the ith character of X is denoted, x i ∈ , for any i = 1, • • • , n.A substring of X from position i to j can be denoted as X[i : j] = x i x i+1 • • • x j .A substring, X[i : j], is called a prefix of X if i = 1 and a suffix of X if j = n.
Given two sequences, X and Y , the LCS problem is finding a subsequence of X and Y whose length is the longest among all common subsequences of the two given sequences.
For some biological applications, some constraints must be applied to the LCS problem.These types of variants of the LCS problem are called constrained LCS (CLCS) problems [2].
A recent variant of the LCS problem, which was first addressed in [2], has received considerable attention.The most cited algorithms solve the CLCS problem based on dynamic programming algorithms.Some improved algorithms have also been proposed in [3,4].The LCS and CLCS problems on indeterminate strings were also discussed in [4].A bit-parallel algorithm for solving the CLCS problem was proposed in [3].The problem was extended to have weighted constraints, a more generalized problem, in [5].A variant of the CLCS problem with multiple constraints, the restricted LCS problem, which excludes the given constraint as a subsequence of the answer, was proposed in [6].This restricted LCS problem becomes non-deterministic polynomial-time hard (NP-hard) when the number of constraints is not fixed [6].
Recently, Chen and Chao [7] proposed a more generalized form of the CLCS problem, the generalized-constrained-LCS (GC-LCS) problem.For the two input sequences, X and Y , of lengths n and m, respectively, and a constraint string, P , of length r, the GC-LCS problem is a set of four problems that find the LCS of X and Y that includes/excludes P as a subsequence/substring.The four generalized constrained LCSs are summarized in Table 1 [7].

Problem
Input Output SEQ-IC-LCS X, Y , and P The LCS of X and Y that includes P as a subsequence STR-IC-LCS X, Y , and P The LCS of X and Y that includes P as a substring SEQ-EC-LCS X, Y , and P The LCS of X and Y that includes P as a subsequence STR-EC-LCS X, Y , and P The LCS of X and Y that includes P as a substring We will discuss the STR-EC-LCS problem in this paper.We found that a previously proposed dynamic programming algorithm for the STR-EC-LCS problem [7] cannot correctly solve the problem.Let L(i, j, k) denote the length of an LCS of X[1 : i] and Y [1 : j], excluding P [1 : k] as a substring.Chen and Chao gave a recursive Formula (1) for computing L(i, j, k) as follows.
The algorithm presented in [7] was stated without strict proof.Thus, the correctness of the proposed algorithm cannot be guaranteed.For example, if X = abbb, Y = aab and P = ab, the values of 1) are listed in Table 2. Table 2. L(i, j, k) computed by recursive Formula (1).
A new dynamic solution for the STR-EC-LCS problem is presented in this paper, and the correctness of the new algorithm is proven.The time complexity of the new algorithm is O(nmr).
The organization of the paper is as follows.
In the following three sections, we describe our dynamic programming algorithm for the STR-EC-LCS problem.
In Section 2, we present a new dynamic programming solution for the STR-EC-LCS problem with time complexity, O(nmr), from a novel perspective.In Section 3, we discuss the issues involved in implementing the algorithm efficiently.Some concluding remarks are provided in Section 4.

A Simple Dynamic Programming Solution
For the two input sequences, of lengths n and m, respectively, and a constraint string, P = p 1 p 2 • • • p r , of length r, we want to find an LCS of X and Y that excludes P as a substring.
In the description of our new algorithm, a function, σ, will be mentioned frequently.For any string, S, and a fixed constraint string, P , the length of the longest suffix of S that is also a prefix of P is denoted by the function, σ(S).
The function, σ, refers to both P and S. Because the string, S, is a variable and the constraint string, P , is fixed, the notation, σ(S), will not cause confusion, even though it does not reflect its dependence on P .
The symbol, ⊕, is also used to denote string concatenation.For example, if P = aaba and S = aabaaab, then substring aab is the longest suffix of S that is also a prefix of P ; therefore, σ(S) = 3.
It is readily seen that S ⊕ P = aabaaabaaba.
Let Z(i, j, k) denote the set of all LCSs of X[i : n] and Y [j : m] that exclude P as a substring of If we can compute f (i, j, k) for any 1 ≤ i ≤ n, 1 ≤ j ≤ m and 0 ≤ k < r efficiently, then the length of an LCS of X and Y that excludes P as a substring must be f (1, 1, 0).
We can obtain a recursive formula for computing f (i, j, k) with the following theorem.The length of an LCS in Z(i, j, k) is denoted, f (i, j, k).
For any 1 ≤ i ≤ n, 1 ≤ j ≤ m and 0 ≤ k < r, f (i, j, k) can be computed with the following recursive Formula (2): where q = σ(P [1 : k] ⊕ x i ), and the boundary conditions are Proof.For any First, we note that for each pair, (i , j ), 1 ≤ i ≤ n, 1 ≤ j ≤ m, such that i ≥ i and j ≥ j, we have f (i , j , k) ≤ f (i, j, k), because a common subsequence, z, of X[i : n] and Y [j : m] that excludes P as a substring of P [1 : k] ⊕ z is also a common subsequence of X[i : n] and Y [j : m] that excludes P as a substring of P [1 : k] ⊕ z.
(1) When x i = y j , we have Therefore, in this case, we have f (i, j, k) = f (i + 1, j, k).
(1.2) If y j = z 1 , then in a similar manner, we can prove that f (i, j, k) = f (i, j + 1, k) in this case.
Combining the two subcases, we conclude that when x i = y j , we have (2) When x i = y j and q < r, there are also two subcases to be distinguished.
(2.1) If x i = y j = z 1 , then z = z 1 , • • • , z t is also a common subsequence of X[i + 1 : n] and Y [j + 1 : m] that excludes P as a substring of P [1 : k] ⊕ z and, thus, and Y [j : m] that excludes P as a substring of P [1 : k] ⊕ z, and thus, is also a prefix of P .It follows that P [1 : q] ⊕ z is a suffix of P [1 : k] ⊕ x i ⊕ z .Therefore, a sequence that excludes P as a substring of P [1 : k] ⊕ x i ⊕ z is also a sequence that excludes P as a substring of P [1 : q] ⊕ z .It follows from the fact that z = z 2 , • • • , z t is a common subsequence of X[i + 1 : n] and Y [j + 1 : m] that excludes P as a substring of P also a common subsequence of X[i + 1 : n] and Y [j + 1 : m] that excludes P as a substring of P [1 : q] ⊕ z .In other words: In contrast, if Combining ( 3) and ( 4), we have: Combining the two subcases, where x i = y j and q < r, we conclude that the recursive Formula ( 2) is correct for this case.
(3) When x i = y j and q = r, we must have x i = y j = z 1 ; otherwise, P [1 : k] ⊕ z will include the string, P [1 : k] ⊕ x i = P .Similar to Subcase (2.1), we can conclude that in this case, The proof is complete.

Implementation of the Algorithm
According to Theorem 1, our new algorithm for computing f (i, j, k) is a standard dynamic programming algorithm.With the recursive Formula (2), the new dynamic programming algorithm for computing f (i, j, k) can be implemented as the following Algorithm 1.
To implement our new algorithm efficiently, it is important to compute σ(P [1 : k] ⊕ x i ) for each 0 ≤ k < r and x i , where 1 ≤ i ≤ n efficiently in line 8.
It is clear that σ(P [1 : k] ⊕ x i ) = k + 1 when x i = p k+1 .It will be more complex to compute σ(P [1 : k] ⊕ x i ) when x i = p k+1 .In this case, the length of the matched prefix of P must be shortened to the largest t < k, such that This computation is very similar to the computation of the prefix function in the Knuth-CMorris-CPratt string searching algorithm (KMP algorithm) for solving the string matching problem [8].
lengths n and m, respectively, and a constraint string, P = p 1 • • • p r , of lengths r Output: The length of an LCS of X and Y that excludes P as a substring 1: for all i, j, k , 1 ≤ i ≤ n, 1 ≤ j ≤ m, and 0 ≤ k ≤ r do for k = 0 to r − 1 do 7: if x i = y j q < r then 10: To accelerate the processing, we can pre-compute a table, λ(k, ch), of the function, σ(P The other values of the table, λ, can be computed by using the prefix function, kmp, in the following recursive Algorithm 4. Algorithm 3 σ(k, ch).Input:  The time cost of the above preprocessing algorithm is clearly O(r|Σ|).By using this pre-computed table, λ, the value of function σ(P [1 : k] ⊕ ch) for each character, ch ∈ and 1 ≤ k < r, can be computed readily in O(1) time.
With this pre-computed table, λ, the loop body of the above Algorithm 1 requires only O(1) time, because λ(k, x i ) can be computed in O(1) time for each x i , 1 ≤ i ≤ n and any 0 ≤ k < r.Therefore, our new algorithm for computing the length of an LCS of X and Y that excludes P as a substring requires O(nmr) time and O(r|Σ|) preprocessing time.
If we want to obtain the actual LCS of X and Y that excludes P as a substring, not only its length, we can also present a simple recursive back-tracing algorithm for this purpose as Algorithm 5.
At the end of our new algorithm, a function call, back(1, 1, 0), will produce the resultant LCS accordingly.
Because the cost of the computation of λ(k, x i ) is O(1), the algorithm, back(i, j, k), will cost O(n + m) in the worst case.
Finally, we summarize our results in the following theorem:

Theorem 1
For the two input sequences, X = x 1 x 2 • • • x n and Y = y 1 y 2 • • • y m , of lengths n and m, respectively, and a constraint string, P = p 1 p 2 • • • p r , of length r, let Z(i, j, k) denote the set of all LCSs of X[i : n] and Y [j : m] that exclude P as a substring of P [1 : k] ⊕ z for each z ∈ Z(i, j, k).

Theorem 2
Algorithm 1 solves the STR-EC-LCS problem correctly in O(nmr) time and O(nmr) space, with preprocessing time O(r|Σ|).