[Paper Review] Computing the Number of Longest Common Subsequences
This paper presents an optimized O(mn) time and O(m) space dynamic programming algorithm to compute the number of distinct longest common subsequences (LCSs) and the number of LCS embeddings between two strings. It simplifies prior approaches by using a bottom-up recurrence that tracks counts through path reconstruction, enabling efficient enumeration without storing all solutions explicitly.
This note provides very simple, efficient algorithms for computing the number of distinct longest common subsequences of two input strings and for computing the number of LCS embeddings.
Motivation & Objective
- To develop a simple and efficient algorithm for counting distinct longest common subsequences (LCSs) between two strings.
- To extend the method to count the number of LCS embeddings—distinct position mappings of an LCS in the input strings.
- To reduce space complexity to O(m) while maintaining O(mn) time complexity for both counting tasks.
- To simplify existing dynamic programming schemes for LCS counting by eliminating redundant computations and storage.
Proposed method
- Use a dynamic programming table L[i,j] to compute the length of the LCS for prefixes A[1..i] and B[1..j], following standard recurrence rules.
- Maintain a second table D[i,j] or E[i] to count distinct LCSs or LCS embeddings, respectively, by aggregating valid paths from previous states.
- Apply recurrence rules that add counts from top (L[i-1,j]) and left (L[i,j-1]) only when they contribute to the same LCS length, subtracting overlap when necessary.
- Optimize space by storing only the current and previous columns of the DP tables, reducing memory to O(m).
- Use temporary variables (newL, newE, oldL, oldE) to update values iteratively in a rolling fashion, avoiding full array storage.
- Modify the logic slightly to switch between counting distinct LCSs and counting embeddings by adjusting conditionals in the update step.
Experimental results
Research questions
- RQ1Can the number of distinct longest common subsequences be computed more efficiently than by enumerating all possible LCSs?
- RQ2What is the minimal space complexity required to compute the count of LCS embeddings?
- RQ3How can the dynamic programming recurrence be simplified to count both distinct LCSs and embeddings in a unified, space-efficient framework?
- RQ4Is it possible to compute the count of LCS embeddings without explicitly generating all embeddings?
Key findings
- The algorithm computes the number of distinct LCSs in O(mn) time and O(m) space, matching the time complexity of standard LCS length computation.
- The same framework efficiently computes the number of LCS embeddings using identical time and space bounds.
- The method avoids storing the full DP table by maintaining only two columns at a time, significantly reducing memory usage.
- The count of distinct LCSs is computed by summing contributions from valid predecessors (top and left) only when they yield the same LCS length, with overlap correction via subtraction.
- The algorithm correctly handles edge cases such as empty strings, where the count is always 1 (the empty subsequence).
- The approach is both simpler and more efficient than prior methods that required storing all solutions or used more complex data structures.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.