Data Structures and Algorithms abbreviated - dynamic programming theory

Dynamic Programming Theory


A model three features

  • Multi-stage decision optimal solution
  • Optimal substructure
    • Optimal substructure means that optimal solutions contain sub-optimal solution of the problem. Conversely say is that we can sub-optimal solution of the problem, the problem of the optimal solution is derived.
    • If we take the optimal substructure, corresponds to the problem of dynamic programming model we defined earlier, then we can be understood as the state behind the stage can be derived by the state of the previous stage .
  • No after-effect
    • No after-effect has two meanings, the first layer of meaning, when the state is derived later stage, we only care about the state of the value of the previous stage, do not care about how this state is deduced step by step.
    • The second meaning is that at some stage the state is established, it is not subject to influence decision-making after the stage. No after-effect is a very "loose" requirements. As long as the problem of dynamic programming model mentioned earlier, in fact, it will basically meet no after-effect.
  • Repeat sub-problems
    • This concept is better understood. The previous one, I have repeatedly mentioned. If summarize, that is, a different sequence of decisions, in one sentence when the same reaches a certain stage, may result in duplicate state .

Understand the theory with examples

  • Suppose we have a matrix W is multiplied by n of the n [n] [n]. It is a positive integer matrix storage. Pawn start position in the upper left end position in the lower right corner. We will piece from the top left to bottom-right. You can only move one to the right or down. From top left to bottom right, there are many different paths you can take. We each path through the numbers add regarded as the length of the path. That the shortest path length from the top left to bottom-right is how much?
  • A model:
    • From (0, 0) go to (n-1, n-1), leaving a total of 2 * (n-1) step, corresponds to 2 * (n-1) th stage.
    • Each stage has to go right or go down two kinds of decision-making, and each stage will correspond to a set of states.
    • We define the state of min_dist (i, j), where i represents a row, j represents a column. Min_dist expression represents the value from (0, 0) to the (i, j) of the shortest path length. Therefore, this issue is a multi-stage decision-optimal solution, in line with the model of dynamic programming.
  • Three characteristics:
    • Recursive tree painting it will find duplicate nodes from upper left to indicate the position of the corresponding node, there are a variety of routes, indicating the existence of this problem repeats problem .
    • Calculating (i, j) corresponding to the position of a state to be concerned only decision (i-1, j), (i, j-1) corresponding to the two positions of the state, after the state is determined the previous stage, a later stage will not be the change, in line with " no after-effect" .
    • The minimum path from a starting position (0, 0) to (i, j), denoted min_dist (i, j), min_dist (i, j) by min_dist (i, j-1) and min_dist (i- 1, j) derived two states, in line with " optimal substructure ."

Two Dynamic programming problem-solving ideas summary

  1. State transition table method can be roughly summarized as problem-solving ideas, backtracking algorithm - the definition of state - painting recursive tree - find duplicate sub-problems - Draw the state transition table - fill in a form based on recursive relationship - filling process will translate into code .
  2. General idea of the state transition equation method can be summarized as, to find optimal substructure - write the state transition equation - the code will be translated into the state transition equation .

 

  • State transition table method
    • First backtracking algorithm search violence to resolve, draw recursion tree to determine whether there are duplicate sub-problems, find out the law to see whether the available dynamic planning solutions.
    • If there are two solutions:
      • Use backtracking + memo resolve duplicate node
      • State transition table method :
        • To draw up a state table. State tables are generally two-dimensional, so you can imagine it as a two-dimensional array.
        • Wherein, each comprising three state variables, row, column, array values. According to the decision making process we have, from front to back, according to the recurrence relation, each state in stages filled state table. Finally, we will fill in this recursive process, translated into code that is dynamic programming code.
  • Applications: solve shortest path problem above
    • First exhaustive backtracking algorithm
    • Private  int minDist = Integer.MAX_VALUE; // global variables or field
       // Called: minDistBacktracing (0, 0, 0, W, n-); 
      public  void minDistBT ( int I, int J, int dist, int [] [ ] W, int n-) {
         // reaches n-1, n-1 in this position, here looking strange Ha, you look example 
        IF (J == I == && n- n-) {
           IF (dist <minDist) minDist = dist;
           return ; 
        } 
        IF (I <n-) { // go down,. 1 + I = update I, J = J 
          minDistBT (. 1 + I, J, dist + W [I] [J ], W, n-); 
        }
        IF (J <n-) { // go to the right, update I = I, J + = J. 1 
          minDistBT (I, J +. 1, dist + W [I] [J], W, n-); 
        } 
      }

       

    • Draw recursive tree as a way to find duplicate sub-problems, (i, j) repeating node, we only need to select the smallest node dist, continue recursive solution, the other nodes can be discarded
    •  

      Draw a two-dimensional state table 

    •  

      public int minDistDP(int[][] matrix, int n) {
        int[][] states = new int[n][n];
        int sum = 0;
        for (int j = 0; j < n; ++j) { // 初始化states的第一行数据
          sum += matrix[0][j];
          states[0][j] = sum;
        }
        sum = 0;
        for (int i = 0; i < n; ++i) { // 初始化states的第一列数据
          sum += matrix[i][0];
          states[i][0] = sum;
        }
        for (int i = 1; i < n; ++i) {
          for (int j = 1; j < n; ++j) {
            states[i][j] = 
                  matrix[i][j] + Math.min(states[i][j-1], states[i-1][j]);
          }
        }
        return states[n-1][n-1];
      }
  • 状态转移方程法

    • 类似递归的解题思路。我们需要分析,某个问题如何通过子问题来递归求解,也就是所谓的最优子结构。

    • 根据最优子结构,写出递归公式,也就是所谓的状态转移方程。有了状态转移方程,代码实现就非常简单了。

    • 一般情况下,我们有两种代码实现方法,一种是递归加“备忘录”,另一种是迭代递推。

    • 强调:状态转移方程是解决动态规划的关键,写出状态转移方程,问题就解决了一半!!

    • 应用:解决上面的最短路径问题

      • 状态转移方程:

        min_dist(i, j) = w[i][j] + min(min_dist(i, j-1), min_dist(i-1, j))
      • 实现代码

      • private int[][] matrix = 
                 {{1,3,5,9}, {2,1,3,4},{5,2,6,7},{6,8,4,3}};
        private int n = 4;
        private int[][] mem = new int[4][4];
        public int minDist(int i, int j) { // 调用minDist(n-1, n-1);
          if (i == 0 && j == 0) return matrix[0][0];
          if (mem[i][j] > 0) return mem[i][j];
          int minLeft = Integer.MAX_VALUE;
          if (j-1 >= 0) {
            minLeft = minDist(i, j-1);
          }
          int minUp = Integer.MAX_VALUE;
          if (i-1 >= 0) {
            minUp = minDist(i-1, j);
          }
          
          int currMinDist = matrix[i][j] + Math.min(minLeft, minUp);
          mem[i][j] = currMinDist;
          return currMinDist;
        }

         

 

四种算法思想比较分析

  • 分类
    • 贪心、回溯、动态规划:解决多阶段决策最优解模型。
    • 分治:解决的问题尽管大部分也是最优解问题,但是,大部分都不能抽象成多阶段决策模型
  • 回溯算法:
    • 相当于穷举搜索,“万金油”,基本上能用的动态规划、贪心解决的问题,我们都可以用回溯算法解决。
    • 时间复杂度高:指数级
  • 动态规划:
    • 动态规划比回溯算法高效。
    • 需要满足一个模型三个特征
    • 动态规划和分治算法在重复子问题上区分非常明显。分治算法要求分割成的子问题,不能有重复子问题,而动态规划正好相反,动态规划之所以高效,就是因为回溯算法实现中存在大量的重复子问题。
  • 贪心算法
    • 动态规划算法的一种特殊情况。
    • 更加高效,代码实现也更加简洁。
    • 可以解决的问题也更加有限。它能解决的问题需要满足三个条件,最优子结构、无后效性和贪心选择性(这里我们不怎么强调重复子问题)。
    • 贪心选择性”的意思是,通过局部最优的选择,能产生全局的最优选择。

 

 

 

Guess you like

Origin www.cnblogs.com/wod-Y/p/12146673.html