lcs or Longest Common Subsequence is a problem in search optimization: Given two strings, find the common subsequence in given strings with maximum length. The problem can be solved in polynomial time using dynamic programming approach.
Source: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
About
lcs or Longest Common Subsequence is a problem in search optimization: Given two strings, find the common subsequence in given strings with maximum length. The problem can be solved in polynomial time using dynamic programming approach. The algorithm for solution to this problem is recursive and gives the following recursive formula.
If i == 0 or j == 0 then
C[i][j] = 0
If i,j > 0 and xi == yi then
C[i][j] = c[i-1,j-1]+1
If i,j > 0 and xi != yi
then
C[i][j] = max(c[i,j-1],c[i-1,j ])
Pseudocode
p = A.length
q = B.length
for i = 1 to p
c[i,0] = 0
for i = 1 to q
c[0,j] = 0
for i = 1 to p
for j = 1 to q
if i ==0 or j == 0
c[i][j] = 0
else if(A[i] == B[j] )
c[i][j] = c[i-1][j-1] + 1;
else
if(c[i][j-1]>c[i-1][j])
c[i][j] = c[i][j-1];
else
c[i][j] = c[i-1][j];
return c
Application
In Bioinformatics, the comparison of two DNA strands and similarity in these strands is given by this algorithm by computing longest common subsequence .
Example
- A = A T G C G T C G A T
- B = A T G T G A C T A G
Longest Common Subsequence is of 7 characters and it is A T G T G A T