Comparison of two methods of LCS

Source: Internet
Author: User

The dynamic programming problem generally has two elements: the optimal substructure and the sub-problem overlap.

In general, we use two methods when solving LCS problems:

1. Momo-ization (Memo method)

The overlapping sub-problem characteristics of this problem are exploited, and the overlapping sub-problem can be solved directly by using recursion.


0 A b C b D A b
0 0 0 0 0 0 0 0 0
B 0 0 1 1 1 1 1 1
D 0 0 1 1 1 2 2 2
C 0 0 1 2 2 2 2 2
A 0 1 1 2 2 2 3 2
B 0 1 2 2 3 3 3 4
A 0 1 2 2 3 3 4 4


The so-called top-down is the recursive solution from the largest point of the table below, the final result is LCS (x,y,x.length,y.length);

That is, the above table from the last grid up to the Sentinel process, before solving each sub-problem, we first detect this sub-problem before we have counted, if there is, then do not calculate the direct return results, if not, then calculate this sub-problem, then save the results for the next encounter when the use of.

Time complexity t (n) = O (MN);

Space complexity s = O (MN);//two-dimensional array


[Java] View plaincopy
01.public class Ttblcs {
int[][static] C = new int[100][100];
GENEVA static int NIF = 9999;
public static void Main (string[] args) {
char[] x = {' A ', ' B ', ' C ', ' B ', ' D ', ' A ', ' B '};
Char[] y = {' B ', ' D ', ' C ', ' A ', ' B ', ' A '};
07.
//ttblcs t = new Ttblcs ();
(int i = 0;i <= x.length;i++) {//around a circle of Sentinels are 0
Ten. for (int j = 0;j <= y.length;j++)
11. {
C[I][J] = NIF;
13.}
14.}
System.out.print (LCS (x,y,x.length,y.length));//top-down
16.}
17.
public static int LCS (char[] x,char[] Y,int I,int j) {
if (C[i][j] < NIF)//records are returned directly (memo) if calculated
return C[I][J];
if ((i = = 0) | | (j = = 0)) {
C[I][J] = 0;
23.}
. else if (x[i-1] = = Y[j-1])
C[I][J] = LCS (x,y,i-1,j-1) + 1;
. else
C[I][J] = LCS (X,Y,I-1,J) >= LCS (x,y,i,j-1)? LCS (X,Y,I-1,J): LCS (X,Y,I,J-1);
. return C[I][J];
29.}
30.
31.}


2. Dynamic Programming DP:

The so-called bottom-up, that is, from the subscript (mark) at the beginning of the process of solving, but to eliminate the recursive process, the bottom-up construction of the original problem of the solution, the first to solve the most basic situation, and then from the most basic situation of a part of the upward solution, such as I want to solve [2 ... 4], then I need to know first [2 ... 2][3 ... 4] and [2 ... 3][4 ... 4] The optimal solution, need to know [3 ... 4], then first need to know [3 ... 3][4 ... 4] The optimal solution, so, instead of the original problem needs to be built first, and then slowly up a layer of construction, the final composition of the original problem solution!.

Time complexity t (n) = O (MN);

Space complexity s = O (MN);//two-dimensional array



[Java] View plaincopy
01.public class Bttlcs {
int[][static] C = new int[100][100];
public static void Main (string[] args) {
char[] x = {' A ', ' B ', ' C ', ' B ', ' D ', ' A ', ' B '};
Char[] y = {' B ', ' D ', ' C ', ' A ', ' B ', ' A '};
. for (int k = 0;k <= x.length;k++)
07. {
C[0][k] = 0;
09.}
Ten. for (int k = 0;k <= y.length;k++)
11. {
C[k][0] = 0;
13.}
LCS (x, y);
15.}
16.
. public static void LCS (char[] x,char[] y) {
for (int i = 1;i <= x.length;i++)
19. {
for (int j = 1;j <= y.length;j++)
21. {
if (x[i-1] = = Y[j-1])
C[I][J] = c[i-1][j-1] + 1;
. else
C[I][J] = C[i-1][j] >= c[i][j-1]?c[i-1][j]:c[i][j-1];
26.}
27.}
. for (int i = 0;i <= x.length;i++)
29. {
. for (int j = 0;j<y.length;j++)
31. {
System.out.print (C[i][j]);
33.}
System.out.print ("\ n");
35.}
System.out.print (C[x.length][y.length]);
37.}
38.}


The top-down advantage is that there is no need to solve the solution of each sub-problem, but only the solution of the sub-problem that needs to be solved, the disadvantage is that recursive invocation is required and the function call wastes time.
The bottom-up advantage is that there is no need for recursive calls, each sub-problem solver faster, the shortcomings of each sub-problem to be calculated, even if the solution of this sub-problem to the original problem of the solution does not help!


Comparison of two methods of LCS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.