The longest common subsequence problem studies finding the longest subsequence common to two sequences. It covers algorithmic approaches to solve it.
The Longest Common Subsequence problem centers on identifying a subsequence that is shared by two or more sequences while being as long as possible. Crucially, unlike substrings, these subsequences do not need to occupy contiguous positions within their original sequences. This relaxed constraint fundamentally alters the problem’s combinatorial landscape and algorithmic approach.
When considering two input sequences of lengths \(n\) and \(m\), every subsequence of the first sequence potentially qualifies as a candidate to be checked against the second sequence. The number of possible subsequences for a sequence of length \(n\) is exponential—specifically \(2^n\)—because each element can be either included or excluded independently. A naive brute-force approach would therefore involve enumerating all these subsequences and verifying their presence in the other sequence(s). This leads to a worst-case time complexity on the order of \(O\left(2^{n_{1}} \sum_{i>1} n_i \right),\) where \(n_1\) is the length of the first sequence and subsequent sequences have lengths \(n_i\). Since this grows exponentially with input size even for two sequences, it quickly becomes infeasible as sequence lengths increase[1].
The core mechanism that enables efficient computation of an LCS between two sequences relies on its optimal substructure and overlapping subproblems properties. Optimal substructure means that an optimal solution to the entire problem can be constructed from optimal solutions to its subproblems. Overlapping subproblems imply that these subproblems are not independent; rather, many share calculations that can be cached.
The dynamic programming formulation harnesses these properties by building a two-dimensional table of size \((m+1) \times (n+1)\), where each entry corresponds to the length of an LCS between prefixes:
\[
f[i][j] = \text{length of LCS between } X_i \text{ and } Y_j,
\]
with prefixes defined such that \(X_i = (x_1 x_2 ... x_i), \quad Y_j = (y_1 y_2 ... y_j).\).
This bottom-up approach builds solutions incrementally from trivial base cases—empty prefixes—with zero-length subsequences across any empty string boundary[4].
The underlying recurrence relation encodes how to extend or skip characters during subsequence formation:
If the characters at positions \(i\) in string X and \(j\) in string Y match, \(x_i = y_j,\) then \(f[i][j] = f[i - 1][j - 1] + 1,\) reflecting extension by one character along the diagonal predecessor.
If they do not match, \(f[i][j] = \max(f[i - 1][j], f[i][j - 1]),\) which captures skipping either character from one string while preserving maximality.
This decision mechanism ensures that at each prefix pair, we choose whether to include matching characters or discard mismatches optimally[4].
Two key properties facilitate this dynamic programming solution:
First Property: For any strings \(X,Y\), appending a common character A at their ends satisfies \(LCS(X^A,Y^A)=LCS(X,Y)^A,\) where ^ denotes concatenation. This property guarantees that once matched symbols at suffixes are found, they can safely extend existing solutions without backtracking[1].
Second Property: If A and B are distinct symbols (A ≠ B), then \(LCS(X^A,Y^B)\) is one of the maximal-length strings in the set { \(LCS(X^A,Y)\), \(LCS(X,Y^B)\) }, ensuring any mismatch forces consideration only of smaller prefix problems where one suffix character is discarded[1]. This dichotomy maintains correctness while pruning impossible matches.
Extending beyond two sequences escalates complexity sharply. For a constant number of sequences (\( N > 2)), dynamic programming still applies but with time complexity:
O\left(N \prod_{i=1}^{N} n_i \right),
multiplying all input lengths together due to multidimensional tables. However, when the number of sequences grows arbitrarily large or unbounded, problem complexity becomes NP-hard because enumerating all common subsequences explodes exponentially[1]. The combinatorial breadth grows too vast for polynomial-time solutions without heuristic constraints.
In practice, implementations rely on efficiently filling a DP matrix row by row or column by column using nested loops iterating over indices up to lengths m and n respectively. Each cell computation involves constant-time comparisons and maximum operations resulting in overall time complexity:
O(m \times n),
with space requirements also proportional to matrix size unless optimized further[4].
For example, comparing "ace" and "aec", both length three strings, produces a DP table dimensioned as \(4\times4, \)
where indices range from zero representing empty prefixes up to full strings.
The final DP cell value at f[3][3]=2 indicates a longest common subsequence length of two characters ("ac"), which matches intuitive inspection but was derived systematically via incremental computations[4].
One subtlety lies in indexing conventions: strings use zero-based indexing internally while DP tables conceptually start at one for prefix counts. Misalignments here cause frequent off-by-one errors if carelessly handled during implementation. Additionally, space optimization attempts require preserving access to diagonal predecessor values (\(f[i - 1][j - 1]\)) even when compressing storage into one-dimensional arrays—loss of which breaks correctness[4]. These practical constraints illustrate how underlying theoretical mechanisms reflect intricate operational details.
The LCS is rarely unique; multiple different subsequences may share maximal length given identical inputs. Because subsequences need not be contiguous or unique symbol sets globally, alternative paths through DP tables yield diverse valid outputs with equal optimality. This multiplicity further complicates exhaustive enumeration algorithms whose runtime must accommodate potentially exponential numbers of maximum-length common subsequences[1].
Through these mechanisms—the combinatorial explosion curtailed by optimal substructures exploited via dynamic programming recurrences respecting precise prefix relations—the Longest Common Subsequence problem achieves tractability for pairs or small sets of sequences but remains computationally intensive beyond those bounds.
[1] https://en.wikipedia.org/wiki/Longest_common_subsequence
[2] https://www.geeksforgeeks.org/dsa/longest-common-subsequence-dp-4/
[3] https://cp-algorithms.com/dynamic_programming/longest_increasing_s...
[4] https://algo.monster/liteproblems/1143
[5] https://medium.com/@prajun_t/dynamic-programming-longest-common-su...
Generating summary…