Detailed and dynamic programming algorithm classic example

Dynamic Programming

What is dynamic programming?

General idea of ​​dynamic programming is to a complex problem into a recursive process of a phased, step by step from simple recursive initial state, finally get the optimal solution to complex problems.

The basic idea of ​​the Policy Editor:

Since dynamic programming to solve the problem of overlapping sub-problems most of this feature, in order to reduce double counting, for each sub-problem solution only once, to different states at different stages of preservation in a two-dimensional array.

  1. Split Problem: The possibility of the problem into a step by step to achieve the recursive or recursive.

The key is this step, there is a kind of dynamic programming problem is to push from the back, sometimes it's easy to know:  If only one case, the best option should be how do then step forward deduced based on the best option the best choice to get the previous step

  2. The  relationship between the state and the definition of the state: in the form of a quantitative performance out of derivation formula similar to high school, because this formula is easy to write a program, it can be said relatively affinity program ( is the last call state transition equation)

  3.  Similar to the basic idea of divide and conquer dynamic programming algorithm to solve the problem is to be broken down into several sub-problems (stage), in order to solve the sub-stage,the solution of the problem before a child, to provide for the child to solve the latter problem useful information. Any child in solving problems, listed a variety of possible local solution, it is possible to keep those decisions reached by the best local solution, discard other local solution. In order to solve each sub-problem, and finally a solution of the initial problem is a problem child.

My understanding is: For example, we find the optimal solution, we should say the best preserved solution, in order to derive the previous step forward when the optimal solution can be used in the process will inevitably be compared to some of the best Jiechai solution, then we should give up, save only the optimal solution ,

Every time we regard the optimal solution preserved, greatly reduces the time complexity.

 

Dynamic planning process to solve the problem in two steps:

1. Looking for a state transition equation

2. The use of a state transition equation to solve the problem from the bottom up

 

Dynamic programming principle

Conditions: can be divided into a plurality of sub-problems related to the sub-problem solution is reused

  • Conditions: can be divided into a plurality of sub-problems related to the sub-problem solution is reused
  • Optimal substructure (sub-optimized structure):
    • The optimal solution of a problem contains sub-optimal solution of the problem
    • Narrow sub-set of questions, sub-problems need only contain those optimization problem, reduce the complexity of
    • We can bottom
  • Subteties (overlapping subproblems): In the course of solving the problems, many sub-problem solutions will be used multiple times.
  • Dynamic programming algorithm design steps:
    • Analysis of structural optimization solution
    • Recursively define the cost of the optimal solution
    • Calculation upwardly from the bottom to save the cost of the optimal solution, optimal solution and obtain information configuration
    • The optimal solution based on the information structure optimal solution structure
  • Dynamic Programming features:
    • The original problem is divided into a series of sub-questions;
    • Solving each sub-problem only once, and the result is stored in a table, when used after the direct access, without double counting, to save computation time
    • Calculated from the bottom up.
    • Overall optimal solution depends on sub-optimal solutions (state transition equation) (referred to as sub-state issues, and ultimately solve the state attributed to the solution of other states)

 

Dynamic Programming longest common subsequence achieve

Longest common subsequence (longest-common-subsequence, LCS) 

   (1) sequence: a sequence X = x1x2 ... xn, any of several deleted, the remaining sequence is called a sub-sequence A. It can be considered to retain the original sequence A sequence from any sequence of several obtained.
      For example: for a sequence 1,3,5,4,2,6,8,7, 3,4,8,7 its sequence a sequence. For a sequence of length n, which a total of 2 ^ n subsequences with (2 ^ n - 1) non-empty sequence. Here need to remind you, is not a subset of the sequence, the order of elements and it is related to the original sequence.

   (2) Public sequence: If the sequence Z promoter sequence both X and Y sequences but also sequences, sequence X and sequence Y is called common sub-sequences. Empty sequence is common subsequence any two sequences.

   (3) the longest common subsequence: common subsequence of X and Y and X longest length the longest common subsequence in Y (containing up element) is called.

      When this problem if the method of exhaustion time to finally determine the longest common subsequence, time complexity is Ο (2mn), is a complex index level for the long sequence it is not applicable. So we use dynamic programming method to solve.

Describe the longest common subsequence problem of optimal substructure 

      Provided X = X- . 1 X 2 ... X m and Y = the Y . 1 Y 2 ... Y n- two sequences, the Z = Z . 1 Z 2 ... Z K is a longest common subsequence of the two sequences.

      1. If X m = Y n- , then Z K = X m = Y n- , and the Z K. 1- a X- m-. 1 , the Y n-. 1- a longest common subsequence;

      2. If X m ≠ Y n- , then Z K ≠ X m , Z means is X- m-. 1 , a longest common subsequence in Y;

      3. If X m ≠ Y n- , then Z K ≠ Y n- , meaning Z is X-, the Y n-. 1- a longest common subsequence.

      As can be seen from the above three cases, two sequences LCS LCS prefix comprises two sequences. Therefore, LCS issues sub-optimal structural characteristics.

 

Recursive definition of the optimal value 

    As can be seen from the optimal substructure, if X m = Y n- , then we should solve X- m-. 1 , the Y n-. 1- a of the LCS, and X m = Y n- added to the end of the LCS, thus obtained LCS is a new ask.

    If xm ≠ yn, we need to solve two sub-problems, each seeking X- m-. 1 , an LCS and the Y X-, the Y n-. 1- a of the LCS. The longer of the two LCS LCS is a X-and Y.

    LCS can be seen that the problem with the nature of overlapping subproblems. To find the X and the Y a LCS, we need to find each X m-. 1 , the Y's and a LCS X, the Y n-. 1- a LCS, which in turn contains the words problems obtaining X m-. 1 , the Y n-1 sub-sub-problems of a LCS. (A little halo around ... no halo ....)

    According to the above analysis, we can draw the following equation;

 

 

Calculating the value of the optimal solution

       According to the above, we can easily calculate the LCS problem to write recursive procedures, through this process we can find the value of each sub LCS problem, moreover, in order to solve the optimal solution itself, we need a good table dp, dp [i, j] so that the recording C [i, j] value of optimal substructure.

 

The process of filling the array

Code:

package cn.itcast.recursion;

public  class the LCS {
     public  int findLCS (A String, String B) {
         int n-= A.length ();
         int m = B.length ();
         // Returns an array of characters, the character array stored in the current string all the characters
         // return character array char [] a 
        char [] = a A.toCharArray ();
         char [] B = B.toCharArray ();
         // create a two-dimensional matrix, it is used to push the common sub sequence 
        int [] [] DP = new new  int [n-] [m];
         for ( int I = 0; I <n-; I ++ ) {
             // if one of the characters found in the first column of the first row is equal to a character 
            if(A [I] == B [0 ]) {
                 // find a value equal to the first row and the first column of b [0], and put it into. 1 
                DP [I] [0] =. 1 ;
                 // and the characters have become behind. 1 
                for ( int J = I +. 1; J <n-; J ++ ) {
                    dp[j][0] = 1;
                }
                break;
            }
        }

        for ( int I = 0; I <m; I ++ ) {
             // if one of the characters found in the first column of the first line of the first character is equal to 
            IF (B [I] == A [0 ]) {
                 // put the first column behind the characters have become. 1 
                DP [0] [I] =. 1 ;
                 for ( int J = I +. 1; J <m; J ++ ) {
                    dp[0][j] = 1;
                }
                break;
            }
        }
        // starting from 1 because of transverse and longitudinal subscript are traversed 0 over 
        for ( int I = 1; I <n-; I ++ ) {
             for ( int J = 1; J <m; J ++ ) {
                 // transverse have equal values and the longitudinal 
                IF (a [I] == B [J]) {
                     // current position value + left 
                    dp [i] [j] = dp [i - 1] [j - 1] + 1 ;
                } The else {
                     // get the current position (left value, the value of the upper side) of the maximum value 
                    DP [I] [J] = Math.max (DP [I] [J -. 1], DP [I -. 1 ] [J ]);
                }
            }
        }

        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(dp[i][j] + "  ");
            }
            System.out.println();
        }
        return dp[n - 1][m - 1];
    }

    public static void main(String[] args) {
        LCS lcs = new LCS();
        int findLCS = lcs.findLCS("android", "random");
        System.out.println ( "longest sequence length:" + findLCS);
    }
}

Guess you like

Origin www.cnblogs.com/xiaozhongfeixiang/p/12026148.html