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...
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
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
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*
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
1. What are the main data types in Python? Which data types are sequences? 2. What...
1. What are the main data types in Python? Which data types are sequences? 2. What are some general guidelines for naming a ‘variable’ in Python? Give at least three examples of variable names that follow these guidelines 3. Provide an example of the Python syntax to create a variable to hold the age of a building (for example,40 years old). a. Provide the Python syntax to test whether the variable created above is more than 35, and print the...
Can someone please explain how to use ladder operators for the addition of angular momentum, it...
Can someone please explain how to use ladder operators for the addition of angular momentum, it can be found in chapter 4 of Griffiths intro to quantum mechanics. Please provide some examples of this, you may use problems from chapter four of the book. Thank you.
Write a program in python language, which accepts 2 numbers and a + sign (for addition)...
Write a program in python language, which accepts 2 numbers and a + sign (for addition) A sign - (for subtraction) A sign * (for multiplication), / (for division) Then calculate and to display the result of the operation he chose with the two numbers. Displaying the appropriate message
Find a closed formula for each of the following sequences. Show all work and explain your...
Find a closed formula for each of the following sequences. Show all work and explain your answers. (a) {1, 6, 17, 34, 57, 86, 121, . . .}, where a0 = 1. (b) an = 5an−1 + 4, a0 = 2 (c) an = 10an−1 − 21an−2, a0 = 6, a1 = 26.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT