Dynamic programming problem-solving ideas, the difference with divide and conquer; problem example (1): steel bar cutting

1. The difference between dynamic programming and divide-and-conquer method:
both use recursion, but the sub-problems of divide-and-conquer method are not repeated, and the sub-problems of dynamic planning are repeated, soNeed a table/array/matrix to save the solution of the sub-problemTo avoid recalculation.
Insert picture description here
2. Dynamic programming problem-solving ideas: the
most important thing is to find the state transition equation.
Insert picture description here

  • The characteristics of the problem that can be solved by this method:
    dynamic programming is used to solve the optimization problem. When "···makes···the best and optimal" appears in the problem description, you can think about using dynamic programming to solve the problem.
  • DP bottom-up solution method:
    DP decomposes the big problem into sub-problems, first solve the sub-problems, and then gradually expand. (From small problems to big problems, often use the for loop to traverse).
  • Need to create a table to save the solution of the sub-problem: the solution of the
    big problem is often used to the solution of the small problem, so == table/array/matrix to save the solution of the sub-problem ==.
  • State transition equation:
    ans ( tbig problem at the current moment) = ans ( t-1small problem at the previous step ) + current problem processing.
    Find the correlation between the big problem and the small problem, you can gradually advance to get the solution of the final problem.

3. Introduction to dynamic programming method:
dynamic programming isPay extra memory space to save time, A typical space-time balance.
3.1. Dynamic programming algorithm design steps:
Insert picture description here
optimal substructure: the optimal solution of the problem is composed of the optimal solutions of related subproblems, and these subproblems can be solved independently.

3.2. Implementation method of dynamic programming algorithm:
a) Top-down method with memo:
Write according to the normal recursive process, butSave the solution of each sub-problem in the process. When solving the sub-problem, first find out whether the solution has been saved, if yes, then return, otherwise save and return after calculation.
Pseudo code for steel bar cutting problem:
Insert picture description here
Insert picture description here

b) Bottom-up method (commonly used):
Solve the solution of the smallest-scale sub-problem first, and then gradually expand the problem, When it is expanded to the problem to be solved, the prerequisite sub-problems are all solved (sort the sub-problems according to scale, and solve them from small to large).
Since there is no frequent recursive call overhead,Bottom-up approachThe time complexity of has a smaller coefficient.
Pseudo-code for the steel bar cutting problem:
Insert picture description here
two layers of loops: one layer of loop is used to traverse small problems -> big problems; the second layer is used to solve the small problems.

c) Complete code: Return the value r of the optimal solution and the optimal solution s:
Insert picture description here
Insert picture description here
Reference materials: Introduction to Algorithms, page 359, Chapter 15 Dynamic Programming

to sum up:

1. In dynamic programming, space is exchanged for time, and an array needs to be opened to store the solution of the sub-problem to avoid repeated calculation of the sub-problem.
2. Dynamic programming is used to solve optimization problems, and the bottom-up method is commonly used (first solve the sub-problems, and then gradually expand. From small problems to large problems, often use for loop traversal).

3. Finding the state transition equation is the most important

Guess you like

Origin blog.csdn.net/qq_33726635/article/details/105915484