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
1
vote
2 answers

^@ symbol appearing after piping output (C++)

So I've implemented the Longest Common Subsequence algorithm for the Introduction to Algorithms book (CLRS) in C++, and it works fine, kinda. When I do something like this: ./lcs abc bc > OUTPUT When I open the OUTPUT file in vim, I see this: 2…
adelbertc
  • 7,270
  • 11
  • 47
  • 70
0
votes
5 answers

Efficiently searching for custom objects in large Python lists

I have a list of custom Python objects and need to search for the existence of specific objects within that list. My concern is the performance implications of searching large lists, especially frequently. Here's a simplified example using a custom…
SyntaxNavigator
  • 312
  • 3
  • 10
0
votes
1 answer

Where am I going wrong in the basic implementation of Longest Common Subsequence

I've tried to implement LCS in python using the following code, where am I going wrong ? def lcs(s1,s2): if len(s1) == 0 or len(s2) == 0: return "" n = len(s1) - 1 m = len(s2) - 1 if s1[n] == s2[m]: …
0
votes
0 answers

Longest Common Subsequence using Lose it or Use it

I need to return the string of the Longest Common Subsequence of 2 strings. But the code keeps crashing, probably due to Infinite recursion? But I don't know where it happens? Am I missing a base case? def lcs(s, t): """ Arguments are 2 strings s…
MrPuffer
  • 25
  • 4
0
votes
0 answers

Finding minimal number of del/insert/change to transform string X(len=m) to string Y(len=n)

I’ve read and pretty much understood the algorithm to solve this problem using only insert/delete actions, which completes the task in O(n * m) I was thinking; if we were also able to change(replace) a character in the process, how would that…
Aishgadol
  • 147
  • 6
0
votes
1 answer

LCS C# Algorithm - Get the deleted lines and added lines in previous and current text

I have to rewrite the LCS algorithm because some company policies. I've already get done the LCS algorithm, but next step is to identify which lines were removed from the previous text and which one were added in the current text. I tried a simple…
Daniel Melo
  • 548
  • 5
  • 12
0
votes
0 answers

LCS using Dynamic programming

I am learning how to solve the longest common subsequence using dynamic programming. I understand how the table works, however, I don't get the reason why the formula for x[i]!=y[j] is like this max{c[i − 1, j], c[i, j − 1]}. Why not other formulas?
0
votes
0 answers

longest common subsequence in c#

I want to allow users to input their own characters to find the lowest common subsequence, this code is mostly a skeleton of somebody else's work- and the characters used are AGGTAB and GXTXYAB which is GTAB however I want users to implement their…
0
votes
1 answer

Longest common subsequence of more than 1 Letter in a string

I'm trying to find the LCS of more than one letter in two strings using a k value. For example, if k = 3 s1 = "AAABBBCCCDDDEEE" s2 = "AAACCCBBBDDDEEE" then the LCS would be 4: "AAABBBDDDEEE" or "AAACCCDDDEEE", another example is if k = 2 s1…
trab
  • 33
  • 5
0
votes
1 answer

length longest common subsequence without dynamic programming

I am doing an exercise to find the longest common subsequence (LSC) without dynamic programming, so far I have the code that returns the longest common subsequence but I also need to return the length of the sequence, what do I have to do? this is…
sinNombre
  • 49
  • 4
0
votes
0 answers

Recursive solution to longest common substring not working

I am trying to get the longest common substring recursively but for some reason my code is not working. def lcs(s, t): if not s or not t: return '' if s[0] is t[0]: return s[0] + lcs(s[1:], t[1:]) a = lcs(s[1:], t) …
Guilherme
  • 1
  • 1
0
votes
0 answers

Better runtime? - C - LCS - Longest Common SubSequence of two sequences

I have written a program to find LCS (Longest Common Subsequences) using Dynamic Programming with DP array. Coursera said it is not fast enough. Is there some sort of ways to improve it? I have tried my best to simplify it as much as possible…
keitou777
  • 21
  • 2
0
votes
0 answers

How can I vectorize longest common substring across data.table columns in R

How can I create a function that will allow me to EITHER quickly calculate the # of characters in the longest common substring OR return the longest common substring between TWO OR MORE COLUMNS in a large data.table in R? I modified this question's…
Neal Barsch
  • 2,810
  • 2
  • 13
  • 39
0
votes
1 answer

Longest Common Sequence with Sequences Slightly Out of order Locally

I need to solve the longest common sequence problem between two sequences when they are slightly out of order and the nodes that are out of order are close to each other. In other words, let's say one of the sequences are s1 s2 s3 ... s_n. It may…
Tom Bennett
  • 2,305
  • 5
  • 24
  • 32
0
votes
0 answers

How to generate two random Strings from a given LCS?

A string is given, which should be the Longest Common Subsequence(LCS) between two Strings. You have to generate two random strings(with alphabets only) having the string as their LCS ? Please help me out to solve this by providing any reference or…
iamskab
  • 1
  • 2