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
1 answer

Find sublists with common starting elements - python

I have a nested list: lists =[['a','b','c'], ['a','b','d'], ['a','b','e'], ['с','с','с','с']] I need to find sublists with 2 or more common (2 or more occurrences) first elements, make a single string from this elements, and…
Alex Nikitin
  • 514
  • 5
  • 12
0
votes
1 answer

Finding LCS using DP

I have used Dynamic Programming to find longest common sub-sequence b/w two strings. What is wrong in the code. Why it always gives answer as 0? #include using namespace std; int dp[20][20]; void initialize() { for(int…
0
votes
0 answers

Algorithm- Printing LCS in O(nlogn) complexity

I'm trying to print the longest common subsequence of 2 long long vectors in O(nlogn) worst case time, and I have the following three conditions- 1. Each element in a vector is distinct 2. Each vector is a permutation of the other 3. If the size of…
0
votes
1 answer

LCS (Longest Common Subsequence) - get best K solutions

The LCS problem gets two strings and returns their longest common subsequence. For example: LCS on the strings: elephant and eat is 3, as the whole string eat is a subsequence in elephant - indices 0,6,7 or 2,6,7 Another example: LCS on the strings:…
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
0
votes
1 answer

Longest Common Sub-sequence of N sequences (for diff purposes)

I want to find the longest common sub-sequence of N strings. I got the algorithm that uses Dynamic Programming for 2 strings, but if I extend it to N, it will consume exponential amount of memory, as I need an array of N dimensions. It is not an…
lmcarreiro
  • 5,312
  • 7
  • 36
  • 63
0
votes
1 answer

function that contains a function of itself?

The title is a little weird, but I don't know exactly how this is called, so plz forgive me with the abstract title.... I've found a code like this online: def lcs(xstr, ystr): """ >>> lcs('thisisatest', 'testing123testing') 'tsitest' …
Eric Kim
  • 2,493
  • 6
  • 33
  • 69
0
votes
1 answer

How to use DP to solve "Longest similar subsequence"

I've read the solutions to the LCS problem. But now there's a Longest Similar Subsequence problem: A sequence C is a similar subsequence of two sequences A, B if and only if C is a subsequence of A and we can replace at most K elements in C such…
Einsambr
  • 189
  • 2
  • 11
0
votes
1 answer

Is there a better way to compare strings in a reasonable amount of time?

I have this Ruby function that tells me if two strings are "almost" equal, that is, if all characters in the string are identical and ordered in the same way except for one. So for instance, these are equal equal eual but these are…
user7055375
0
votes
0 answers

How to extract a partial match/approximate match substring from text file

For example: string1 = "Sherlock Holmes is a fictional private detective created by British author Sir Arthur Conan Doyle. Known as a consulting detective in the stories, Holmes is known for his proficiency with observation." string2 = …
VinodJM
  • 1
  • 2
0
votes
2 answers

longest common subsequence in elm with memoization

I would like to make an efficient version of the LCS algorithm in elm. I like this ocaml version but it uses side effects in order to cache the results as it goes. let lcs xs ys = let cache = Hashtbl.create 16 in let rec lcs xs ys = try…
fayong lin
  • 214
  • 2
  • 15
0
votes
0 answers

PythonQuestion on Longest Common Substring(LCS) algorithm

I'm pretty new to Python, it's my first programming language, and I've wanted to work on some manual data structure manipulation and playing around. I've recently been learning the basic algorithm for solving the LCS problem, and I understand how…
LoganK
  • 1
  • 1
0
votes
3 answers

Shortest common SuperSequence

The problem is that given 2 strings X and Y , we need to find length of the shortest sequence Z such that both the strings occur as sub sequence in Z. Now, I get the intuition that length = |X| + |Y| -|LCS(X,Y)|. But how do we prove it ? Ex: X =…
Number945
  • 4,631
  • 8
  • 45
  • 83
0
votes
0 answers

Branch and Bound approach for solving Longest Common Subsequence(LCS).

Is there a way to solve longest common subsequence(LCS) problem using branch and bound technique. I understand branch and bound but I am not able to apply the technique to solve LCS. Just wanted forum opinion to point in right direction to find the…
zeal
  • 465
  • 2
  • 11
  • 22
0
votes
0 answers

LCS approximation with sparse alphabet in O(m+n)

In Longest Common sub-sequences problem, we have two string s1 and s2 with length m and n, we want to find the longest sub-sequence in both s1 and s2 which is not required to occupy consecutive positions within the original sequences. The common…
user137927
  • 347
  • 3
  • 13
0
votes
2 answers

Longest Common Subsequence Python 2 function

When I run LCS( 'human', 'chimp' ), I'm getting "h" instead of "hm". When I run LCS( 'gattaca', 'tacgaacta' ), I'm getting "g" instead of "gaaca". When I run LCS( 'wow', 'whew' ), I'm getting "ww" which is correct. When I run LCS( '', 'whew' ), I'm…