Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Longest Common Subsequence

Tags:

c#

algorithm

I have written the below code for LCS. It works for many cases but breaks for the one below. I do not understand where my code is breaking. Please help. The code is in C#

namespace LongestCommonSubsequenceBF
{
class Program
{
    static void Main(string[] args)
    {

        string B = "AAACCGTGAGTTATTCGTTCTAGAA";
        string A = "CACCCCTAAGGTACCTTTGGTTC";
        //find LCS in A,B starting from index 0 of each
        int longestCommonSubsequence = LCS(A, B, 0, 0);
        Console.WriteLine(longestCommonSubsequence);
        Console.Read();

    }

    //Find the longest common subsequnce starting from index1 in A and index2 in B
    //Pass A as shorter string
    public static int LCS(String A, String B, int index1, int index2)
    {
        int max = 0;
        if (index1 == A.Length)
        {
            //You have reached beyond A and thus no subsequence
            return 0;
        }
        if (index2 == B.Length)
        {   //you may reach end of 2nd string. LCS from that end is 0
            return 0;
        }

        for (int i = index1; i < A.Length ; i++)
        {
            int exist = B.IndexOf(A[i],index2);
            if (exist != -1)
            {
             //   found = true;

                int temp = 1 + LCS(A, B, i + 1, exist + 1);
                if (max < temp)
                {
                    max = temp;
                }


            }


        }
        return max;

    }
  }
}
like image 445
Programmer Avatar asked Jan 04 '11 18:01

Programmer


People also ask

What is longest common subsequence explain with example?

LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them. A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. For example, “abc”, “abg”, “bdf”, “aeg”, '”acefg”, .. etc are subsequences of “abcdefg”.

What is the use of longest common subsequence?

The longest common subsequence problem is a classic computer science problem, the basis of data comparison programs such as the diff utility, and has applications in computational linguistics and bioinformatics.

What is the time complexity of LCS?

The general algorithms which are followed to solve the Longest Common Subsequence (LCS) problems have both time complexity and space complexity of O(m * n).

Which algorithm is used for longest common subsequence?

Dynamic Programming This algorithm will print the longest common subsequence of X and Y.


1 Answers

Why do you think your algorithm is broken? The longest common subsequence is ACCTAGTATTGTTC, which is 14 characters long:

string B = "AAACCGTGAGTTATTCGTTCTAGAA";
              ^^^ ^ ^^ ^^^^ ^^^^

string A = "CACCCCTAAGGTACCTTTGGTTC";
             ^^^  ^ ^^ ^^  ^^ ^ ^^^

(I modified your algorithm to return the sequence instead of just the length.)

like image 200
Heinzi Avatar answered Sep 29 '22 12:09

Heinzi