• edited by
13,935 views
40 40 votes

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 $I(i, j)$ to compute the length of the LCS of $X[m]$ and $Y[n]$ 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?

  1. $\text{expr1} = l\left(i-1, j\right) +1$

  2. $\text{expr1} = l\left(i, j-1\right)$

  3. $\text{expr2} = \max\left(l\left(i-1, j\right), l\left(i,j-1\right)\right)$

  4. $\text{expr2} = \max\left(l\left(i-1, j-1\right), l\left(i,j\right)\right)$

3 Answers

Best answer
45 45 votes

Answer is C. When the currently compared elements doesn't match, we have two possibilities for the LCS, one including X[i] but not Y[j] and other including Y[j] but not X[i].

/* Returns length of LCS for X[0..m-1], Y[0..n-1] */
int lcs( char *X, char *Y, int m, int n )
{
   if (m == 0 || n == 0)
     return 0;
   if (X[m-1] == Y[n-1])
     return 1 + lcs(X, Y, m-1, n-1);
   else
     return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
}
• selected by
27 27 votes

Answer is C. 

When the currently compared elements doesn't match, we have two possibilities for the LCS, one including $X\left [ i \right ]$ but not $Y\left [ j \right ]$  and other including $Y\left [ j \right ]$ but not $X\left [ i \right ]$.

• edited by
3 3 votes
The standard LCS recurrence is:

\[
l(i,j)=
\begin{cases}
0, & \text{if } i=0 \text{ or } j=0 \\[4pt]
l(i-1,j-1)+1, & \text{if } X[i-1]=Y[j-1] \\[4pt]
\max(l(i-1,j),\,l(i,j-1)), & \text{if } X[i-1]\ne Y[j-1]
\end{cases}
\]

Therefore,

\[
\text{expr1}=l(i-1,j-1)+1
\]

and

\[
\text{expr2}=\max(l(i-1,j),\,l(i,j-1))
\]

Explanation:

If \(X[i-1]=Y[j-1]\), then the matching character contributes \(1\) to the LCS, so

\[
l(i,j)=l(i-1,j-1)+1.
\]

If \(X[i-1]\neq Y[j-1]\), then either \(X[i-1]\) or \(Y[j-1]\) must be excluded, and we take the better of the two possibilities:

\[
l(i,j)=\max(l(i-1,j),\,l(i,j-1)).
\]

Hence,

\[
\boxed{\text{expr1}=l(i-1,j-1)+1}
\]

\[
\boxed{\text{expr2}=\max(l(i-1,j),\,l(i,j-1))}
\]

Among the given options, only Option C correctly specifies \(\text{expr2}\).
Answer:
Position:
Show:

Related questions

57 57 votes
4 answers 4 answers
22.8k
22.8k views
go_editor asked Apr 23, 2016
22,827 views
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 ...
36 36 votes
5 answers 5 answers
17.3k
17.3k views
Kathleen asked Sep 22, 2014
17,295 views
Consider the program below:#include <stdio.h int fun(int n, int *f_p) { int t, f; if (n <= 1) { *f_p = 1; return 1; } t = fun(n-1, f_p); f = t + *f_p; *f_p = t; return f;...
74 74 votes
8 answers 8 answers
32.6k
32.6k views
Kathleen asked Sep 22, 2014
32,563 views
In quick-sort, for sorting $n$ elements, the $\left(n/4\right)^{th}$ smallest element is selected as pivot using an $O(n)$ time algorithm. What is the worst case time com...
31 31 votes
5 answers 5 answers
14.5k
14.5k views
Kathleen asked Sep 22, 2014
14,511 views
Consider the following graph:Which one of the following is NOT the sequence of edges added to the minimum spanning tree using Kruskal’s algorithm?$\text{(b, e) (e, f) (a,...