# Find the second longest common subsequence?

I am trying to determine that how my sequence(probe) "hits" against a long sequence(genome). The LongestCommonSubsequence in Mathematica gives the first longest common subsequence between two sequences. Is there a good way to find the second longest common subsequence between two sequences? ( Once the probe hits somewhere with longest common subsequence, it will not hit the same region anymore, so that the longest commonsubsequence -1 shouldn't be considered)

I am thinking that one possible way is drop or delete the first longest common subsequence from the long sequence (genome) and run LongestCommonSubsequence again. But obviously this will be really slow considering the genome sequence is big.

• I think you'll have to write your own string-matching code that updates and retains the two longest subsequences as your probe scans your target, and then at the end just return the second-longest subsequence. – David G. Stork Nov 17 '17 at 1:59
• I don't think your proposed solution (of deleting the longest and re-running the algorithm) will work, because the second-longest subsequence might involve characters from the deleted subsequence. – David G. Stork Nov 17 '17 at 2:18
• @DavidG.Stork - that is a good point. Isn't the second longest common subsequence always going to be the longest one minus one character (or tied with it of course)? But I guess that is a trivial 2nd-longest common subsequence, and there could be others of the same length. – Jason B. Nov 17 '17 at 2:38
• @JasonB. If you are allowing non-contiguous subsequences (as is generally the case) then yes... you can merely delete any one of the characters in the longest subsequence. As such, there will be multiple answers, in general. – David G. Stork Nov 17 '17 at 2:44
• @DavidG.Stork. Thanks for the helpful discussion. That's a good point I didn't realize. I guess I should make it more clearly. Once the sequence(probe) hits somewhere with the longest commonsubsequence, it should not hit the same region again. That's why I proposed the solution. But like I said, it could be really slow. I think your method is a good way to go. – Yordan Nov 17 '17 at 3:37