Next Article in Journal
An Algorithm for Managing Aircraft Movement on an Airport Surface
Previous Article in Journal
Linear Time Local Approximation Algorithm for Maximum Stable Marriage
 
 
Font Type:
Arial Georgia Verdana
Font Size:
Aa Aa Aa
Line Spacing:
Column Width:
Background:
Article

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

1
Faculty of Mathematics & Computer Science, Quanzhou Normal University, Quanzhou 362000, China
2
Faculty of Mathematics & Computer Science, Fuzhou University, Fuzhou 350108, China
*
Author to whom correspondence should be addressed.
Algorithms 2013, 6(3), 485-493; https://doi.org/10.3390/a6030485
Submission received: 14 June 2013 / Revised: 15 July 2013 / Accepted: 24 July 2013 / Published: 15 August 2013

Abstract

:
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 find the longest common subsequence, Z, of X and Y that excludes P as a substring. The problem and its solution were first 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 ( n m r ) .

1. 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].
Table 1. The generalized-constrained-longest common subsequence (GC-LCS) problems. STR-EC, string-excluding.
Table 1. The generalized-constrained-longest common subsequence (GC-LCS) problems. STR-EC, string-excluding.
ProblemInputOutput
SEQ-IC-LCSX, Y, and PThe LCS of X and Y that includes P as a subsequence
STR-IC-LCSX, Y, and PThe LCS of X and Y that includes P as a substring
SEQ-EC-LCSX, Y, and PThe LCS of X and Y that includes P as a subsequence
STR-EC-LCSX, Y, and PThe 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.
L ( i , j , k ) = L ( i 1 , j 1 , k ) if   k = 1   and   x i = y j = p k , 1 + max { L ( i 1 , j 1 , k 1 ) , L ( i 1 , j 1 , k ) } if   k 2   and   x i = y j = p k , 1 + L ( i 1 , j 1 , k ) if   x i = y j   and   ( k > 0   and   x i p k ) , max L ( i 1 , j , k ) , L ( i , j 1 , k ) if   x i y j
The boundary conditions of this recursive formula are L ( i , 0 , k ) = L ( 0 , j , k ) = 0 for any 0 i n , 0 j m and 0 k r .
The algorithm presented in [7] was stated without strict proof. Thus, the correctness of the proposed algorithm cannot be guaranteed. For example, if X = a b b b , Y = a a b and P = a b , the values of L ( i , j , k ) , 1 i 4 , 1 j 3 , 0 k 2 computed by recursive Formula (1) are listed in Table 2.
Table 2. L ( i , j , k ) computed by recursive Formula (1).
Table 2. L ( i , j , k ) computed by recursive Formula (1).
k = 0 k = 1 k = 2
i = 1 111000111
i = 2 112001112
i = 3 112001112
i = 4 112001112
From Table 2, we know that the final answer is L ( 4 , 3 , 2 ) = 2 , which is computed by the formula, L ( 4 , 3 , 2 ) = 1 + L ( 3 , 2 , 2 ) , since, in this case, k 2 and a 4 = b 3 = p 2 = b . However, this is a wrong answer, since the correct answer should be one.
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 ( n m r ) .
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 ( n m r ) , 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.

2. A Simple Dynamic Programming Solution

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, 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 = a a b a and S = a a b a a a b , then substring a a b is the longest suffix of S that is also a prefix of P; therefore, σ ( S ) = 3 .
It is readily seen that S P = a a b a a a b a a b a .
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 ) , 1 i n , 1 j m , 0 k r . P [ 1 : k ] is an empty string if k = 0 . The length of an LCS in Z ( i , j , k ) is denoted f ( i , j , k ) .
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.
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 ) .
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):
f ( i , j , k ) = max f ( i + 1 , j + 1 , k ) , 1 + f ( i + 1 , j + 1 , q ) if   x i = y j   and   q < r max f ( i + 1 , j , k ) , f ( i , j + 1 , k ) otherwise
where q = σ ( P [ 1 : k ] x i ) , and the boundary conditions are f ( i , m + 1 , k ) = f ( n + 1 , j , k ) = 0 for any 1 i n , 1 j m and 0 k r .
Proof. 
For any 1 i n , 1 j m and 0 k < r , suppose f ( i , j , k ) = t and z = z 1 , , z t Z ( i , j , k ) .
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 x i z 1 or y j z 1 .
(1.1) If x i z 1 , then z = z 1 , , z t is a common subsequence of X [ i + 1 : n ] and Y [ j : m ] that excludes P as a substring of P [ 1 : k ] z ; thus, f ( i + 1 , j , k ) t . In contrast, f ( i + 1 , j , k ) f ( i , j , k ) = t . 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
f ( i , j , k ) = max f ( i + 1 , j , k ) , f ( i , j + 1 , k )
(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, f ( i + 1 , j + 1 , k ) t . In contrast, f ( i + 1 , j + 1 , k ) f ( i , j , k ) = t . Therefore, we have f ( i , j , k ) = f ( i + 1 , j + 1 , k ) in this case.
(2.2) If x i = y j = z 1 , then f ( i , j , k ) = t > 0 and z = z 1 , , z t is an LCS of X [ i : n ] and Y [ j : m ] that excludes P as a substring of P [ 1 : k ] z , and thus, 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 [ 1 : k ] x i z .
If q = σ ( P [ 1 : k ] x i ) , then P [ 1 : q ] is the longest suffix of P [ 1 : k ] x i that 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 [ 1 : k ] x i z that z = z 2 , , 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 : q ] z . In other words:
f ( i + 1 , j + 1 , q ) t 1 = f ( i , j , k ) 1
In contrast, if P [ 1 : q ] is the longest suffix of P [ 1 : k ] x i , f ( i + 1 , j + 1 , q ) = s and v = v 1 , , v s Z ( i + 1 , j + 1 , q ) , then v is an LCS of X [ i + 1 : n ] and Y [ j + 1 : m ] that excludes P as a substring of P [ 1 : q ] v . In this case, v = x i v is a common subsequence of X [ i : n ] and Y [ j : m ] that excludes P as a substring of P [ 1 : k ] x i v , because P [ 1 : q ] is the longest suffix of P [ 1 : k ] x i and q < r . Therefore:
f ( i , j , k ) s + 1 = f ( i + 1 , j + 1 , q ) + 1
Combining (3) and (4), we have:
f ( i , j , k ) = 1 + f ( i + 1 , j + 1 , q )
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,
f ( i , j , k ) = f ( i + 1 , j + 1 , k ) = max { f ( i + 1 , j , k ) , f ( i , j + 1 , k ) }
The proof is complete.

3. 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 p k t + 1 p k = p 1 p t and x i = p t + 1 . Therefore, in this case, σ ( P [ 1 : k ] x i ) = t + 1 .
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].
Algorithm 1 STR-EC-LCS.
Input: Strings X = x1xn, Y = y1ym of lengths n and m, respectively, and a constraint string, P = p1pr, of lengths r
Output: The length of an LCS of X and Y that excludes P as a substring
  • for all i, j, k , 1 ≤ in, 1 ≤ jm, and 0 ≤ kr do
  • f(i,m + 1, k) ← 0, f(n + 1, j, k) ← 0 {boundary conditiong}
  • end for
  • for i = n down to 1 do
  • for j = m down to 1 do
  •   for k = 0 to r − 1 do
  •    f(i, j, k) ← max{f(i + 1, j, k), f(i, j + 1, k)}
  •    q ←σ(P[1 : k] ⊕ xi)
  •    if xi = yj q < r then
  •     f(i, j, k) ← max{f(i + 1, j + 1, k), 1 + f(i + 1, j + 1, q)}
  •    end if
  •   end for
  • end for
  • end for
  • return f(1, 1, 0)
For a given string, S = s 1 s n , the prefix function, k m p ( i ) , denotes the length of the longest prefix of s 1 s i 1 that matches a suffix of s 1 s i . For example, if S = a b a b a a , then k m p ( 1 ) , , k m p ( 6 ) = 0 , 0 , 1 , 2 , 3 , 1 .
For the constraint string, P = p 1 p r , of length r, its prefix function, k m p , can be pre-computed in O ( r ) time by Algorithm 2.
Algorithm 2 Prefix Function.
Input: Strings P = p1pr
Output: The prefix function kmp of P
  • kmp(0) ← −1
  • for i = 2 to r do
  • k ← 0
  • while k ≥ 0 pk+1pi do
  •   kkmp(k)
  • end while
  • kk + 1
  • kmp(i) ← k
  • end for
With this pre-computed prefix function, k m p , the function, σ ( P [ 1 : k ] c h ) , for each character, c h and 1 k r , can be described as Algorithm 3.
To accelerate the processing, we can pre-compute a table, λ ( k , c h ) , of the function, σ ( P [ 1 : k ] c h ) , for each character, c h and 1 k r . It is clear that λ ( k 1 , P [ k ] ) = k for each 1 k r . The other values of the table, λ, can be computed by using the prefix function, k m p , in the following recursive Algorithm 4.
Algorithm 3 σ(k, ch).
Input: Strings P = p1pr, integer k and character ch
Output: σ(P[1 : k] ⊕ ch)
  • while k ≥ 0 pk+1ch do
  • kkmp(k)
  • end while
  • return k + 1
Algorithm 4 λ(k, ch).
Input: Integer k, character ch
Output: Value of λ(k, ch)
  • if k > 0 λ(k, ch) = 0 then
  •  λ(k, ch) ← λ(kmp(k), ch)
  • end if
  • return λ(k, ch)
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 ] c h ) for each character, c h 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 ( n m r ) 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, b a c k ( 1 , 1 , 0 ) , will produce the resultant LCS accordingly.
Because the cost of the computation of λ ( k , x i ) is O ( 1 ) , the algorithm, b a c k ( i , j , k ) , will cost O ( n + m ) in the worst case.
Finally, we summarize our results in the following theorem:
Theorem 2
Algorithm 1 solves the STR-EC-LCS problem correctly in O ( n m r ) time and O ( n m r ) space, with preprocessing time O ( r | Σ | ) .
Algorithm 5 back(i, j, k).
Comments: A recursive back tracing algorithm to construct the actual LCS.
  • if i > n j > m then
  • return
  • end if
  • if xi = yj f(i, j, k) = 1 + f(i + 1, j + 1, λ(k, xi)) then
  • print xi
  • back(i + 1, j + 1, λ(k, xi))
  • else if f(i + 1, j, k) > f(i, j + 1, k) then
  • back(i + 1, j, k)
  • else
  • back(i, j + 1, k)
  • enf if

4. Conclusions

We have suggested a new dynamic programming solution for the STR-EC-LCS problem. The new algorithm corrects a previously presented dynamic programming algorithm with the same time and space complexities.
The STR-IC-LCS problem is another interesting GC-LCS, which is very similar to the STR-EC-LCS problem.
The STR-IC-LCS problem, introduced in [7], is to find an LCS of two main sequences, in which a constraining sequence of length r must be included as its substring. In [7], an O ( n m r ) -time algorithm was presented to solve this problem. Almost immediately, the presented algorithm was improved to a quadratic-time algorithm and to accept many main input sequences [9,10].
It is not clear whether the same improvement can be applied to our presented O ( n m r ) -time algorithm for the STR-EC-LCS problem to achieve a quadratic-time algorithm. We will investigate the problem further.

Acknowledgments

The authors acknowledge the financial support of the Natural Science Foundation of Fujian under Grant No. 2013J0101 and the Haixi Project of Fujian under Grant No. A099.
The authors are grateful to the anonymous referee for a careful checking of the details and for helpful comments that improved this paper.

Conflict of Interest

The authors declare no conflict of interest.

References

  1. Tang, C.Y.; Lu, C.L. Constrained multiple sequence alignment tool development and its application to RNase family alignment. J. Bioinform. Comput. Biol. 2003, 1, 267–287. [Google Scholar] [CrossRef] [PubMed]
  2. Tsai, Y.T. The constrained longest common subsequence problem. Inf. Process. Lett. 2003, 88, 173–176. [Google Scholar] [CrossRef]
  3. Deorowicz, S.; Obstoj, J. Constrained longest common subsequence computing algorithms in practice. Comput. Inf. 2010, 29, 427–445. [Google Scholar]
  4. Iliopoulos, C.S.; Rahman, M.S. A new efficient algorithm for computing the longest common subsequence. Theory Comput. Sci. 2009, 45, 355–371. [Google Scholar] [CrossRef]
  5. Peng, Y.H.; Yang, C.B.; Huang, K.S.; Tseng, K.T. An algorithm and applications to sequence alignment with weighted constraints. Int. J. Found. Comput. Sci. 2010, 21, 51–59. [Google Scholar] [CrossRef]
  6. Gotthilf, Z.; Hermelin, D.; Landau, G.M.; Lewenstein, M. Restricted LCS. In Proceedings of the 17th International Conference on String Processing and Information Retrieval, SPIRE’10, Los Cabos, Mexico, 11–13 October 2010; pp. 250–257.
  7. Chen, Y.C.; Chao, K.M. On the generalized constrained longest common subsequence problems. J. Comb. Optim. 2011, 21, 383–392. [Google Scholar] [CrossRef]
  8. Knuth, D.E.; Morris, J.H., Jr.; Pratt, V. Fast pattern matching in strings. SIAM J. Comput. 1977, 6, 323–350. [Google Scholar] [CrossRef]
  9. Deorowicz, S. Quadratic-time algorithm for a string constrained LCS problem. Inf. Process. Lett. 2012, 112, 423–426. [Google Scholar] [CrossRef]
  10. Tseng, C.T.; Yang, C.B.; Ann, H.Y. Efficient algorithms for the longest common subsequence problem with sequential substring constraints. J. Complex. 2013, 29, 44–52. [Google Scholar] [CrossRef]

Share and Cite

MDPI and ACS Style

Zhu, D.; Wang, X. A Simple Algorithm for Solving for the Generalized Longest Common Subsequence (LCS) Problem with a Substring Exclusion Constraint. Algorithms 2013, 6, 485-493. https://doi.org/10.3390/a6030485

AMA Style

Zhu D, Wang X. A Simple Algorithm for Solving for the Generalized Longest Common Subsequence (LCS) Problem with a Substring Exclusion Constraint. Algorithms. 2013; 6(3):485-493. https://doi.org/10.3390/a6030485

Chicago/Turabian Style

Zhu, Daxin, and Xiaodong Wang. 2013. "A Simple Algorithm for Solving for the Generalized Longest Common Subsequence (LCS) Problem with a Substring Exclusion Constraint" Algorithms 6, no. 3: 485-493. https://doi.org/10.3390/a6030485

APA Style

Zhu, D., & Wang, X. (2013). A Simple Algorithm for Solving for the Generalized Longest Common Subsequence (LCS) Problem with a Substring Exclusion Constraint. Algorithms, 6(3), 485-493. https://doi.org/10.3390/a6030485

Article Metrics

Back to TopTop