Dynamic Programming solves the longest public subsequence Problem

Source: Internet
Author: User

Dynamic Programming

Complex problems often occur. Instead of simply breaking down them into several subproblems, they may break down a series of subproblems. Simply resolve a large problem into a sub-problem, and combine the sub-problem solution to export the solution of the big problem. the time consumed for solving the problem increases in a power series according to the scale of the problem.

To reduce the time required to repeatedly find the same subproblem, an array is introduced, no matter whether they are useful for the final solution or not, to resolve all subproblems in the array, this is the basic method used by dynamic programming.

[Problem] calculates the longest common character subsequence of a two-character sequence.

Problem description: The subsequence of a character sequence is a character sequence formed by removing a number of characters (either one or not) from a given Character Sequence at Will (not necessarily consecutive. Make the given character sequence X = "x0, X1 ,..., Xm-1 ", sequence y =" y0, Y1 ,..., Yk-1 is a subsequence of X, there is a strictly incrementing subscript sequence of x <I0, i1 ,..., Ik-1>, making for all J = ,..., K-1 with Xij = YJ. For example, x = "abcbdab" and Y = "bcdb" are subsequences of X.

Consider how to break down the longest common subsequence into sub-problems, set a = "A0, A1 ,..., Am-1 ", B =" B0, B1 ,..., Bm-1 ", and z =" z0, Z1 ,..., Zk-1 "is their longest common subsequence. It is not hard to prove that it has the following features:

(1) If am-1 = bn-1, then zk-1 = Am-1 = bn-1, and "z0, Z1 ,..., Zk-2 "is" A0, A1 ,..., Am-2 "and" B0, B1 ,..., A Longest Common subsequence of bn-2;

(2) If am-1! = Bn-1, if zk-1! = Am-1, containing "z0, Z1 ,..., Zk-1 "is" A0, A1 ,..., Am-2 "and" B0, B1 ,..., A Longest Common subsequence of bn-1;

(3) If am-1! = Bn-1, if zk-1! = Bn-1, contains "z0, Z1 ,..., Zk-1 "is" A0, A1 ,..., Am-1 "and" B0, B1 ,..., A Longest Common subsequence of bn-2.

In this way, in the search for a and B Public subsequences, if there is am-1 = bn-1, then further solve a subproblem, find "A0, A1 ,..., Am-2 "and" B0, B1 ,..., A Longest Common subsequence of bm-2; If am-1! = Bn-1, it is to solve two sub-problems, find out "A0, A1 ,..., Am-2 "and" B0, B1 ,..., Bn-1 "of a Longest Common subsequence and finding out" A0, A1 ,..., Am-1 "and" B0, B1 ,..., The longest common subsequence of bn-2, and the elders of the two are used as the longest common subsequence of A and B.

 

 

Solution:

Introduce a two-dimensional array C [] [], and use C [I] [J] to record the LCS length of X [I] AND Y [J, B [I] [J] records C [I] [J] through which the subproblem value is obtained to determine the search direction.
So before C [I, j] is calculated, C [I-1] [J-1], c [I-1] [J] and C [I] [J-1] have been calculated. In this case, we can determine whether X [I] = Y [J] Or X [I]. = Y [J] to calculate C [I] [J].

The recursive expression of the problem is as follows:


Process of backtracking output Longest Common subsequence:

 

Algorithm analysis:
Since each call moves at least one step up or to the left (or to the left at the same time), I = 0 or J = 0 will occur when you call (m + n) at most, return starts at this time. The return time is the opposite to the recursive call time. Because the number of steps is the same, the algorithm time complexity is merge (m + n ).

 

 

Code:

 

#include <stdio.h>#include <string.h>#define MAXLEN 100void LCSLength(char *x, char *y, int m, int n, int c[][MAXLEN], int b[][MAXLEN]){    int i, j;        for(i = 0; i <= m; i++)        c[i][0] = 0;    for(j = 1; j <= n; j++)        c[0][j] = 0;    for(i = 1; i<= m; i++)    {        for(j = 1; j <= n; j++)        {            if(x[i-1] == y[j-1])            {                c[i][j] = c[i-1][j-1] + 1;                b[i][j] = 0;            }            else if(c[i-1][j] >= c[i][j-1])            {                c[i][j] = c[i-1][j];                b[i][j] = 1;            }            else            {                c[i][j] = c[i][j-1];                b[i][j] = -1;            }        }    }}void PrintLCS(int b[][MAXLEN], char *x, int i, int j){    if(i == 0 || j == 0)        return;    if(b[i][j] == 0)    {        PrintLCS(b, x, i-1, j-1);        printf("%c ", x[i-1]);    }    else if(b[i][j] == 1)        PrintLCS(b, x, i-1, j);    else        PrintLCS(b, x, i, j-1);}int main(int argc, char **argv){    char x[MAXLEN] = {"ABCBDAB"};    char y[MAXLEN] = {"BDCABA"};    int b[MAXLEN][MAXLEN];    int c[MAXLEN][MAXLEN];    int m, n;        m = strlen(x);    n = strlen(y);        LCSLength(x, y, m, n, c, b);    PrintLCS(b, x, m, n);        return 0;}


Dynamic Programming solves the longest public subsequence Problem

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.