How to calculate the number of longest common subsequences

I am trying to calculate the number of the longest possible subsequences existing between two lines.

eg. String X = "efgefg"; String Y = "efegf";

: The number of the longest common sequences: 3 (i.e.: efeg, efef, efgf - this does not need to be calculated by the algorithm, as shown here for demonstration)

I managed to do this in O (| X | * | Y |) using dynamic programming based on the general idea here: The cheapest path algorithm .

Can anyone think of a way to efficiently calculate with better execution efficiency?

- Edited in response to Jason's comment.

+3
source share
4 answers

The longest common subsequence issue is the well-studied CS issue.

You can read here: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem

+1
source

I don’t know, but here are some attempts to ponder:

The worst case that I could build has an exponent - 2 ** (0.5 | X |) - the number of the longest common subsequences:

X = "aAbBcCdD..."
Y = "AaBbCcDd..."

where the longest common subsequences include exactly one of {A, a}, exactly one of {B, b} etc ... (nitpicking: if your alphabet is limited to 256 characters, this will eventually break - but 2 * * 128 is already huge.)

. O (| X | * | Y |), ! , , , .

0

, , n O (n 2-ε) , , .: https://arxiv.org/abs/1412.0348

, O (n 2-ε) . , O (n 2) . O (n 2/log (n)) .

, ? , # P-complete counting. , , , , # P-:

S. , . . . . . ACM-SIAM (SODA), . 551-557, 1995

, , , . , , , . , ( , , ..).

So, if you want to calculate the number of different subsequences of two sequences, then most likely your current algorithm is incorrect, and any algorithm cannot calculate it in polynomial time if P = NP (or more ...).

0
source

Best explanation (with code) I found:

Count all LCS

0
source

Source: https://habr.com/ru/post/1732362/


All Articles