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
2
votes
3 answers

Finding largest sequence of bytes in two byte arrays

Example: { 54, 87, 23, 87, 45, 67, 7, 85, 65, 65, 3, 4, 55, 76, 65, 64, 5, 6, 4, 54, 45, 6, 4 }; { 76, 57, 65, 3, 4, 55, 76, 65, 64, 5, 6, 4, 54, 45, 8, 65, 66, 57, 6, 7, 7, 56, 6, 7, 44, 57, 8, 76, 54, 67 }; Basically, I have two byte[], and need…
Whosdatdev
  • 333
  • 3
  • 10
2
votes
0 answers

comput edit distance between 2 very large strings

SCENARIO: Given 2 input strings I need to find minimum number of insertions deletions and substitutions required to convert one string to other. The strings are text from 2 files. The comparison has to be done at word level. What i have done is…
uzair_syed
  • 313
  • 3
  • 16
2
votes
1 answer

Identify a common pattern

Is there a (easy) possibility to identify a common pattern which two strings share? Here is a little example to make clear what I mean: I have two variables containing a string. Both include the same pattern ("ABC") and also some "noise". a <-…
Alex
  • 4,925
  • 2
  • 32
  • 48
2
votes
0 answers

K-candidates on Hunt & McIlroy algorithm

I'm trying to understand the algorithm of Hunt & McIlroy but I don't understand how to find the k-candidates. I know that the k-candidates are pairs of indices such that: A_i = B_j P_(ij) > max(P_(i-1, j), P_(i, j-1)) The second point implies two…
user5346990
2
votes
2 answers

Diff, Patch and Reverse-patch in Java

I am looking for a java util which can create a diff between two java objects, which can be nested and contain arrays etc. The util should also have the capability to apply the diff (a.k.a patch) on the original object and also remove the diff from…
2
votes
1 answer

Longest common subsequence implementation-python

I have implemented the longest common subsequence problem as instructed in this video. It just execute first set of code and produces an empty list. What is wrong with this implementation? def lcs_recursive(xlist,ylist): if not xlist or ylist: …
Nilani Algiriyage
  • 32,876
  • 32
  • 87
  • 121
2
votes
1 answer

Longest Common Substring using Suffix Automata

I used to calculate longest common Substring using dynamic programming O(m * n), suffix tree O(m + n), suffix array O(nlog^2 n) according to my need. Recently I have learnt Suffix Automaton which performs in O(n) which is very impressive. I can…
Kaidul
  • 15,409
  • 15
  • 81
  • 150
2
votes
3 answers

Approach to Longest Common Substring

In the Programming Challenge in the Common Child , i have taken a different approach than the general Longest Common Substring problem. The Code is #include #include #include #include #include #include…
Shaurya Chaudhuri
  • 3,772
  • 6
  • 28
  • 58
2
votes
1 answer

Longest common substring in string itself

Given a string like "geekthegeertheregeers" .So we have to find longest common substring in the string itself. Like in this case "geer" will be longest common substring. My question is that which algorithm will be applied here.can LCS be modified…
Ankur Singh
  • 25
  • 1
  • 4
2
votes
2 answers

Longest Common Subsequence

Hi this is my code for longest common subsequence for 2 strings in c# . I need help in backtracking . I need to find out the subsequence : GTCGT String str1 = "GTCGTTCG"; String str2 = "ACCGGTCGAGTG"; int[,] l = new int[str1.Length, str2.Length];…
2
votes
3 answers

How to apply longest common subsequence algorithm on large strings?

How to apply longest common subsequence on bigger strings (600000 characters). Is there any way to do it in DP? I have done this for shorter strings. #include #include #include #include using namespace…
user2739256
2
votes
4 answers

longest common subsequence: why is this wrong?

int lcs(char * A, char * B) { int m = strlen(A); int n = strlen(B); int *X = malloc(m * sizeof(int)); int *Y = malloc(n * sizeof(int)); int i; int j; for (i = m; i >= 0; i--) { for (j = n; j >= 0; j--) { if…
ithisa
  • 752
  • 1
  • 8
  • 25
2
votes
2 answers

longest common subsequence printdDiff

Just a quick question about the longest Common subsequence algorithm. I have done the part where you need to generate the subsequence as follow: public int[][] lcsLength(char[] input1, char[] input2) { int[][] opt = new int[M][N]; for (int i…
2
votes
1 answer

ocaml lcs without objects like Array

How can I find the longest common subsequence without using Arrays and only List folds in ocaml
2
votes
2 answers

Is this an acceptable algorithm?

I've designed an algorithm to find the longest common subsequence. these are steps: Pick the first letter in the first string. Look for it in the second string and if its found, Add that letter to common_subsequence and store its position in…
Sajad Rastegar
  • 3,014
  • 3
  • 25
  • 36