Shortest Common Super-sequence in Dynamic Programming
Programming

Shortest Common Super-sequence in Dynamic Programming

From arrays to linked lists, you can essentially solve any problem statement with Dynamic Programming.

Ishita Juneja
Ishita Juneja
7 min read

According to the statistics produced in the last fiscal year, the average salary of a Dynamic Developer for any mid-level tech company is closer to 1,30,000 INR.

All the while, people have been ignoring the sheer potential of developing arrays and creating strings. But, by looking at this figure, we can say that it definitely pays off to learn the important concepts of DSA if you are interested in full-stack development.

One such concept that we are going to unfold is related to finding the shortest common supersequence in a string.

Now, the string data structure is known for holding elements or more specifically characters for the program whenever they are required.

Henceforth, the shortest running supersequence would be the characters of the array that resemble the most number of similar alphabets.

This blog uncovers this topic in depth while helping you get beginner level details related to Dynamic Programming.

 

What is Dynamic Programming?

In technical terms, you can define the concept of Dynamic Programming as a class function that effectively helps in solving problems in computer science by overlapping the subproblems.

One of the main purposes of dividing a problem into subsets or subproblems is that it allows the program to reach the output faster and more efficiently.

Dynamic programming is one of the top methods applied for solving array based problems such as to rearrange array alternately or finding the kth smallest element in an array.

Now, you may ask how Dynamic Programming solves the time complexity of the programs by overlapping the subproblems?

Well, the main purpose of overlapping or dividing the problem into subproblems is that you can effectively store the value of the problem subsets for later.

This is essentially a computerised way of remembering the solution to a problem that has been previously visited, thus saving time and space.

The concept of Dynamic Programming helps in improving the functioning of the CPU because now the system is not required to calculate the solution to the same problems time and again.

That said, since the approach of Dynamic Programming effectively solves the problems related to arrays, strings and sequences, we are here to discuss the shortest common supersequence problem in DP.

So let's get started with the definition and moving forward you will get to know how you can determine the shortest common supersequence for a given sequence of data.


What do you mean by Shortest Common Supersequence in Dynamic Programming?

Imagine that we have two sequences namely, A and B. Now, it is predefined that these sequences have a few characters in common that may or may not be arranged in a sorted order within these sequences.

Now, the problem statement for the shortest common supersequence suggests finding a common supersequence between the two given sequences of a minimal length.

It is important to note that this supersequence regardless of its length is not unique for the sequence provided in the problem statement.

Also, this minimal length supersequence can be effectively formed and calculated by analysing the longest common supersequence of the given data.

Let's find out more about finding the shortest common supersequence for a given sequence of data in the next section.

 

How to find the Shortest Common Supersequence in Dynamic Programming?

Consider the following statement that approaches the problem of finding the shortest common supersequence in a given string.

 

Problem Statement:

You have been given two strings namely str1 and str2. Your task is to figure out the size of the shortest sequence amongst both the strings that has both str1 and str2 as its supersequences.

Now, there are essentially four different approaches that we can effectively apply for solving the problem statement mentioned above.

Let's discuss each of these approaches in detail.

 

Method 1: Finding the Longest Common Supersequence

Naturally when we are faced with a problem concerning finding the minimal length of supersequence amongst a set if string, we can approach the problem by finding the longest common supersequence first and then further iterating on the concept.

Here's how the algorithm for this approach would work:

For the strings str1 and str2, start by finding the longest common supersequence.Next up, you will be inserting the characters that do not involve the longest supersequence that is common between the two strings and input them in the lcs.Now, once we have found the first occurance of the entire chain of supersequence that is common in both the strings, we will add a few common characters and print the program.

This way, we are only considering a portion of the longest common supersequence.

Time Complexity for this approach:

O(m*n)

 

Method 2: Approaching by the Recursive Solution

The idea behind using this method is to recursively analyse the two strings and check their last elements for any matching characters. 

Here's how the algorithm for the recursive function would work:

Start by comparing each of the characters of the given strings one at a time and input the matching characters in a third string.Traverse till the end of both strings and finally reach the last elements.Once the shortest common supersequence is determined print the elements of the third string that you have created.

Time Complexity for this approach:

O(2min(m,n))

 

Method 3: Using the Dynamic Programming Approach

The idea of this approach is to improve the efficiency of the recursive solution by iterating on the same results obtained earlier.

Here's how the algorithm for implementing the Dynamic Programming Approach would work:

Start with iterating and searching the given string sequences using the recursive algorithm.Now, call the functions for Dynamic Programming and store the values of the predetermined shortest common supersequence in a seperate string.Finally, when the traversal is completed, print the results of the program.

Time Complexity for this approach:

O(m*n)

 

Final Thoughts

One of the greatest advantages of using Dynamic Programming in any problem is that, even after the program is completed, you can still access the solution, results and the functions used within the program.

Dynamic Programming allows the system memory to store the information generated while running the program within a table so that it can be implemented later.

From arrays to linked lists, you can essentially solve any problem statement such as rearrange array alternately or finding the nth node of a linked list using Dynamic Programming.

Discussion (0 comments)

0 comments

No comments yet. Be the first!