GATE / CS ARCHIVE

GATE CSE 2009 / QUESTION 53

Longest common subsequence recurrence

Algorithms · Dynamic Programming · Full GATE CSE 2009 paper

GATE 2009, Q53 2 marks · MCQ
Report issue

Dynamic Programming

Question 53, GATE 2009: Let l(i, j) be LCS length of prefixes of lengths i, j of zero-indexed X, Y. Boundary l=0 if either prefix empty; expr1 applies if last symbols match, expr2 otherwise. Which proposal is correct? A expr1=l(i−1, j)+1; B expr1=l(i, j−1); C expr2=max(l(i−1, j), l(i, j−1)); D expr2=max(l(i−1, j−1), l(i, j)).
Question 53, GATE 2009: Let l(i, j) be LCS length of prefixes of lengths i, j of zero-indexed X, Y. Boundary l=0 if either prefix empty; expr1 applies if last symbols match, expr2 otherwise. Which proposal is correct? A expr1=l(i−1, j)+1; B expr1=l(i, j−1); C expr2=max(l(i−1, j), l(i, j−1)); D expr2=max(l(i−1, j−1), l(i, j)).
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Let l(i, j) be the LCS length of the first i elements of X and first j elements of Y. If either prefix is empty, l(i, j)=0. When X[i−1]=Y[j−1], the matching final elements extend an optimal LCS of both shortened prefixes, so expr1=l(i−1, j−1)+1. If they differ, an LCS omits at least one of the two final elements, yielding expr2=max(l(i−1, j), l(i, j−1)). Only option C gives a correct proposed expression. A omits shortening Y in the matching case, B omits both the match increment and shortening X, and D is circular.

If the last symbols match, use that matched pair and extend the best solution for both shorter prefixes: l(i−1, j−1)+1. If they differ, a common subsequence must omit at least one of them, so take the larger of the two one-prefix-shortened values. Only C states a valid supplied formula. Option D references l(i, j) on its own right side and cannot define a bottom-up recurrence.

Original paper, page 12