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

Finding longest common subsequence in O(NlogN) time

Is there any way of finding the longest common subsequence of two sequences in O(NlogN) time? I read somewhere that there is a way to achieve this using binary search. I know the dp approach that takes O(N2) time.
LTim
  • 473
  • 1
  • 4
  • 15
6
votes
3 answers

how to implement near matches of strings in java?

Hello fellow programmers, I would like to ask for some help with regards to near matches of strings. Currently, I have a program that stores strings of description, users can search for description by typing it completely or partially. I would…
melyong
  • 83
  • 1
  • 7
6
votes
3 answers

Longest Common Palindromic Subsequence

Is there any efficient algorithm that counts the length of Longest Common Palindromic Subsequence of two given strings? for example: string 1. afbcdfca string 2. bcadfcgyfka the LCPS is 5 and LCPS string is afcfa.
Mostafiz Rahman
  • 8,169
  • 7
  • 57
  • 74
5
votes
4 answers

How to speed up calculation of length of longest common substring?

I have two very large strings and I am trying to find out their Longest Common Substring. One way is using suffix trees (supposed to have a very good complexity, though a complex implementation), and the another is the dynamic programming method…
Lazer
  • 90,700
  • 113
  • 281
  • 364
5
votes
1 answer

What is the general format of Ruby "diff-lcs" diff output?

The Ruby diff-lcs library does a great job of generating the changeset you need to get from one sequence to another but the format of the output is somewhat confusing to me. I would expect a list of changes but instead the output is always a list…
maerics
  • 151,642
  • 46
  • 269
  • 291
4
votes
1 answer

Is there any algorithm to address the longest common subsequence problem with different weights for each character?

I'm looking for an algorithm that addresses the LCS problem for two strings with the following conditions: Each string consists of English characters and each character has a weight. For example: sequence 1 (S1): "ABBCD" with weights [1, 2, 4, 1,…
4
votes
0 answers

Reduction for Longest Common Subsequence (LCS)

I am working on a problem regarding LCS of two strings, and I was wondering if there is any reduction from the general case of LCS to its binary version, i.e., by solving LCS for bit-strings we can also solve LCS with an arbitrary (but finite)…
4
votes
1 answer

Diff command - avoiding monolithic grouping of consecutive differing lines

Playing around with the standard linux diff command, I could not find a way to avoid the following type of grouping in its output (the output listings here assume the unified format) This question aims at the case that each line differs by little…
matanster
  • 15,072
  • 19
  • 88
  • 167
4
votes
4 answers

How to calculate the number of longest common subsequences

I'm trying to calculate the amount of longest possible subsequences that exist between two strings. e.g. String X = "efgefg"; String Y = "efegf"; output: The Number of longest common sequences is: 3 (i.e.: efeg, efef, efgf - this doesn't…
Meir
  • 12,285
  • 19
  • 58
  • 70
4
votes
4 answers

How to print all possible solutions for Longest Common subsequence

I want to print all the possible solutions to LCS problem. The two strings abcbdab and bdcaba should print following 3 strings: bdab,bcba,bcab. C is the global matrix table which takes values according to algorithm and m, n are the length of the…
user2848299
4
votes
4 answers

how can i find lcs length between two large strings

I've written the following code in C# for obtaining the length of longest common subsequence of two texts given by use, but it doesn't work with large strings. Could you please help me. I'm really confused. public Form1() { …
ilia7
  • 49
  • 1
  • 9
4
votes
2 answers

Multiple Sequence Alignment (Longest Common Subsequence)?

OK this is what I want to do: Get more than two strings and "align" them (no DNA/RNA sequence or the like, just regular strings with not like 1000 items in each of them) I've already done some work with pairwise alignment (align two strings) however…
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
3
votes
1 answer

Longest Common Sequence O(mn) of two strings without string concatenation

I was under the impression that the following function has a time complexity of O(mn) with m and n being the length of the two strings. However, someone disagrees because he claims that string concatenation involves the copy of characters and hence…
Matt
  • 7,004
  • 11
  • 71
  • 117
3
votes
1 answer

Is Levenshtein distance related to largest common subsequence?

I don't have proof but i have gut feeling that , suppose s1 is string which needs to be converted to s2 then we can keep the largest common subsequence in s1 as it is and edit distance is number of elements we need to replace/remove/insert. For…
3
votes
2 answers

Diff algorithm C++

I'm trying to create a program in C++ that could be able to diff two .txt files. struct line { string text; size_t num; int status; }; void compareFiles(vector &buffer_1, vector &buffer_2, size_t index_1, size_t index_2) { …
João Santos
  • 175
  • 2
  • 10
1
2
3
13 14