GATE CS 2009 · Asymptotic Analysis & Recurrences

MCQ · 2 marks · Problem PROG-DIVC-0118

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 \(X[m]\) and \(Y[n]\) of lengths \(m\) and \(n\), respectively, with indexes of \(X\) and \(Y\) starting from 0.We wish to find the length of the longest common sub-sequence (LCS) of \(X[m]\) and \(Y[n]\) as \(l(m,n)\), where an incomplete recursive definition for the function \(l(i,j)\) to compute the length of the LCS of \(X[m]\) and \(Y[n]\) is given below:\[l(i,j)=\begin{cases}0 & \text{if either } i=0 \text{ or } j=0\\ \text{expr1} & \text{if } i,j>0 \text{ and } X[i-1]=Y[j-1]\\ \text{expr2} & \text{if } i,j>0 \text{ and } X[i-1]\ne Y[j-1]\end{cases}\]Which one of the following options is correct?
  1. A
    \(\text{expr1}=l(i-1,j)+1\)
  2. B
    \(\text{expr1}=l(i,j-1)\)
  3. C
    \(\text{expr2}=\max\big(l(i-1,j),\,l(i,j-1)\big)\)
  4. D
    \(\text{expr2}=\max\big(l(i-1,j-1),\,l(i,j)\big)\)

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solution\[\boxed{l(i,j)=\begin{cases}l(i-1,j-1)+1 & X[i-1]=Y[j-1]\\ \max\big(l(i-1,j),\,l(i,j-1)\big) & X[i-1]\ne Y[j-1]\end{cases}}\](A) and (B) are wrong: a matching pair extends the LCS of both shorter prefixes, \(l(i-1,j-1)+1\).(C) is correct: when the last elements differ, at least one of them is not in the LCS, so drop one and take the better result.(D) uses \(l(i,j)\) to define itself.Answer: C

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com