Home » What is the time complexity of LCS?

What is the time complexity of LCS?

longest common subsequence

by akshaysharma12
longest common subsequence

Time complexity and LCS are both important topics you do not want to skip. But if you are even slightly unsure about them, it is difficult to find the right, desirable answers. 

Whether it be how to reverse a string in c, understanding the definition of time complexity or the longest common subsequence! Every concept becomes increasingly difficult to analyze and understand that are listed on the internet. So many questions but only limited answers are available!

But fortunately for you, we have suitable answers to all your questions regarding time complexity and LCS. Read till the end to get it all right.

What is time complexity?

The concept of time complexity is very complex in itself. If you missed that one lesson about time complexity, do not worry, we have got you. We will brief you on the topic in an understandable way.

Time complexity is a word used in the context of programming. It depicts the time taken to process and execute a code or an algorithm in relation to the size of the code or the algorithm.

The concept analyses the overall time taken to execute an algorithm. It also inclucates and studies the change in the time taken to successfully execute the process. 

Now that you know what time complexity is, let us move on to LCS.

What is LCS?

LCS stands for Longest Common Subsequence. It entails finding the longest subsequence that is shared by all the sequences in an array. The common elements that will compose the LCS do not have to be in consecutive order in their own sequence, but they have to be in the order of their position. For example, if ABC is the LCS of a string set, then ABC needs to appear in that order in the string.

Let us take a look at an example.

In a set of sequences, the string sequences are ABJYTF, YLTFRC, BYTFA and YTBJFK. YTF is the only subsequence present in all four sequences. The LCS of these sequences is thus 3 since the length of YTF is 3.

In most cases, they are only two sequences. You can also have a look at  reverse a string in c to make the strings more compatible whenever necessary.

Let us now move on to the time complexity of LCS.

What is the time complexity of LCS?

The time complexity of LCS depends solely on the approach you take to get to the solution. However, it must also be noted that sometimes the time complexity of a code or an algorithm could also depend on your computer, network, connectivity, speed and other such external factors.

Nevertheless, there are two approaches incorporated to find the Longest Common Subsequence of a set which have been identified below:

Recursive Approach

This approach deals with going through all the possible subsequences in a set to determine the common ones among them and then choose the longest one of them as the LCS.

Here, you need to make two indexes, i and j, for each string under scrutiny.

The time complexity in this brute and naïve approach would be O(2n) in the worst scenarios. Here, n is the length of the shortest string. When the X and Y strings do not have any LCS, that is the worst scenario. 

The space complexity is O(1) since no additional space is being utilized in this approach.

Breaking down the process

Following is a partial breakdown of the recursive approach for input strings BGHS and BHPG. 

lcs(“BGHS”, “BHPG”) leads [on one side] to lcs(“BGH”, “BHPG”) which further leads to lcs(“BG”, “BHPG”) and lcs(“BGH”, “BHP”).

On the other side, lcs(“BGHS”, “BHPG”) leads to lcs(“BGHS”, “BHP”) which further leads to lcs(“BGH”, “BHP”) and lcs(“BGHS”, “BH”).

The problem with the Recursive Approach

In this scheme of things, LCS(“BGH,” “BHP”) is being solved twice. And considering this is only a partial breakdown, you will find more such groups further being solved more than once. This causes an overlap which is a cause for the high time complexity of this method.

The problem of overlapping can be solved with memorization or tabulation in the dynamic programming approach.

Dynamic Programming Approach

To optimize the time complexity of the previous approach, we can make use of the dynamic programming approach. Here the value of the longest common subsequence is stored and determined in a two-dimensional array.

The time complexity with this approach is O(nm) in the worst-case scenarios where n and m are the lengths of the strings. This is a lot better than that of the recursive approach.

And since this approach requires a table to get the dp[][] with n rows and m columns, the space complexity would be the same as the time complexity, i.e., O(nm).

Questions asked regarding LCS time complexity in an interview

Before we sign things off, we would also like you to go through some interview questions relating to time complexity, LCS or both. They are as follows:

  • How do you find LCS?
  • What is the time complexity of the LCS in the Dynamic programming approach?
  • What is the time complexity of the LCS in the Recursive approach?
  • What is the space complexity of the Dynamic programming approach in LCS?
  • What is the space complexity of the Recursive approach in LCS?
  • Which approach would you choose in finding the LCS, dynamic programming or recursive?
  • What is time complexity?
  • Why is the recursive approach not the right way to go?
  • How does the recursive approach work in LCS?
  • How does the dynamic programming approach work in LCS?
  • Give a breakdown of the recursive tree when finding an LCS.

Conclusion

That was all you needed to know about time complexity, longest common subsequence and time complexity in finding the LCS of two or more strings. 

You can either use the recursive approach, which has a higher time complexity or the dynamic programming approach, which avoids making repetitive checks made by the first approach.

You may also like

Leave a Comment