A sub-sequence of a given sequence is just the given sequence with some elements (possibly none or all) left out. We are given two sequences and of lengths and , respectively with indexes of and starting from .
We wish to find the length of the longest common sub-sequence (LCS) of and as , where an incomplete recursive definition for the function to compute the length of the LCS of and is given below:
l(i,j) = 0, if either i = 0 or j = 0
= expr1, if i,j > 0 and X[i-1] = Y[j-1]
= expr2, if i,j > 0 and X[i-1] ≠ Y[j-1]
Which one of the following options is correct?
View worked solution
Answer: C
The function represents the length of the Longest Common Subsequence (LCS) of the prefixes and .
We can formulate the recurrence relation by analyzing the last characters of the prefixes, namely and :
-
Case 1: The last characters match ()
Since the characters match, they must be part of the LCS. Thus, the length of the LCS increases by , and we recursively find the LCS of the remaining prefixes of lengths and :
-
Case 2: The last characters do not match ()
Since the characters do not match, both cannot simultaneously be part of the LCS. The optimal solution is the maximum of two possibilities:
- Excluding :
- Excluding :
Thus, the recurrence is:
Comparing these with the given options:
- Option A is incorrect because should be .
- Option B is incorrect.
- Option C correctly matches our formulation for .
- Option D is incorrect.
Free practice · Your answers on this page are not saved.
