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
0
votes
0 answers

How to normalize LCS algorithm result in a way that strings's length won't affect?

I have n strings (each has its own size) and the letters are included in finite group - S (~ 120 letters). I want to calculate LCS result between each string to another and I want that all results will be normalized. I want to normalize LCS result…
0
votes
1 answer

LCS algorithm running through a file with 10 strings

I have a file with 10 strings - each string in 1 line - and I need to run LCS and get the LCS and LCS length of each comparison, for example, String 1 with String 2, String 1 with String 3, String 1 with String 4 and so on until it goes through each…
0
votes
0 answers

LCS C Program Crashing on first input

Hello I am new to c and I am having some trouble getting my code to run, everything compiles fine but when I enter the first input the exe crashes. I am pretty sure the problem lies in either the memory allocation of my array or my use of pointers…
Cwsager
  • 1
  • 1
0
votes
1 answer

Naive Approach to Longest Common Subsequence

We studied dynamic programming theory over Fall quarter, and I'm trying to brush up and continue further studies into it. I'm currently trying a naive approach into the LCS problem as mentioned in this TopCoder article: Dynamic Programming The…
Parity Bit
  • 113
  • 1
  • 12
0
votes
1 answer

How to prove correctness of the following algo

Problem is to find LIS(Longest Increasing Subsequence) of any given array. Ex. a[]={10,9,7,8,9}; length=3; {7,8,9} So one way of doing in nlogn is Sort the array Take LCS of the the two Resulting is LIS. Now I understood how to do it. But how do…
sandy
  • 509
  • 1
  • 6
  • 23
0
votes
0 answers

While loop, new operator and some others

I'm trying to draw an LCS table. And this part of my code stop after the first iteration of the inner while loop of the fill_the_table() function. I can't find what's wrong in my code. I'm new to C++ by the…
Tom Ân
  • 1
  • 1
0
votes
0 answers

error in finding the longest common sub sequence between two strings

#include #include int m,n,a,c[20][20]; char x[15],y[15],b[20][20]; void print_lcs(int i,int j) { if(i==0 || j==0) return; if(b[i][j]=='C') { print_lcs(i-1,j-1); printf("%c",x[i-1]); } …
gospelslide
  • 179
  • 2
  • 2
  • 12
0
votes
2 answers

Parent string of two given strings

Given 2 strings, we have to find a string smallest in length such that the given strings are subsequences to the string. In other words, we need to find a string such that deleting some characters result in the given strings. was thinking of brute…
rjalfa
  • 729
  • 1
  • 8
  • 14
0
votes
3 answers

Longest Common Sequence -Syntax error

I am attempting to find the LCS of two DNA sequences. I am outputting the matrix form as well as the string that includes the longest common sequence. However, when I return both matrix and list in my code, I obtain the following error: IndexError:…
Roy
  • 1
0
votes
1 answer

Longest Common Sequence -Index Error

I am trying to find the LCS between two sequences: TACGCTGGTACTGGCAT and AGCTGGTCAGAA. I want my answer to output as a matrix so that I can backtrack which sequence is common (GCTGGT). When I use my code below, I am getting the following error.…
Paul
  • 3
  • 2
0
votes
0 answers

python longest repeated substring in a single string?

i can only find hints on how to make an algorithm for a longest repeated substring in two separate strings in comparing the two. but what i need help on is how to find a longest repeated substring in one SINGLE looooong string (which is read from a…
vap
  • 95
  • 1
  • 1
  • 7
0
votes
1 answer

Greedy LCS Java Code Implementation

I'm trying to make java code from this journal: Journal Greedy LCS This code is about a greedy approach for computing longest common subsequences by afroza begum. And here is my [EDITED] code: public class LCS_Greedy { public static void…
john
  • 57
  • 7
0
votes
1 answer

longest common substring between 2 HUGE files - out of memory: java heap space

I'm completely brain fried after this, I need to find the longest common substring between 2 files, a small one and a HUGE one. I don't even know where to start to begin the search, heres what I have so far import java.io.BufferedReader; import…
Steven R
  • 323
  • 2
  • 6
  • 18
0
votes
1 answer

Algorithm - compute the longest common subsequence (LCS) of two DAGs

I have two directed acyclic graph and I need to compute the longest common subsequence (LCS) of these graphs. For two strings/subsequences I use LCS algorithm using dynamic programming (DP) but how can I modify this algorithm to the graphs? Any…
Pepa Zapletal
  • 2,879
  • 3
  • 39
  • 69
0
votes
2 answers

Facing error in LCS function in php

I am coding LCS(longest common subsequence) in php program by using recursive approach. I have the following code:
sana
  • 410
  • 2
  • 6
  • 24