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

Plotting time against size of input for Longest Common Subsequence Problem

I wish to plot the time against size of input, for Longest common subsequence problem in recursive as well as dynamic programming approaches. Until now I've developed programs for evaluating lcs functions in both ways, a simple random string…
razor35
  • 333
  • 3
  • 4
  • 11
1
vote
2 answers

Longest common subsequence prolem

What is longest common subsequence for these 2 strings {xaybadfeg, abcdefg}. Isn't it "abdeg"? I am using this algorithm (the dynamic programming technique) to find the solution. It returns "adeg" as the answer. Is my understanding of subsequence…
sri_84
  • 81
  • 6
1
vote
1 answer

Ionic-AngularJS-Whats do the flags -lcs stand for in ionic run android -lcs?

I wanted to ask what the flags "lcs" in "ionic run android -lcs" mean? (I had an error with the ionic framework that my app was only builded in the right way when using this flags but I don't know what they do stand for.)
FranzHuber23
  • 3,311
  • 5
  • 24
  • 63
1
vote
2 answers

Get suffix / prefix corrections from two strings to transform source string to target string in python

I need to transform a source string to target string and express the same as an operation (D,A,TYPE) ie(deletion , addition, PREFIX\SUFFIX) to the source string which will transform it to target string on applying these operation to the suffix of…
stackit
  • 3,036
  • 9
  • 34
  • 62
1
vote
0 answers

Get reasonable long subsequences in multiple files

I have 3 files, each about 6MB in size, and I need to know what would be the blocks common in those files. With blocks I mean chunks of binary data 128 bytes long. Unfortunately, they might start anywhere. I need to know, where in the files those…
rhavin
  • 1,512
  • 1
  • 12
  • 33
1
vote
1 answer

printing Diff from Longest Common Subsequence

So I've been doing some practice problems for both Perl & Python (kinda choosing between the 2) and I got a problem where I need to make my own diff algorithm just like Github's. I'm up to the point where I know that the Longest Common Subsequence…
Anthony Wijaya
  • 427
  • 1
  • 5
  • 12
1
vote
2 answers

Bound Exception in algorithm of the longest common subsequence

I wrote an algorithm which finds the longest common subsequence of two strings. Here is what "plik1" file includes: 1 11 23 1 18 9 15 23 5 11 1 18 1 20 5 11 1 And here is what should save in file2: 11 1 18 5 After compiling there is an…
user4132350
1
vote
2 answers

How to use supervised machine learning methods working on variant input dimensions?

So basically I am dealing with a training and test data set (a bunch of arrays) with unequal length like these: a: {true, [1,3, 4, 5, 5, 8 ,10 ,10]} b: {true, [1,3, 25, 18 ,1 ,10]} c: {false, [1, 8 ,10]} d: {false, [1,3 ,10 ,10]} I am new to…
lllllllllllll
  • 8,519
  • 9
  • 45
  • 80
1
vote
1 answer

Longest common sub sequence recurrence

The recurrence for lcs is: L[i,j] = max(L[i-1,j], L[i,j-1]) if a[i] != a[j] Can you tell me why it is i-1 or j-1? Why isn't L[i,j] = L[i-1,j-1] correct?
ffff
  • 2,853
  • 1
  • 25
  • 44
1
vote
1 answer

Longest common substring across multiple sequences

I'm trying to find the longest common substring (LCS) across multiple sequences. There are a number of modules on CPAN that implement the LCS algorithm for 2 sequences, such as Algorithm::Diff and String::LCSS_XS, but I'm having a hard time in…
enricoferrero
  • 2,249
  • 1
  • 23
  • 28
1
vote
1 answer

Database design for storing LCS information?

I have a table that contains 2 columns, one is a id, and other is column containing long strings eg. Id strings 1 AGTTAGGACCTTACTCTATATCTGTTCTGTTGGTATGGAG 2 GTACTTGTATTCTGATATCTAGGGTTTTCTAATTACTTCTG 3 …
Razort4x
  • 3,296
  • 10
  • 50
  • 88
1
vote
1 answer

Longest Common Subseqence

I'm trying to write a recursive algorithm that finds the longest common subsequence of two lists, as described in http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#LCS_function_defined It appears that the recursion never ends and I…
Ares
  • 1,411
  • 1
  • 19
  • 35
1
vote
3 answers

longest common subsequence print all subsequences

how to display all the substrings of a longest common substring of two substring i know the dp method to calculate length of lcs but how to display all those lcs code for lcs function LCSLength(X[1..m], Y[1..n]) C = array(0..m, 0..n) for i :=…
ayush nigam
  • 177
  • 1
  • 4
  • 15
1
vote
0 answers

Merging two files in PHP

I'm working on a part of a webapp where users will be able to change the same file together and see the changes each other do (just like Google Drive currently do). But I'm stuck on the "see the changes each other do" and merging files parts. I've…
Exanis
  • 41
  • 1
  • 8
1
vote
1 answer

Python string LCS bug

I am soo close to finishing my program, but I am having a small issue. The I/O is suppose to look like this: I: fanlc2("human", "chimpanzee") O: [4, 'h#man', '#h#m#an###'] BUT mine, does the following: I: fanlc2("human", "chimpanzee") O: [4, '#',…
user1681664
  • 1,771
  • 8
  • 28
  • 53