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, , of length n, the ith character of X is denoted, , for any . A substring of X from position i to j can be denoted as . A substring, , is called a prefix of X if and a suffix of X if .
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.
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
denote the length of an LCS of
and
, excluding
as a substring. Chen and Chao gave a recursive Formula (1) for computing
as follows.
The boundary conditions of this recursive formula are
for any
and
.
The algorithm presented in [
7] was stated without strict proof. Thus, the correctness of the proposed algorithm cannot be guaranteed. For example, if
and
, the values of
computed by recursive Formula (1) are listed in
Table 2.
Table 2.
computed by recursive Formula (1).
Table 2.
computed by recursive Formula (1).
| | | | | | | | | |
---|
| 1 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 |
| 1 | 1 | 2 | 0 | 0 | 1 | 1 | 1 | 2 |
| 1 | 1 | 2 | 0 | 0 | 1 | 1 | 1 | 2 |
| 1 | 1 | 2 | 0 | 0 | 1 | 1 | 1 | 2 |
From
Table 2, we know that the final answer is
, which is computed by the formula,
, since, in this case,
and
. 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 .
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,
, 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, and , of lengths n and m, respectively, and a constraint string, , 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, .
The function, σ, refers to both P and S. Because the string, S, is a variable and the constraint string, P, is fixed, the notation, , 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 and , then substring is the longest suffix of S that is also a prefix of P; therefore, .
It is readily seen that .
Let denote the set of all LCSs of and that exclude P as a substring of for each . is an empty string if . The length of an LCS in is denoted .
If we can compute for any and efficiently, then the length of an LCS of X and Y that excludes P as a substring must be .
We can obtain a recursive formula for computing
with the following theorem.
Theorem 1 For the two input sequences, and , of lengths n and m, respectively, and a constraint string, , of length r, let denote the set of all LCSs of and that exclude P as a substring of for each .
The length of an LCS in is denoted, .
For any and , can be computed with the following recursive Formula (2):where , and the boundary conditions are for any and .
Proof. For any and , suppose and .
First, we note that for each pair, , such that and , we have , because a common subsequence, z, of and that excludes P as a substring of is also a common subsequence of and that excludes P as a substring of .
(1) When , we have or .
(1.1) If , then is a common subsequence of and that excludes P as a substring of ; thus, . In contrast, . Therefore, in this case, we have .
(1.2) If , then in a similar manner, we can prove that in this case.
Combining the two subcases, we conclude that when
, we have
(2) When and , there are also two subcases to be distinguished.
(2.1) If , then is also a common subsequence of and that excludes P as a substring of and, thus, . In contrast, . Therefore, we have in this case.
(2.2) If , then and is an LCS of and that excludes P as a substring of , and thus, is a common subsequence of and that excludes P as a substring of .
If
, then
is the longest suffix of
that is also a prefix of
P. It follows that
is a suffix of
. Therefore, a sequence that excludes
P as a substring of
is also a sequence that excludes
P as a substring of
. It follows from the fact that
is a common subsequence of
and
that excludes
P as a substring of
that
is also a common subsequence of
and
that excludes
P as a substring of
. In other words:
In contrast, if
is the longest suffix of
,
and
, then
v is an LCS of
and
that excludes
P as a substring of
. In this case,
is a common subsequence of
and
that excludes
P as a substring of
, because
is the longest suffix of
and
. Therefore:
Combining (
3) and (
4), we have:
Combining the two subcases, where
and
, we conclude that the recursive Formula (
2) is correct for this case.
(3) When
and
, we must have
; otherwise,
will include the string,
. Similar to Subcase (2.1), we can conclude that in this case,
The proof is complete. ■
3. Implementation of the Algorithm
According to Theorem 1, our new algorithm for computing
is a standard dynamic programming algorithm. With the recursive Formula (
2), the new dynamic programming algorithm for computing
can be implemented as the following Algorithm 1.
To implement our new algorithm efficiently, it is important to compute for each and , where efficiently in line 8.
It is clear that when . It will be more complex to compute when . In this case, the length of the matched prefix of P must be shortened to the largest , such that and . Therefore, in this case, .
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 = x1 … xn, Y = y1 … ym of lengths n and m, respectively, and a constraint string,
P = p1 … pr, of lengths r Output: The length of an LCS of X and Y that excludes P as a substringfor all i, j, k , 1 ≤ i ≤ n, 1 ≤ j ≤ m, and 0 ≤ k ≤ r 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, , the prefix function, , denotes the length of the longest prefix of that matches a suffix of . For example, if , then .
For the constraint string,
, of length
r, its prefix function,
, can be pre-computed in
time by Algorithm 2.
Algorithm 2 Prefix Function. |
Input: Strings P = p1 … pr Output: The prefix function kmp of Pkmp(0) ← −1 for i = 2 to r do k ← 0 while k ≥ 0 pk+1 ≠ pi do k ← kmp(k) end while k ← k + 1 kmp(i) ← k end for
|
With this pre-computed prefix function, , the function, , for each character, and , can be described as Algorithm 3.
To accelerate the processing, we can pre-compute a table,
, of the function,
, for each character,
and
. It is clear that
for each
. The other values of the table,
λ, can be computed by using the prefix function,
, in the following recursive Algorithm 4.
Algorithm 3 σ(k, ch). |
Input: Strings P = p1 … pr, integer k and character ch Output: σ(P[1 : k] ⊕ ch)
while k ≥ 0 pk+1 ≠ ch do k ← kmp(k) end while return k + 1
|
Algorithm 4 λ(k, ch). |
Input: Integer k, character ch Output: Value of λ(k, ch) |
The time cost of the above preprocessing algorithm is clearly . By using this pre-computed table, λ, the value of function for each character, and , can be computed readily in time.
With this pre-computed table, λ, the loop body of the above Algorithm 1 requires only time, because can be computed in time for each and any . Therefore, our new algorithm for computing the length of an LCS of X and Y that excludes P as a substring requires time and 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, , will produce the resultant LCS accordingly.
Because the cost of the computation of is , the algorithm, , will cost 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 time and space, with preprocessing time .
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
-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 -time algorithm for the STR-EC-LCS problem to achieve a quadratic-time algorithm. We will investigate the problem further.