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]
The value of could be obtained by dynamic programming based on the correct recursive definition of of the form given above, using an array , where and , such that .
Which one of the following statements would be TRUE regarding the dynamic programming solution for the recursive definition of ?
View worked solution
Answer: B
To find the correct statement, let us analyze the recurrence relation for the Longest Common Subsequence (LCS) problem:
Dependency Analysis
To compute the value of , we require the values of:
- (top-left diagonal neighbor)
- (top neighbor)
- (left neighbor)
This means any evaluation order must ensure that these three cells are computed before .
- Row-Major Order: We fill the table row by row (from top to bottom, left to right). When computing , the previous row () and the left element in the current row () are already computed. Thus, this is a valid evaluation order.
- Column-Major Order: We fill the table column by column (from left to right, top to bottom). When computing , the previous column () and the top element in the current column () are already computed. Thus, this is also a valid evaluation order.
Hence, statement B is TRUE.
Elimination of Other Options
- Option A: Only the base cases (the first row and the first column ) must be initialized to . The remaining cells are computed and overwritten, so initializing the entire array to is not necessary.
- Option C: Incorrect, as both row-major and column-major orders are valid.
- Option D: This is too restrictive. For example, let and . Here, , but only depends on , , and . It does not depend on , so does not need to be computed before .
Free practice · Your answers on this page are not saved.
