All Exams Test series for 1 year @ ₹349 only
Question

The longest common subsequence of $\{1,2,3,2,4,1,2\}$ and $\{2,4,3,1,2,1\}$ is

The correct answer is
2,3,2,1

Understanding Longest Common Subsequence (LCS)

The problem asks us to find the Longest Common Subsequence (LCS) between two given sequences. A subsequence is formed by deleting zero or more elements from a sequence without changing the order of the remaining elements. A common subsequence must be derivable from both sequences while maintaining the relative order of its elements. The LCS is the common subsequence that has the maximum possible length.

Analyzing the Sequences

The two sequences provided are:

  • Sequence 1 ($S_1$): $1, 2, 3, 2, 4, 1, 2$
  • Sequence 2 ($S_2$): $2, 4, 3, 1, 2, 1$

Our task is to find the longest possible sequence that is a subsequence of both $S_1$ and $S_2$.

Finding the LCS Between Sequences

We will evaluate each option to determine if it is a common subsequence and identify the longest one.

Option 1: ${2,1,2,3}$

Let's check if ${2,1,2,3}$ is a subsequence of $S_1$ ($1, 2, 3, 2, 4, 1, 2$). We can find the first $2$ (index 1), but the next required element is $1$, which appears later (index 5). After finding $1$, we need another $2$, which appears at index 6. However, the sequence requires $3$ after the second $2$ (or after the $1$), which is not possible in $S_1$ while maintaining the order. Therefore, ${2,1,2,3}$ is not a subsequence of $S_1$ and thus not a common subsequence.

Option 2: ${1,3,2,1}$

First, let's check $S_1$ ($1, 2, 3, 2, 4, 1, 2$). We can find $1$ (index 0), then $3$ (index 2), then $2$ (index 3), and finally $1$ (index 5). The indices {0, 2, 3, 5} maintain the relative order, confirming ${1,3,2,1}$ is a subsequence of $S_1$.

Now, let's check $S_2$ ($2, 4, 3, 1, 2, 1$). We need to find the elements $1$, $3$, $2$, $1$ in the correct relative order.

  • We look for the first element, $1$. It appears at index 3 ($S_2[3]=1$) and index 5 ($S_2[5]=1$).
  • If we choose $S_2[3]=1$, we need to find $3$ *after* index 3. There are no $3$s after index 3 in $S_2$.
  • If we choose $S_2[5]=1$, we need to find $3$ *after* index 5. There are no $3$s after index 5 in $S_2$.

It is impossible to find the element $1$ followed by $3$ in $S_2$. Therefore, ${1,3,2,1}$ is not a subsequence of $S_2$ and cannot be a common subsequence.

Option 3: ${2,3,2,1}$

Let's check $S_1$ ($1, 2, 3, 2, 4, 1, 2$). We can find $2$ (index 1), then $3$ (index 2), then $2$ (index 3), and then $1$ (index 5). The indices {1, 2, 3, 5} maintain the relative order. This confirms ${2,3,2,1}$ is a subsequence of $S_1$.

Now, let's check $S_2$ ($2, 4, 3, 1, 2, 1$). We can find $2$ (index 0), then $3$ (index 2), then $2$ (index 4), and then $1$ (index 5). The indices {0, 2, 4, 5} maintain the relative order. This confirms ${2,3,2,1}$ is a subsequence of $S_2$.

Since ${2,3,2,1}$ is a subsequence of both $S_1$ and $S_2$, it is a common subsequence. Its length is 4.

Option 4: ${2,3,1,2,1}$

Let's check $S_1$ ($1, 2, 3, 2, 4, 1, 2$). We can find $2$ (index 1), then $3$ (index 2), then $1$ (index 5), then $2$ (index 6). This forms the subsequence ${2, 3, 1, 2}$ using indices {1, 2, 5, 6}. However, the option requires a final $1$ after the last $2$. There are no more elements in $S_1$ after index 6. Thus, ${2,3,1,2,1}$ is not a subsequence of $S_1$ and cannot be a common subsequence.

Conclusion on LCS Length

Based on our analysis:

  • Options 1, 2, and 4 are not common subsequences because they are not valid subsequences of both $S_1$ and $S_2$.
  • Option 3, ${2,3,2,1}$, is a common subsequence of both $S_1$ and $S_2$, and its length is 4.

Standard algorithms used to find the LCS confirm that the maximum length of a common subsequence for these two specific sequences is 4. Since ${2,3,2,1}$ has length 4 and is confirmed to be a common subsequence, it is the Longest Common Subsequence among the given choices.

Was this answer helpful?

Important Questions from Dynamic Programming

  1. Consider a table $T$, where the elements $T[i][j], 0 \le i,j \le n$, represent the cost of the optimal solutions of different subproblems of a problem that is being solved using a dynamic programming algorithm. The recursive formulation to compute the table entries is as follows:

    $T[0][k] = T[k][0] = 1$ for $k = 0,1,2, \dots, n$
    $T[i][j] = 2T[i - 1][j] + 3T[i][j - 1]$ for $1 \le i,j \le n$

    Consider the following two algorithms to compute entries of $T$. Assume that for both the algorithms, for all $0 \le i,j \le n$, $T[i][j]$ has been initialized to 1.

    Algorithm $B_1$:
    For $i = 1, 2, \dots, n$
    For $j = 1, 2, \dots, n$
    $T[i][j] = 2T[i - 1][j] + 3T[i][j - 1]$

    Algorithm $B_2$:
    For $s = 2, 3, \dots, 2n$
    For $i = 1, 2, \dots, n$
    For $j = 1, 2, \dots, n$
    If ($i + j == s$)
    $T[i][j] = 2T[i - 1][j] + 3T[i][j - 1]$

    Algorithm $B_k, k \in \{1,2\}$ is said to be correct if and only if it calculates the correct values of $T[i][j]$, for all $0 \le i,j \le n$, (as per the recursive formulation) at the end of the execution of the algorithm $B_k$.

    Which one of the following statements is true?
  2. Consider the problem of a chain <A1, A2, A3> of three matrices. Suppose that the dimensions of the matrices are 10 × 100, 100 × 5 and 5 × 50 respectively. There are two different ways of parenthesization : (i) ((A1 A2)A3) and (ii) (A1(A2 A3)). Computing the product according to the first parenthesization is ________ times faster in comparison to the second parenthesization.

  3. Let \(n = 4\) and \((a_{1}, a_{2}, a_{3}, a_{4}) = (\text{do, if, int, while})\). Let \(p(1:4) = \left(\frac{3}{8}, \frac{3}{8}, \frac{1}{8}, \frac{1}{8}\right)\) and \(q(1:4) = \left(\frac{2}{8}, \frac{3}{8}, \frac{1}{8}, \frac{1}{8}, \frac{1}{8}\right)\) where p(i) and q(i) denotes the probability with which we search \(a_{i}\) and the identifier x being searched satisfy \(a_{i} \lt x \lt a_{i+1}\) respectively. The optimal search tree is given by :

  4. 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\).

    The longest common subsequence of X and Y is :

  5. The travelling salesman problem can be solved in :

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App