## Introducing Dynamic Programming

And the bottom up approach to solve problems which exihibit traits as described above is called Dynamic Programming. Two re-iterate, the two similarities in all the problems which can be solved using the Dynamic Programming are:

- “An optimal solution to a problem contains and optimal solution to a sub problem.”
- “The recursive solution contains few distinct sub problems repeated more than once.”

Programming is a very old term and it was not always a synonym for ‘writing computer programs’. It meant to be a tabular way of calculation with rows and columns of instruction or data, and so are the programming languages. If you are familiar with the Assembly language, it also has the same tabular format of programming where each row contains a set of 2 or more columns and a row is an instruction which performs a task.

We use the tabular format to explain the solution to finding the length of the longest common subsequence using dynamic programming. We consider the same strings for this demonstration. Create a table with each character of first sequence as columns and each character of the second sequence as rows like the below table:

Now we definitely need to know how did we populate this table. Here is the rule,

We are evaluating each cell one at a time, suppose we are evaluating cell (i, j ) i.e. i^{th} row and j^{th} column, then we check if the character of row i matches the character of column j. Below two cases arise:

- If the two characters are equal, we add one to the longest length calculated in the the previous row and column and store it in cell(i,j), it is the same case as the one we discussed where X[i] = Y[j], the length of the longest subsequence evaluates to be 1 + L
_{i-1,j-1}.

That means we take the value from cell(i-1,j-1), add 1 to it and store it in cell(i,j). - If the two characters are not equal, we find the maximum value among the two cell(i, j-1) and cell(i-1, j). This is again in accordance of the statements we proved previously in the discussion where X[i] != X[j], L
_{i,j}= max(L<sub<i-1,j), L_{i,j-1}).

The first row and column will always have the value zero because they don’t contain any character.

Lets calculate the value of cell(2,2) assuming the index starts at 0 ,0. The second row contains character D and the second column contains character B. Both the characters don’t match so the value of cell(1,2) will have the value which is maximum of [cell(2,1) and cell(1,2)] i.e.e maximum of (0,1) which is 1. Hence, cell(2,2) will contain the value 1. This also means that the length of the longest common subsequence of the two strings **A B** and **B D** is one.

Now we calculate the value of cell(2,5), the second row contains the character D and the fifth row contains D as well. Both the characters match so the value of cell (2,5) will be 1 + the value of cell (2-1, 5-1) as per the discussions we had above. L_{i,j} = L_{i-1, j-1} + 1.

In a similar way we can populate the whole table. The value in the cell (M,N) is the length of the longest common subsequence. In this case it is 4. So what is the running time of the algorithm?

Clearly it is the time taken to populate the whole table, there is a constant amount of work involved in looking at the two values, comparing them and figuring out the value of a given cell in the table, it actually depends on the previous calculations. And this constant work has to be done for M * N cells, so the total running time is proportional to M * N.

We can say that T = O(M * N) . We can also define it as a stricter bound by using the theta notation, but I don’t want to dwell into that at this moment of time, we can just understand that it is proportional to M * N.

Same with the space complexity, which looks to be proportional to M * N because we have used a table with M rows and N columns. Well that can be reduced to Max(M, N) as you can see to calculate the value of a given cell, we need the current row and 2 cells from the previous row.

## Source Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class LCS { public static void main(String[] args) { String X = "ABCDABCADBC"; String Y = "AXBDCABC"; LCS driver = new LCS(); int[][] dpLCS = driver.dpLCS(X, Y); System.out.println(driver.backtrack(dpLCS, X.toCharArray(), Y.toCharArray(), X.length(), Y.length())); } private StringBuilder backtrack(int[][] dpTable, char[] charX, char[] charY, int i, int j) { StringBuilder sb = new StringBuilder(); if (i == 0 || j == 0) return sb; else if (charX[i - 1] == charY[j - 1]) return backtrack(dpTable, charX, charY, i - 1, j - 1).append(charX[i - 1]); else if (dpTable[i][j - 1] > dpTable[i - 1][j]) return backtrack(dpTable, charX, charY, i, j - 1); else return backtrack(dpTable, charX, charY, i - 1, j); } private int[][] dpLCS(String X, String Y) { if (X == null || X.length() <= 0 || Y == null || Y.length() <= 0) return null; int[][] dpTable = new int[X.length() + 1][Y.length() + 1]; char[] charX = X.toCharArray(); char[] charY = Y.toCharArray(); for (int i = 0; i < charX.length; i++) { for (int j = 0; j < charY.length; j++) { if (charX[i] == charY[j]) dpTable[i + 1][j + 1] = dpTable[i][j] + 1; else if (charX[i] != charY[j]) { dpTable[i + 1][j + 1] = Math.max(dpTable[i + 1][j], dpTable[i][j + 1]); } } } return dpTable; } } |

## Conclusion

So this is how we calculate the length of the longest subsequence, we also need to find the longest subsequences and write code for the same, which we plan to do in the next article. There we will calculate the actual time and space complexity of the whole algorithm along with part to find the LCS.

Hope this helps, happy learning. Stay connected and stay Subscribed