Questions tagged [lcs]

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

LCS

Longest Common Subsequence is of 7 characters and it is A T G T G A T

204 questions
3
votes
0 answers

Recursive longest common subsequence, incorrect printing

First I'd like to excuse for probably pathetic question. I am writing recursive longest common subsequence program. It usually works fine, but if there are two (or more) equally long results it gets wild. (e.g. jdbac,abjac should give: "bac, jac"…
GlonPL
  • 41
  • 6
3
votes
3 answers

Analyzing time complexity of a function written in C

I was implementing Longest Common Subsequence problem in C. I wish to compare the time taken for execution of recursive version of the solution and dynamic programming version. How can I find the time taken for running the LCS function in both…
razor35
  • 333
  • 3
  • 4
  • 11
3
votes
3 answers

Maximum Recursion in LCS Recursive Function

I'm trying to execute an LCS function that utilizes recursion to give me the number of positions the LCS is valid, along with the place of LCS depicted here: input: LCS("smile", "tile") output: [3, "##ile", "#ile"] Whenever I try and execute it, it…
priya
  • 129
  • 1
  • 1
  • 8
3
votes
1 answer

Longest common subsequence for 3+ sequences in c

I have already written the part of LCS.I want to know If I give N(N>3) ,that means how many set of input. Like this : Input: 4 ab abc abcd abcde Output: 3Just find the longest of those lcs(3 sequences a part)ab abc abcd->ab->2abc abcd…
NaiveRed
  • 65
  • 1
  • 9
3
votes
4 answers

Longest common subsequence (LCS) brute force algorithm

I want to create a brute force algorithm to find the largest common subsequence between 2 strings, but I'm struggling to enumerate all possibilities in the form of a algorithm. I don't want a dynamic programming answer as oddly enough I manage to…
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
3
votes
2 answers

How can I find a faster algorithm for this special case of Longest Common Sub-sequence (LCS)?

I know the LCS problem need time ~ O(mn) where m and n are length of two sequence X and Y respectively. But my problem is a little bit easier so I expect a faster algorithm than ~O(mn). Here is my problem: Input: a positive integer Q, two sequence…
3
votes
1 answer

Implementing Parallel Algorithm for Longest Common Subsequence

I am trying to implement the Parallel Algorithm for Longest Common Subsequence Problem described in http://www.iaeng.org/publication/WCE2010/WCE2010_pp499-504.pdf But i am having a problem with the variable C in Equation 6 on page 4 The paper…
Androme
  • 2,399
  • 4
  • 43
  • 82
3
votes
1 answer

Runtime computation of my implementation of LCS

Please help me in trying to find the runtime of my implementation of largest common substring problem int main(){ string a; string b; cin>>a>>b; string::iterator a1,b1; string max,temp; for(a1=a.begin();a1!=a.end();a1++){ …
Soumadeep Saha
  • 337
  • 2
  • 14
3
votes
1 answer

How to solve LCS(Longest Common Subsequence) with a gap condition

I know the general LCS problem and algorithm. It's like this: LCS(Xi, Yj) = [0 (i = 0 or j = 0) or LCS(Xi-1, Yj-1) + 1 (xi = yj) or max(LCS(Xi, Yj-1), LCS(Xi-1, Yj)) (xi != yj)] But what if we add a gap condition? For…
Ashe
  • 211
  • 2
  • 11
3
votes
1 answer

Getting the longest common subsequence in ERLANG

I'm new to this ERLANG, I know the basics. It's like scheme but broader. I know how to create a function but I'm having problems with creating a function that gets the Longest Common Subsequence. lcs(str1,str2) is a function that will accept two…
ThisGuy
  • 833
  • 1
  • 9
  • 19
3
votes
1 answer

How to speed up Python string matching code

I have this code which computes the Longest Common Subsequence between random strings to see how accurately one can reconstruct an unknown region of the input. To get good statistics I need to iterate it many times but my current python…
user2171391
3
votes
3 answers

All possible LCS(Longest Common Subsequence) of two strings

We can find the LCS(Longest Common Subsequence) of two strings with DP(Dynamic Programming). By keeping track with DP Table we can get the LCS. But if there exists more than one LCS how can we get all of them? Example: string1 : bcab string2 :…
Mostafiz Rahman
  • 8,169
  • 7
  • 57
  • 74
3
votes
2 answers

Why does this DP solution for longest common subsequence work correctly?

Concerning the longest common subsequence problem, the basic algorithm as presented in all online resources is clear to me. This algorithm is described here: What I am not clear is the algorithm presented for the dynamic programming version of…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
2
votes
3 answers

LCS ALGORITHM ( example )

There's a dynamic programming algorithm to find the Longest Common Subsequence of two sequences. How can I find the LCS algorithm of two sequences X and Y. (Test of correctness) (a) X = ABEDFEESTYH Y=ABEDFEESTYHABCDF (b) X = BFAAAABBBBBJPRSTY…
John Tim
  • 21
  • 1
  • 4
2
votes
1 answer

Reverse LCS (longest common subsequence) algorithm: Minimal sequence containing given set of subsequences / shortest common supersequence

I'm trying to figure out how to compute the shortest sequence containing a given set of subsequences. For example, given: abcd bcdgh cdef The answer should be abcdefgh I was thinking about first computing the longest common subsequences of all…
Martin
  • 21
  • 1
1 2
3
13 14