Question

In: Computer Science

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.

Solutions

Expert Solution

Let the input sequences be X[0..m-1] and Y[0..n-1] of lengths m and n respectively. And let L(X[0..m-1], Y[0..n-1]) be the length of LCS of the two sequences X and Y. Following is the recursive definition of L(X[0..m-1], Y[0..n-1]).If last characters of both sequences match (or X[m-1] == Y[n-1]) then L(X[0..m-1], Y[0..n-1]) = 1 + L(X[0..m-2], Y[0..n-2])

If last characters of both sequences do not match (or X[m-1] != Y[n-1]) then

L(X[0..m-1], Y[0..n-1]) = MAX ( L(X[0..m-2], Y[0..n-1]), L(X[0..m-1], Y[0..n-2]) ). So this problem has Overlapping Substructure property and recomputation of same subproblems can be avoided by either using Memoization or Tabulation.Time Complexity of the above implementation is O(mn).One important observation in above simple implementation is, in each iteration of outer loop we only, need values from all columns of previous row. So there is no need of storing all rows in our DP matrix, we can just store two rows at a time and use them, in that way used space will reduce from L[m+1][n+1] to L[2][n+1].

This uses O(mn) space where m and n are length of given strings .If only the length of LCS is required,the sqpace complexity of the solution can be improved up to O(min(m,n)) as we are reading only from previous row of current row


Related Solutions

Let X and Y be two discrete random variables with a common mgf of e^(4t). After...
Let X and Y be two discrete random variables with a common mgf of e^(4t). After some analysis you conclude, X = 4 and Y = 6. Is this a valid claim? Why or why not?
Let X and Y be two discrete random variables with a common mgf of e^(4t). After...
Let X and Y be two discrete random variables with a common mgf of e^(4t). After some analysis you conclude, X = 4 and Y = 6. Is this a valid claim? Why or why not?
Let X and Y be two independent random variables such that X + Y has the...
Let X and Y be two independent random variables such that X + Y has the same density as X. What is Y?
Let X, Y ⊂ Z and x, y ∈ Z Let A = (X\{x}) ∪ {x}....
Let X, Y ⊂ Z and x, y ∈ Z Let A = (X\{x}) ∪ {x}. a) Prove or disprove: A ⊆ X b) Prove or disprove: X ⊆ A c) Prove or disprove: P(X ∪ Y ) ⊆ P(X) ∪ P(Y ) ∪ P(X ∩ Y ) d) Prove or disprove: P(X) ∪ P(Y ) ∪ P(X ∩ Y ) ⊆ P(X ∪ Y )
For the arrays x and y given below, use MATLAB to find all the elements in x that are greater than the corresponding elements in y.
For the arrays x and y given below, use MATLAB to find all the elements in x that are greater than the corresponding elements in y.x = [-3, 0, 0, 2, 6, 8] y = [-5, -2, 0, 3, 4, 10]
Let X ∼ Poisson(λ) and Y ∼ U[X, 2X]. Find E(Y ) and V ar(Y ).
Let X ∼ Poisson(λ) and Y ∼ U[X, 2X]. Find E(Y ) and V ar(Y ).
Let f(x,y)= (3/2)(x^2+y^2 ) in 0≤x≤1, 0≤y≤1. (a) Find V(X) (b) Find V(Y)
Let f(x,y)= (3/2)(x^2+y^2 ) in 0≤x≤1, 0≤y≤1. (a) Find V(X) (b) Find V(Y)
Let X and Y be two independent random variables. X is a binomial (25,0.4) and Y...
Let X and Y be two independent random variables. X is a binomial (25,0.4) and Y is a uniform (0,6). Let W=2X-Y and Z= 2X+Y. a) Find the expected value of X, the expected value of Y, the variance of X and the variance of Y. b) Find the expected value of W. c) Find the variance of W. d) Find the covariance of Z and W. d) Find the covariance of Z and W.
Let f(x,y) = 3x3 + 3x2 y − y3 − 15x. a) Find and classify the...
Let f(x,y) = 3x3 + 3x2 y − y3 − 15x. a) Find and classify the critical points of f. Use any method taught during the course (the second-derivative test or completing the square). b) One of the critical points is (a,b) = (1,1). Write down the second-degree Taylor approximation of f about this point and motivate, both with computations and with words, how one can see from this approximation what kind of critical point (1,1) is. Use completing the...
Let f(x,y) = 3x3 + 3x2 y − y3 − 15x. a) Find and classify the...
Let f(x,y) = 3x3 + 3x2 y − y3 − 15x. a) Find and classify the critical points of f. Use any method taught during the course (the second-derivative test or completing the square). b) One of the critical points is (a,b) = (1,1). Write down the second-degree Taylor approximation of f about this point and motivate, both with computations and with words, how one can see from this approximation what kind of critical point (1,1) is. Use completing the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT