feat: add multiple longest common subsequence (MLCS-APP) - #1070
Open
Gaspardcode wants to merge 1 commit into
Open
Gaspardcode wants to merge 1 commit into
Gaspardcode wants to merge 1 commit into
Conversation
Finds a longest common subsequence of any number of strings with the MLCS-APP algorithm (Wang et al., AAAI 2010), an A* variant: - heuristic built from the d - 1 suffix tables M(i,i+1), as in the paper - each layer keeps the points with y - c <= f(p), at most k of them (c = 20 and k = 2000, the values of the paper) - g(q) is only updated through a longer path, as in MLCS-A* - a point is returned only when no other point can beat it (Theorem 1), and a point without successor also ends a subsequence, since h is only an upper bound Supersedes TheAlgorithms#861. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1070 +/- ##
==========================================
- Coverage 95.89% 95.88% -0.02%
==========================================
Files 396 397 +1
Lines 30440 30656 +216
==========================================
+ Hits 29191 29395 +204
- Misses 1249 1261 +12 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds
multiple_longest_common_subsequenceinsrc/string: finds a longest common subsequence of any number of strings (the classic DP only handles two efficiently, MLCS is NP-hard in general).It implements MLCS-APP from A Fast Heuristic Search Algorithm for Finding the Longest Common Subsequence of Multiple Strings (Wang, Pan, Shang, Korkin, AAAI 2010) — paper, an A* variant over the matches of the strings:
h(p)= min of the suffix tablesM(i,i+1)[p_i][p_i+1](only the d − 1 consecutive tables, as in the paper), an upper bound of the remaining LCSf(p) = g(p) + h(p), points ordered byfthenhin aBinaryHeapMTlookup tabley − c ≤ f(p)and at mostkof them, withc = 20andk = 2000(the values used in the paper)Two details go slightly beyond the pseudocode so that the result is always valid and as long as possible:
g(q)is only updated through a longer path (as in MLCS-A*), so the parent chain of a point never gets shorterf(the condition of Theorem 1), and a point without any successor also ends a subsequence, sincehis only an upper bound (e.g.["BB", "BBC", "BC"]hash = 1at the single match but no successor)Tests check the length of the result and that it is a subsequence of every input, since several LCS may exist. Edge cases: no string, a single string, empty strings, no common letter, unicode.
I also cross-checked it locally against an exact DP on 21,300 random inputs (2 to 4 strings, alphabets of 3 and 4 letters): the result was optimal in every case.
This supersedes #861 (closed as stale); its review comments are addressed (
Option<usize>instead of a sentinel,Contextmethods,BinaryHeap, link todoc/paper.pdf).Type of change
Checklist:
cargo clippy --all -- -D warningsjust before my last commit and fixed any issue that was found.cargo fmtjust before my last commit.cargo testjust before my last commit and all tests passed.mod.rsfile within its own folder, and in any parent folder(s).DIRECTORY.mdwith the correct link.COUNTRIBUTING.mdand my code follows its guidelines.The whole MLCS test module runs in ~20 ms.
🤖 Generated with Claude Code