Experiment 4 dynamic planning algorithm-Longest Common subsequence Problem

Source: Internet
Author: User

Dynamic Planning Algorithm for Solving LCS Problems

1 Structure of the longest common subsequence

The structure of the longest common subsequence is as follows:

Set sequence X = <x1, x2 ,..., Xm> and Y = <y1, y2 ,..., One of the longest common subsequences of yn> Z = <z1, z2 ,..., Zk>, then:

1> If xm = yn, zk = xm = yn, and Zk-1 is the longest common subsequence of Xm-1 and Yn-1;

2> If xm =yn and zk =xm, Z is the longest common subsequence of Xm-1 and Y;

3> If xm =yn and zk =yn, Z is the longest common subsequence of X and Yn-1;

The Xm-1 = <x1, x2 ,..., Xm-1>, Yn-1 = <y1, y2 ,..., Yn-1>, Zk-1 = <z1, z2 ,..., Zk-1>.

2. recursive structure of subproblems

According to the optimal sub-structure nature of the longest common subsequence problem, we need to find Xm = <x1, x2 ,..., Xm> and Yn = <y1, y2 ,..., The longest common subsequence of yn> can be recursive as follows:

· When xm = yn, find the longest common subsequence of the Xm-1 and the Yn-1, and add xm or yn to its tail to obtain the longest common subsequence of X and Y;

· When xm =yn, two subproblems must be solved: finding one of the longest common subsequences of Xm-1 and Y and one of the longest common subsequences of X and Yn-1. The elders of the two common subsequences are the longest common subsequences of X and Y.

From this recursive structure, it is easy to see that the longest common subsequence problem has subproblem overlapping nature. For example, when calculating the longest common subsequences of X and Y, the longest common subsequences of X and Yn-1 and Xm-1 and Y may be calculated. Both of these subproblems contain a common subproblem, that is, the longest common subsequence for calculating Xm-1 and Yn-1.

Similar to the optimal order of matrix product calculation, we establish a recursive relationship between the Optimal Values of subproblems. Use c [I, j] to record the length of the longest common subsequences of sequence Xi and Yj, where Xi = <x1, x2 ,..., Xi>, Yj = <y1, y2 ,..., Yj>. When I = 0 or j = 0, the null sequence is the longest common subsequence of Xi and Yj, So c [I, j] = 0. In other cases, the recursive relationship is as follows:

3. Calculate the optimal value.

Using the recursive formula at the end of the last section, we can easily write a recursive algorithm for computing c [I, j], but its computing time increases with the input length index. In the subproblem space, there are only O (m * n) Different subproblems. Therefore, using the dynamic planning algorithm to calculate the optimal value from the bottom up can improve the efficiency of the algorithm.

The Dynamic Programming Algorithm LCS_Length (X, Y) used to calculate the longest common subsequence length ,..., Xm> and Y = <y1, y2 ,..., Yn> as input. Output two arrays c [0 .. m, 0 .. n] and B [1 .. m, 1 .. n]. C [I, j] stores the length of the longest common subsequence of Xi and Yj, and B [I, j] records indicate c [I, j] The value is obtained by the subproblem, which is used to construct the longest common subsequence. Finally, the length of the longest common subsequences of X and Y is recorded in c [m, n.

   1:  #include<iostream>
   2:  #include<stdlib.h>
   3:  #include<string.h> 
   4:  using namespace std;
   5:  const int strlens=15;
   6:  int b[strlens][strlens],c[strlens][strlens];
   7:  void LCSLength(string x,string y){
   8:      int m=x.length(),n=y.length();
   9:      for(int i=1;i<=m;i++)
  10:          for(int j=1;j<=n;j++)
  11:          {
  12:              if(x[i]==y[j])
  13:              {
  14:                  c[i][j]=c[i-1][j-1]+1;
  15:                  b[i][j]=1; 
  16:              }
  17:              else if(c[i-1][j]>=c[i][j-1])
  18:              {
  19:                  c[i][j]=c[i-1][j];
  20:                  b[i][j]=2;         
  21:              }
  22:              else
  23:              {
  24:                  c[i][j]=c[i][j-1];
  25:                  b[i][j]=3;
  26:              }         
  27:          }
  28:  } 
  29:  void LCS(int i,int j,string x){
  30:      if(i==0||j==0) return;
  31:      if(b[i][j]==1)
  32:      {
  33:          LCS(i-1,j-1,x);
  34:          printf("%c",x[i]);
  35:      }
  36:      else if(b[i][j]==2)
  37:          LCS(i-1,j,x);
  38:      else 
  39:          LCS(i,j-1,x);
  40:  } 
  41:  int main(){
  42:      string x="AFJGGFJGEJVODF",y="FFOEJFOEJFODJF";
  43:      memset(c,0,sizeof(c));
  44:      memset(b,0,sizeof(b));
  45:      LCSLength(x,y);
  46:      LCS(14,14,x);
  47:      cout<<endl;
  48:  }

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.