Question
Can I please get help with this? will upvote upon completion.
Problem 2. The longest common substring problem is to find the longest string that is a substring of two strings. The longest
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dynamic Programming Approach

Let the first string be s1 and second be s2.Suppose we are at DP state when the length of s1 is i and length of s2 is j, the result of which is stored in dp[i][j]. So, it means:

dp[i][j] = length of the longest substring in first i characters of S1 and first j characters of S2

Now if s1[i-1] == s2[j-1], then dp[i][j] = 1 + dp[i-1][j-1], that is result of current row in matrix dp[][] depends on values from previous row. Hence the required length of longest common substring can be obtained by maintaining values of two consecutive rows only, thereby reducing space requirements to O(2 * n).

dp[i][j] = 1 + dp[i-1][j-1]   if s1[i-1] == s2[j-1]

To print the longest common substring, we use variable end. When dp[i][j] is calculated, it is compared with res where res is the maximum length of the common substring.

If res is less than dp[i][j], then end is updated to i-1 to show that longest common substring ends at index i-1 in s1 and res is updated to dp[i][j]. The longest common substring then is from index end – res + 1 to index end in s1.

For instance,if we consider string doll as str1 and another string dog as str2 and loop through all characters of str1,position denoted by i, and str2,position denoted by j, to build the table dp[][].We consider the variable curr to state the current row which toggles its value to 0 and 1 at the end of every outer iteration.When i=0 we all the values of dp[][] corresponding to the first row is set 0.When i=1 and str1[i-1]!=str2[j-1], dp[curr][j] = 0.When str1[i-1] = str2[j-1] = 'd' (at j=1) , dp[cur][j] = dp[1][1] = dp[1-curr][j-1] + 1 = 0+1 =1. We store the maximum length of longest common substring (maximum value of dp[cur][j]) as res and the ending index of the substring as end. Here res=1 and end=0.

When i=2 and j=2, curr=0, str1[i-1] = 'o' and str2[j-1] = 'o'.Since str1[i-1] = str2[j-1] therefore dp[curr][j] = dp[1-curr][j-1] + 1 = dp[1][1] + 1 = 1 + 1 = 2.
Since res = 1 which is less than dp[curr][j], res = dp[curr][j] = 2 and end = i-1 = 1.

All other positions of dp is less than res.So finally the length of the largest common substring between doll and dog is 2.

To retrieve the substring iterate from position end-res+1 i.e. 1-2+1 = 0 till end i.e. 1 in str1 doll to get do as the largest common substring between dog and doll.

Time Complexity

  • Worst case time complexity: O(m*n)
  • Average case time complexity: O(m*n)
  • Best case time complexity: O(m*n)
  • Space complexity: O(2*n)

Since we are using two for loops for both the strings ,therefore the time complexity of finding the longest common substring using dynamic programming approach is O(n * m) where n and m are the lengths of the strings.Since this implemetation involves only two rows and n columns for building dp[][],therefore, the space complexity would be O(2 * n).

Pseudocode of Dynamic Programming approach:

Following is the pseudo code for dynamic programming implementation:-

longest_substring(X, Y, m, n)
  
   dp = [[0 for k in range(n+1)] for l in range(m+1)] 
      
    # To store the length of longest common substring 
    result = 0 
  
    # Following steps to build 
    # dp[m+1][n+1] in bottom up fashion 
    for i in range(m + 1): 
        for j in range(n + 1): 
            if (i == 0 or j == 0): 
                dp[i][j] = 0
            elif (X[i-1] == Y[j-1]): 
                dp[i][j] = dp[i-1][j-1] + 1
                if (result < dp[i][j]) 
                { 
                    result = dp[i][j]; 
                    r = i; 
                    c = j; 
                } 
            else: 
                dp[i][j] = 0
    while (dp[r][c] != 0)
    { 
        ans[--result] = X[r - 1]; 
        r--; 
        c--; 
    } 
    return ans 




Add a comment
Know the answer?
Add Answer to:
Can I please get help with this? will upvote upon completion. Problem 2. The longest common...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a pseudocode description of the printLCS () algorithm, which prints the longest common subs...

    Write a pseudocode description of the printLCS () algorithm, which prints the longest common subsequence of two strings x and y. Your algorithm should take as input the completed ïïcs Π integer array of longest common subsequence lengths, and the two strings x and y. (So, you do not have the path[] [] array - see Lecture 19, slides 100 and 101.) Your algorithm must return the specific string corresponding the length found in 1lcs [n] [m] and it should...

  • 2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is...

    2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B) LCS(B, C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is, supposing that the longest common pair wise...

  • 2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence th...

    2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B). LCS(B,C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is, supposing that the longest common pair wise subsequence...

  • Problem 1. Write a program in Java to find the Longest Common Subsequence (LCS) using Dynamic...

    Problem 1. Write a program in Java to find the Longest Common Subsequence (LCS) using Dynamic Programming. Your program will read two strings from keyboard and display the LCS on the screen. Assume upper and lower case letters as same. Sample Input (taken from keyboard): saginaw gain Sample output (display on the screen): ain

  • Programming language: Java m: substring length n: input strings d: Hamming distance (mismatch) I: Number of...

    Programming language: Java m: substring length n: input strings d: Hamming distance (mismatch) I: Number of letters in Sample input string (s1, s2, s3) Strings consists of: A, C, G, and T Example outputs: Generate all possible possibilities of length m(4) using the values A, C, G, and T. EX possibilites: {A,A,A,A A,A,A,C.... G,G,G,G} and find all the possible combinations that have the same sequence with a hamming distance of 1 (only 1 difference) Given n input strings of length...

  • Problem 3: longest common subsequence Recall that in the longest common subsequence problem: the input is...

    Problem 3: longest common subsequence Recall that in the longest common subsequence problem: the input is two sequences seq1 = [x0, x1, ..., xn] and seq2 = [y0, y1, ..., yn]; the output is the length of the longest common subsequence. (a) Write an efficient algorithm in code or pseudocode to solve this problem using the table-filling method. (b) What is the time complexity of this algorithm?

  • Need help with all 3 parts. Thanks Question 1 (Longest Common Subsequence) In the longest common...

    Need help with all 3 parts. Thanks Question 1 (Longest Common Subsequence) In the longest common sub- sequence algorithm we discussed in class, we formulated the recursive formula based on prefixes of the two inputs, i.e., X[1...) and Y [1..,]. 1. Rewrite the recursive formula using suffixes instead of prefixes, i.e., X[...m] and Y[j..n]. 2. Develop a bottom-up dynamic programming algorithm based on the recur- sive formula in (a). Describe the algorithm and write a pseudo code. 3. Use the...

  • Given two strings X and Y, a third string Z is a common superstring of X...

    Given two strings X and Y, a third string Z is a common superstring of X and Y if X and Y are both subsequences of 2. (Example: if X = sos and Y = soft, then Z = sosft is a common superstring of X and Y.) Design a dynamic programming algorithm which, given as input two strings X and Y, returns the length of the shortest common superstring (SCS) of X and Y. Specifically, you have to write...

  • need the answer to b not a. thanks! 2. (40 pts) Let A, B, and C be three strings each n characters long. We want t...

    need the answer to b not a. thanks! 2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B). LCS(B,C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is,...

  • Can I please get help with this question? Will upvote. thanks. Problem 4. Here's a problem...

    Can I please get help with this question? Will upvote. thanks. Problem 4. Here's a problem that occurs in automatic program analysis. For a set of variables x1, ..., Xn, you are given some equality constraints, of the form "x, = x," and some disequality constraints, of the form “x, #x": Is it possible to satisfy all of them? For instance, the constraints x, = XX, = x3,x; = x, *, * x. cannot be satisfied. Give a polynomial time...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT