GATE Exam  >  GATE Questions  >  The following code is a naïve implementa... Start Learning for Free
The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?
/* 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));
}
  • a)
    O(n)
  • b)
    O(mn)
  • c)
    O(m2n2)
  • d)
    O(m)
Correct answer is option 'B'. Can you explain this answer?
Verified Answer
The following code is a naïve implementation of the LCS problem. ...
int lcs( char *X, char *Y, int m, int n )
{
int L[m+1][n+1];
int i, j;
/* Following steps build L[m+1][n+1] in bottom up fashion. Note
that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
for (i=0; i<=m; i++)
{
for (j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i-1] == Y[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
/* L[m][n] contains length of LCS for X[0..n-1] and Y[0..m-1] */
return L[m][n];
}
Hence when tabulation is used , distinct function calls = (m+1)(n+1)
Hence time complexity = O(mn).
View all questions of this test
Most Upvoted Answer
The following code is a naïve implementation of the LCS problem. ...
Understanding the LCS Problem
The Longest Common Subsequence (LCS) problem finds the longest sequence that can appear in the same order in two sequences, without rearranging their characters. The given code is a naive recursive implementation of the LCS problem.
Time Complexity of Naive Implementation
- The naive implementation has an exponential time complexity of O(2^(m+n)), as each character in both strings leads to two recursive calls.
Tabulation Approach
- When tabulation (or dynamic programming) is implemented, we use a 2D table to store the lengths of LCS for substrings of X and Y.
Why O(mn)?
- The tabulation approach fills up a table of size (m+1) x (n+1) where:
- m is the length of string X.
- n is the length of string Y.
- Each entry in the table corresponds to a specific subproblem, specifically the LCS length for various prefixes of X and Y.
- Filling each cell in the table takes constant time O(1), and since there are (m+1)(n+1) cells, the overall time complexity becomes:
Final Complexity Calculation
- Therefore, the time complexity for the tabulation method is O(mn), as we systematically solve and store results for all subproblems.
In summary, when using tabulation for the LCS problem, the complexity is significantly improved to O(mn), making it efficient for larger strings compared to the naive recursive method.
Explore Courses for GATE exam

Similar GATE Doubts

The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?/* 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);elsereturn max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));}a)O(n)b)O(mn)c)O(m2n2)d)O(m)Correct answer is option 'B'. Can you explain this answer?
Question Description
The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?/* 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);elsereturn max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));}a)O(n)b)O(mn)c)O(m2n2)d)O(m)Correct answer is option 'B'. Can you explain this answer? for GATE 2024 is part of GATE preparation. The Question and answers have been prepared according to the GATE exam syllabus. Information about The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?/* 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);elsereturn max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));}a)O(n)b)O(mn)c)O(m2n2)d)O(m)Correct answer is option 'B'. Can you explain this answer? covers all topics & solutions for GATE 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?/* 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);elsereturn max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));}a)O(n)b)O(mn)c)O(m2n2)d)O(m)Correct answer is option 'B'. Can you explain this answer?.
Solutions for The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?/* 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);elsereturn max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));}a)O(n)b)O(mn)c)O(m2n2)d)O(m)Correct answer is option 'B'. Can you explain this answer? in English & in Hindi are available as part of our courses for GATE. Download more important topics, notes, lectures and mock test series for GATE Exam by signing up for free.
Here you can find the meaning of The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?/* 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);elsereturn max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));}a)O(n)b)O(mn)c)O(m2n2)d)O(m)Correct answer is option 'B'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?/* 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);elsereturn max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));}a)O(n)b)O(mn)c)O(m2n2)d)O(m)Correct answer is option 'B'. Can you explain this answer?, a detailed solution for The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?/* 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);elsereturn max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));}a)O(n)b)O(mn)c)O(m2n2)d)O(m)Correct answer is option 'B'. Can you explain this answer? has been provided alongside types of The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?/* 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);elsereturn max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));}a)O(n)b)O(mn)c)O(m2n2)d)O(m)Correct answer is option 'B'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice The following code is a naïve implementation of the LCS problem. What is the complexity of the algorithm if tabulation is implemented?/* 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);elsereturn max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));}a)O(n)b)O(mn)c)O(m2n2)d)O(m)Correct answer is option 'B'. Can you explain this answer? tests, examples and also practice GATE tests.
Explore Courses for GATE exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev