UGC-NET Dec 2015 · Asymptotic Analysis & Recurrences

MCQ · 2 marks · Problem PROG-DIVC-0040

Given two sequences \(X\) and \(Y\):\(X=\langle a, b, c, b, d, a, b \rangle\)\(Y=\langle b, d, c, a, b, a \rangle\)A longest common subsequence of \(X\) and \(Y\) is:
  1. A
    \(\langle b, c, a \rangle\)
  2. B
    \(\langle c, a, b \rangle\)
  3. C
    \(\langle b, c, a, a \rangle\)
  4. D
    \(\langle b, c, b, a \rangle\)

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

Show the worked solution\[\boxed{c[i][j]=\begin{cases}c[i-1][j-1]+1 & x_i=y_j\\ \max\big(c[i-1][j],\,c[i][j-1]\big) & x_i\ne y_j\end{cases}}\]Filling the table gives an LCS length of 4 (for example \(\langle b,c,b,a\rangle\), \(\langle b,d,a,b\rangle\) or \(\langle b,c,a,b\rangle\)).(A) and (B) are common subsequences of length 3, so they are not longest.(C) \(\langle b,c,a,a\rangle\) is not a subsequence of \(X\): after \(c\), \(X\) has only one \(a\).(D) \(\langle b,c,b,a\rangle\) is in \(X\) (positions 2, 3, 4, 6) and in \(Y\) (positions 1, 3, 5, 6), and has length 4.The official UGC NET key was B, but \(\langle c,a,b\rangle\) is not the longest.Answer: D

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