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

Longest Common Subsequence in VBA Giving #VALUE! Error

I've been putting together a UDF in Excel (365) to calculate the longest common subsequence between two strings (based on this implementation in python https://www.geeksforgeeks.org/printing-longest-common-subsequence/). When I run the UDF I get a…
2
votes
3 answers

Performance issue finding LCS with Haskell

This is a classic programming problem https://en.wikipedia.org/wiki/Longest_common_subsequence_problem The JS implementation passes all the tests but the Haskell one consumes too much memory and gets killed. What am I doing wrong? -- TOP TO…
2
votes
1 answer

Optimal Substructure Property

suppose we have two Strings X,Y of length n,m respectively. I found the longest common subsequence Z of X and Y. how can I prove the optimal substructure property of the longest common subsequence (LCS) ?
2
votes
1 answer

longest palindromic substring using lcs?

I was solving this longest palindromic substring problem on leetcode and I followed the dynamic programming approach by creating one n*n boolean table(which I guess is also the standard solution to this) and successfully solved it but I was just…
2
votes
1 answer

Remove longest common prefix from a list of paths

I have a list of paths []string{"/a/path/to/something", "/a/path/in/something", "/a/path/in/something/else"} I'd like to remove the Longest Common prefix from all paths so that the remaining part of it is the distinct part of the path. For the…
wasp256
  • 5,943
  • 12
  • 72
  • 119
2
votes
1 answer

Node structure in longest increasing subsequence algorithm (Jacobson & Vo)

I have a problem in understanding the node structure for the calculation of the complete longest increasing subsequence (lis) in the paper "Heaviest Increasing/Common Subsequence Problems" by Jacobson and Vo. Here is the pseudo code from the…
2
votes
1 answer

Can the memoization table used to find the longest common subsequence of two strings, also be used to find the indices of differences?

I'm writing a GitHub browser extension that displays some diffs in an alternate way. GitHub simply shows pre-computed diffs, so I need to re-diff things myself. The UNIX diff util is based on finding a Longest Common Subsequence. I found an…
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
2
votes
2 answers

LCS algorithm: How to find out from a Table, how many longest common subsequences are found?

I implement the Longest Common Subsequence problem in C#. I need to detect ALL the common maximal subsequences between two strings. To do this, I create a table using Needleman-Wunsch algorithm to store the LCS sequence for each step of the…
2
votes
2 answers

DNA subsequence dynamic programming question

I'm trying to solve DNA problem which is more of improved(?) version of LCS problem. In the problem, there is string which is string and semi-substring which allows part of string to have one or no letter skipped. For example, for string "desktop",…
Choi Yeong
  • 23
  • 3
2
votes
1 answer

Brute Force Approach for LCS and its Time Complexity [O(m*n)!?]

I have read several Algorithm books where it is been told brute force approach of Longest Common Subsequence takes 2^n which is exponential in time complexity. Whereas, I've noticed that while I am applying my brute force technique it's taking O(mn)…
2
votes
0 answers

Find common sequences of words in sublists in Python

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

Using diff in an array of objects that conform to a protocol

I'm experimenting with using Composition instead of Inheritance and I wanted to use diff on an array of objects that comply with a given protocol. To do so, I implemented a protocol and made it comply with Equatable: // Playground - noun: a place…
João Pereira
  • 3,545
  • 7
  • 44
  • 53
2
votes
0 answers

Longest Common Subsequence, required length on contiguous parts

I think I have enough grasp of the LCS algorithm from this page. Specifically this psedo-code implementation: (m and n are the lengths of A and B) int lcs_length(char * A, char * B) { allocate storage for array L; for (i = m; i >= 0; i--) …
2
votes
1 answer

Given a string X and the reverse of that string,Y. Is the longest common sequence of X and Y always a palindrome?

Our professor gave us the following problem: Input A sequence of characters X = x1, x2, ... ,xn Output The length of the longest sub-sequence of X that is a palindrome My solution was: Take X and reverse it into the sequence Y = y1, y2, ...…
Marco Lugo
  • 169
  • 2
  • 10
2
votes
1 answer

Longest sub-sequence in a matrix

I have a matrix like this-> 1 6 2 8 3 7 4 9 5 You can go any direction, up down left right diagonally and you have to find the longest sub sequence, you can select a next number in the sequence in such a way that its absolute difference is greater…