Question

In: Computer Science

How to use python to find all longest common subsequences of 2 sequences? In addition, please...

How to use python to find all longest common subsequences of 2 sequences? In addition, please also analyze the space complexity.

For example, the input is "ABCBDAB" and "BDCABA", then the lcs should be "BCAB" "BDAB" and "BCBA".

Solutions

Expert Solution

Solution :

I have commented the complete program for better understanding :

# Function to return all LCS of sub-strings X[0..m-1], Y[0..n-1]
def LCS(X, Y, m, n, T):

        # if we have reached the end of either sequence
        if m == 0 or n == 0:
                # create a List with 1 empty string and return
                return [""]

        # if last character of X and Y matches
        if X[m - 1] == Y[n - 1]:

                # ignore last characters of X and Y and find all LCS of substring
                # X[0..m-2], Y[0..n-2] and store it in a List
                lcs = LCS(X, Y, m - 1, n - 1, T)

                # append current character X[m - 1] or Y[n - 1]
                # to all LCS of substring X[0..m-2] and Y[0..n-2]
                for i in range(len(lcs)):
                        lcs[i] = lcs[i] + (X[m - 1])

                return lcs

        # we reach here when the last character of X and Y don't match

        # if top cell of current cell has more value than the left cell,
        # then ignore current character of X and find all LCS of
        # substring X[0..m-2], Y[0..n-1]
        if T[m - 1][n] > T[m][n - 1]:
                return LCS(X, Y, m - 1, n, T)

        # if left cell of current cell has more value than the top cell,
        # then ignore current character of Y and find all LCS of
        # substring X[0..m-1], Y[0..n-2]
        if T[m][n - 1] > T[m - 1][n]:
                return LCS(X, Y, m, n - 1, T)

        # if top cell has equal value to the left cell, then consider both character

        top = LCS(X, Y, m - 1, n, T)
        left = LCS(X, Y, m, n - 1, T)

        # merge two Lists and return
        return top + left


# Function to fill lookup table by finding the length of LCS
# of substring X and Y
def LCSLength(X, Y, T):

        # fill the lookup table in bottom-up manner
        for i in range(1, len(X) + 1):
                for j in range(1, len(Y) + 1):
                        # if current character of X and Y matches
                        if X[i - 1] == Y[j - 1]:
                                T[i][j] = T[i - 1][j - 1] + 1

                        # else if current character of X and Y don't match
                        else:
                                T[i][j] = max(T[i - 1][j], T[i][j - 1])


# Function to find all LCS of X[0..m-1] and Y[0..n-1]
def findLCS(X, Y, T):

        # fill lookup table
        LCSLength(X, Y, T)

        # find all longest common sequences
        lcs = LCS(X, Y, len(X), len(Y), T)

        # since List can contain duplicates, "convert" the List to Set
        # and return
        return set(lcs)


if __name__ == '__main__':

        X = "ABCBDAB"
        Y = "BDCABA"

        # T[i][j] stores the length of LCS of substring X[0..i-1], Y[0..j-1]
        T = [[0 for x in range(len(Y) + 1)] for y in range(len(X) + 1)]

        lcs = findLCS(X, Y, T)
        print(lcs)

Code demo for reference :

Output :

Analyzing Time Complexity of the algorithm :

We are using a maximum of length(X)*length(Y) to store the common subsequences. Therefore the space complexity of the above solution is O(m*n).


Related Solutions

Write a Python application to find the longest ‘A’ path on a map of capital (uppercase)...
Write a Python application to find the longest ‘A’ path on a map of capital (uppercase) letters. The map is represented as a matrix (2-dimensional list) of capital letters. Starting from any point, you can go left, right, up and down (but not diagonally). A path is defined as the unbroken sequence of letters that only covers the spots with the letter A. The length of the A path is defined as the number of ‘A’s on the path. Your...
1. Please use Python 3 programing. 2. Please share your code. 3. Please show all outputs....
1. Please use Python 3 programing. 2. Please share your code. 3. Please show all outputs. Create a GUI Calculator with the following: Title : Calculator Label and Entry box for 1st Number Label and Entry box for 2nd Number Buttons for *, /, +, - Label for result and Displaying the result
Use the Bohr model to find the longest wavelength of light in the Paschen
Use the Bohr model to find the longest wavelength of light in the Paschen
Let X and Y be two arrays and you want to find their longest common sequence...
Let X and Y be two arrays and you want to find their longest common sequence (LCS). Describe how you can calculate it in a space complexity of O(min{|X|, |Y|}). NOT O(|X| * |Y|). Pseudocode is not necessary but a detailed explanation of the idea will be very helpful.
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
PLEASE USE PYTHON CODE 7. Use Newton's method to find the polynomial that fits the following...
PLEASE USE PYTHON CODE 7. Use Newton's method to find the polynomial that fits the following points: x = -3, 2, -1, 3, 1 y = 0, 5, -4, 12, 0
Please use the python and explain. Also please show python. Suppose that a cashier owes a...
Please use the python and explain. Also please show python. Suppose that a cashier owes a customer some change and that the cashier only has quarters, dimes, nickels, and pennies. Write a program the computes the minimum number of coins that the cashier can return. To solve this problem use the greedy algorithm explained below. PROBLEM STATEMENT: Your program should first ask the user for the amount of money he/she is owed (in dollars). You may assume that the user...
Please use python: # Problem Description Given a directed graph G = (V, E), find the...
Please use python: # Problem Description Given a directed graph G = (V, E), find the number of connected components in G. # Input The graph has `n` vertices and `m` edges. There are m + 1 lines, the first line gives two numbers `n` and `m`, describing the number of vertices and edges. Each of the following lines contains two numbers `a` and `b` meaning there is an edge (a,b) belong to E. All the numbers in a line...
Please solve questions 1 and 2. Please show all work and all steps. 1.) Find the...
Please solve questions 1 and 2. Please show all work and all steps. 1.) Find the solution xa of the Bessel equation t2x'' + tx' + t2x = 0 such that xa(0) = a 2.) Find the solution xa of the Bessel equation t2x'' + tx' + (t2-1)x = 0 such that x'a(0) = a
how to find all the neighbors in a n-dimensional array in python? thank you in advance...
how to find all the neighbors in a n-dimensional array in python? thank you in advance all the neighboors of a coordinate*
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT