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

Longest Common Subsequence(LCS) intuition

The recursive version of LCS code looks something like this(m, n are lengths of the strings X and Y respectively) int lcs( char[] X, char[] Y, int m, int n ) { if (m == 0 || n == 0) return 0; if (X[m-1] == Y[n-1]) …
knightcool
  • 324
  • 1
  • 3
  • 11
1
vote
0 answers

Multiple Longest Common Sequency -- Memory optimization code sources

I have implemented an mlcs solution using dynamic programming and the results are satisfactory. My problem is that when the number of sequences n increases and those sequences are pretty long, the resulting lcs table is too big and cannot be kept in…
rory
  • 173
  • 1
  • 1
  • 5
1
vote
2 answers

Finding Longest Common Substring with starting indexes

I saw this code implementation here. It basically takes two strings, finds the longest common substring, and returns the length of it. I wanted to modify it slightly to get the starting indexes of the substrings for each words, but just can't figure…
smg9450
  • 86
  • 1
  • 1
  • 6
1
vote
2 answers

How can I find LCS(longest common subsequence) with gap constraint?

I know the general LCS algorithm and can make the table. But how to find the LCS with a gap constraint as shown in the picture above? How can I make a table? An example of LCS with gap constraint:
jiwon
  • 31
  • 4
1
vote
0 answers

How to memoize LCS in Haskell

As part of revising my knowledge of algorithms I decided to implement Longest Common Subsequence problem in Java and then in my new favourite language - Haskell. lcs :: (Eq a) => [a] -> [a] -> [a] lcs [] _ = [] lcs _ [] = [] lcs (x:xs) (y:ys) = …
szymon
  • 61
  • 4
1
vote
1 answer

Why does this LCS (Longest Common Subsequence) Python implementation with memoization performs badly?

I am learning Dynamic Programming and came across the LCS (Longest Common Subsequence) algorithm. I have implemented several versions of it in Python, to see how implementations differ from each other and how they perform. Here is the code: import…
tonix
  • 6,671
  • 13
  • 75
  • 136
1
vote
1 answer

Optimization of Longest common subsequence algorithm written in PL/SQL

I have written the Longest Common Subsequence algorithm in PL/SQL function. I need only the length of the subsequence as output. Now my greatest challenge is the speed of the function when using the function for hundreds of thousands of records.…
1
vote
0 answers

Recursive Longest Common Substring (LCS) problem optmization

I have the following code in Java: public static int Secret(String a, String b, int x, int y){ if ((x == -1) || (y == -1)) { return 0; } if (a.charAt(x) == b.charAt(y)) { return Secret(a, b, x-1, y-1) + 1; } else { …
Higor
  • 11
  • 2
1
vote
1 answer

Initialize two 2D arrays and fill the first column and first row of one of the arrays with 0 along with Dynamic memory allocation

This function should initialize arrays C[m+1][n+1] and B[m][n] and fill the first row and first column of C with zeros. Note: int*** C refers to a pointer points to the 2D integer array. Please correct the error. void initLCSTable(int*** C, char***…
1
vote
2 answers

Find sequences of words in sublists in Python

In some NLP task I have a nested list of strings: [['Start', 'двигаться', 'другая', 'сторона', 'света', 'надолго', 'скоро'], ['Start', 'двигаться', 'другая', 'сторона', 'света', 'чтобы', 'посмотреть'], ['Start', 'двигаться', 'новая',…
Alex Nikitin
  • 514
  • 5
  • 12
1
vote
0 answers

Finding lcs between a list items and dataframes var1 grouped by some var2

I have this list mylist = list(structure(c(1L, 2L, 4L, 6L, 7L, 8L, 11L, 13L, 14L, 16L), env = , graph = "8e4abfef-5278-11e8-bbcf-c5dd5cc2b8d4", class = "igraph.vs"), structure(c(9L, 17L), env = , graph =…
SumitArya
  • 111
  • 5
1
vote
1 answer

java - how to avoid global external variable as output in recursive function

I have this code to find "All Longest Common Sequences" and their length. public class LCS { static Set lcsList = new HashSet<>(); public static int[][] twoStringMatrix(String a, String b) { int arr[][] = new int[a.length()…
daniel
  • 151
  • 1
  • 13
1
vote
0 answers

Find longest common subsequences with minimum number of subsequences

Given two strings, I would like to find the longest common subsequences with minimum breaks. For e.g: String 1 = abce String 2 = abbde The longest common subsequences is abe with legth = 3 However I would like to get case (1) instead of (2) because…
Elveryx
  • 151
  • 1
  • 8
1
vote
1 answer

Find longest common substring of array of Strings

In my Swift 3.0 app, I want to determine the best name for something by finding the longest common substring of 6 to 12 strings. Example strings: ON/OFF office lights DIM office lights VALUE office lights FB office lights FB VALUE office…
Bram Roelandts
  • 470
  • 7
  • 25
1
vote
3 answers

Suffix Tree: Longest repeating substring implementation

I have implemented a suffix tree, which is not compressed. I wanted to know how to solve the problem of finding the longest repreating substring in a string. I know that we have to find the deepest internal node with two children, but how can be…
TimeToCodeTheRoad
  • 7,032
  • 16
  • 57
  • 70