Question

In: Computer Science

Given two strings X and Y of length n and m respectively, design a dynamic programming...

Given two strings X and Y of length n and m respectively, design a dynamic
programming based algorithm to finnd a super-string Z of minimum length such that both
X and Y are subsequence of Z. Note that characters in X and Y do not need to appear
consecutively in Z as long as they appear in the same order in Z as in X or Y . Design an other
algorithm for solving the same problem but with three input strings, W;X; Y , i.e., finding the
minimum length super-string for three strings. Can your algorithm be extended to k input
strings? If so, what would be the running time and space complexities of your algorithm.

Solutions

Expert Solution

At first we will try to get a solution for 2 strings, we will create a function that will return string which is shortest common supersequence of input strings.

the following code is in python.

def shortestSuperSeq(x, y) :
    m = len(x)
    n = len(y)

    # dp[i][j] contains length of shortest

    dp = [[0 for i in range(n + 1)] for j in range(m + 1)]

    # Initialising 1st row and 1st column
    for i in range(m + 1):
        dp[0][i] = i
    for i in range(n + 1):
        dp[i][0] = i
    # filling the table same as LCS program
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if x[i - 1] == y[j - 1]:
                dp[i][j] = 1 + dp[i - 1][j - 1]
            else :
                dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1])

    # Extracting superSeq

    # dp[m][n] stores the length of the
    # shortest supersequence of X and Y
    index = dp[m][n]

    # string to store the shortest supersequence using backtracking
    string = ""

    # Start from the bottom right corner and one by one pushing characters in output string,
    i = m
    j = n
    while i > 0 and j > 0:

        # If current character in X and Y are same, then current character is part of shortest superseq
        if x[i - 1] == y[j - 1]:
            # add current character in result
            string += x[i - 1]
            # reducing values of i, j and index
            i -= 1
            j -= 1
            index -= 1

        # If current character in X and Y are different
        elif dp[i - 1][j] > dp[i][j - 1]:

            # add current character of Y in result
            string += y[j - 1]

            # reduce values of j and index
            j -= 1
            index -= 1
        else :

            # add current character of X in result
            string += x[i - 1]

            # reduce values of i and index
            i -= 1
            index -= 1
    # If X reaches its end, put remaining characters
    # of Y in the result string
    while j > 0:
        string += y[j - 1]
        j -= 1
        index -= 1

    # If Y reaches its end, put remaining characters
    # of X in the result string
    while i > 0 :
        string += x[i - 1]
        i -= 1
        index -= 1

    string = list(string)
    # reverse the string and return it
    string.reverse()
    return ''.join(string)

Now if you want to generalise this for 3 or K number of strings. say S1, S2, S3 be input strings

Then at first call function shortestSpuerSeq for the first two strings S1 and S2, store the result in a variable say ans1

after that call the function for S1 and S3, store the result in a vriable say ans2.

Finally call the function for ans1 and ans2. that will be required ans.

In similar fashion generliase it for K number of strings keep removing common subsequnces taking 1 string as base.

Assuming k to be 4 and say input strings be s1, s2, s3, s4.

then fun(s1, s2)--> ans12

fun(s1, s3) --> ans13

fun(s1, s4) --> ans14

now

fun(ans12, ans13) --> f_ans1

fun(ans12, ans14) --> f_ans2

finally.

fun(f_ans1, f_ans2) --> this will finally return the required ans

Time complexity for k strings will be O( k2 * n2) where k is number of strings and n is length of string.

And space complexity will be O(n2)


Related Solutions

Given two functions, M(x, y) and N(x, y), suppose that (∂N/∂x − ∂M/∂y)/(M − N) is...
Given two functions, M(x, y) and N(x, y), suppose that (∂N/∂x − ∂M/∂y)/(M − N) is a function of x + y. That is, let f(t) be a function such that f(x + y) = (∂N/∂x − ∂M/∂y)/(M − N) Assume that you can solve the differential equation M dx + N dy = 0 by multiplying by an integrating factor μ that makes it exact and that it can also be written as a function of x + y,...
With being given two n-bit binary strings, verify if the strings are identical or not. Design...
With being given two n-bit binary strings, verify if the strings are identical or not. Design a procedure showing all steps and write a pseudo-code. What is the complexity of the procedure? If it can tolerate some error, is there a better than linear time solution? If so, write the pseudo-code.
Design an efficient algorithm to compute the number of binary strings with length n that satisfy...
Design an efficient algorithm to compute the number of binary strings with length n that satisfy 1 the regular expression ((0 + 11 + 101)∗ (1101))∗ .
find a recurrence relation for the number of bit strings of length n that contain two...
find a recurrence relation for the number of bit strings of length n that contain two consecutive 1s. What are the initial conditions? How many bit strings of length eight contain two consecutive 1s
Dynamic Programming Question. Make the table for finding a LCS (Longest Common Subsequence) of the strings...
Dynamic Programming Question. Make the table for finding a LCS (Longest Common Subsequence) of the strings SLWOVNNDK and ALWGQVNBKB. You need to Show the traceback for the LCS.
Two small spheres are suspended by strings of length L = 1.20 m from a ceiling...
Two small spheres are suspended by strings of length L = 1.20 m from a ceiling at points d = 3.20 m apart. They have equal mass of m = 8.00 kg and equal and opposite charge of q = 1.40E-4 C. Calculate the angle or angles the strings make from vertical at equilibrium.
Suppose X and Y are two independent random variables with X~N(1,4) and Y~N(4,6). a. P(X <...
Suppose X and Y are two independent random variables with X~N(1,4) and Y~N(4,6). a. P(X < -1.5). b. P(0.5Y+1 > 5). c. P(-2 < X + Y < 5). d. P(X – Y ≥ 3).
Build a Dynamic Programming algorithm in Python for the following problem. Given two sequences of vowels,...
Build a Dynamic Programming algorithm in Python for the following problem. Given two sequences of vowels, find the length of longest subsequence present in both of them. NOTE: A subsequence is a sequence that appears in the same order, but not necessarily contiguous. For example, “aei”, “aeo”, “eio”, “aiu”, ... are subsequences of “aeiou”. Sample Input 1: aeiou aiu Sample Output 1: 3 Sample Input 2: aeaiueoiuaeeoeooaauoi aeuioauuuoaeieeaueuiouaiieuiuuuaoueueauaeiauuo Sample Output 2: 16 Sample Input 3: iioioieiaiauaoeoiioiiue iuuueauiaieooaoaaouaaaae Sample Output 3:...
Find the arc length of the curve on the given interval. x= lnt , y =...
Find the arc length of the curve on the given interval. x= lnt , y = t + 1, 1 ≤ t ≤ 2
find a recurrence relation for the number of bit strings of length n that contain the...
find a recurrence relation for the number of bit strings of length n that contain the string 10. What are the initial conditions? How many bit strings of length eight contain the string 10
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT