We often encounter dynamic programming-type problems in our work and life. Dynamic programming problems are very, very classical and skillful. Generally, large companies and corporations like this algorithm very much because it reduces a lot of repetitive calculations and thus saves a lot of time for manual labor. Today’s article from algo.monster will share with you the most comprehensive introduction to dynamic programming.

• The core idea of dynamic programming
• An example of dynamic programming
• The solution to the problem of dynamic programming

[2] What is dynamic programming?

Dynamic programming (DP) is a method used in mathematics, management science, computer science, economics, and bioinformatics to solve complex problems by decomposing the original problem into relatively simple subproblems. It is often applied to issues with overlapping subproblems and optimal substructure properties.

We split a problem into subproblems until we can solve the subproblems directly. Then, we save the subproblem answers to reduce double computation. Then, the subproblem answers are dedicated to reducing the duplication of mathematics, and the solution to the original problem is derived by backpropagation from the subproblem answers.

Usually, these subproblems are similar enough to be recursively derived from functional relations. Then dynamic programming is dedicated to solving each subproblem once to reduce repeated calculations, such as the Fibonacci sequence can be seen as an entry-level classical dynamic programming problem.

The core idea of dynamic programming
The core idea of dynamic programming lies in splitting the numerator problem, remembering the past, and reducing repeated calculations.

Let’s look at one famous example.

A : “1+1+1+1+1+1+1+1+1+1 = ?” What is the value of the equation?
B: It’s “8”.
A: What about writing “1+” to the left of the above equation? What is the value of the equation at this point.”
B: The answer is quick “9.”
A: How did you know the answer so quickly?
B: Add 1 to 8.
A: You don’t have to recalculate again and again because you remembered the value of the first equation is 8!

The dynamic programming algorithm can also be described as remembering the solved solution to save time.

[2] The frogs jumping problem

There is an example of taking you into dynamic programming.

A frog can jump up 1 step or two steps at a time. Find the total number of ways the frog can jump up a 10-step staircase.
Some of you may be a little confused when you first see this problem and don’t know how to solve it. Try to imagine.
To jump the 10th step, either jump to the 9th step and then jump 1 step up, or jump to the 8th step and then take two steps up at a time.
Similarly, to jump to the 9th step, either jump to the 8th step and then jump 1 step up or jump to the 7th step and then take two steps up at a time.
To jump to the 8th step, either jump to the 7th step and then jump 1 step up, or jump to the 6th step and then jump two steps up at a time.

[3] Using dynamic programming

You can also use dynamic programming to solve this problem.

[4] Bottom-up dynamic programming

The basic idea of dynamic programming and recursive solution with memoization is the same, both are to reduce repeated calculations, and the time complexity is also similar.

• Recursion with memoization extends the solution from f(10) to f(1), also called a top-down solution.
• Dynamic programming starts from the solution of the minor problem, by the intersection property, and gradually decides the solution of the more significant problem, it is from f(1) to f(10) direction, pushing up the solution, so it is called the bottom-up solution method.
The frog jumping problem has several typical dynamic programming features, optimal substructures, state transfer equations, bounds, and overlapping subproblems.
• f(n-1) and f(n-2) are called the optimal substructures of f(n)
• f(n) = f(n-1) + f(n-2) is then called the state transfer equation
• f(1) = 1, f(2) = 2 is the boundary
• For example, f(10) = f(9) + f(8), f(9) = f(8) + f(7), f(8) is the overlapping subproblem.

[2] Dynamic programming solution routines

When to use dynamic programming?

If we can solve a problem by exhausting all possible answers, and after finishing them, it is found that there is an overlapping subproblem, you can consider using dynamic programming.

For example, some scenarios of finding the most value, such as the longest increasing subsequence, the minimum edit distance, the backpack problem, the pooling of change problem, etc., are all classic applications.

[3] The solution

The core idea is to split the numerator problem, remember the past, and reduce repeated calculations. And dynamic programming is generally bottom-up, so based on the frog jumping problem, I summarized my thoughts:

• Exhaustive analysis
• Determine the boundary
• Find the discipline and determine the optimal substructure
• Write the state transfer equation

[4] exhaustive analysis

• When the number of steps is 1, there is one way to jump, f (1) = 1
• When there are only two steps, there are two ways to jump, the first one is to jump two steps directly, and the second one is to jump one step first and then one step later. That is, f(2) = 2;
• When the steps are 3, if you want to jump to the 3rd step, you can jump to the 2nd step and then jump 1 step up, or you jump to the 1st step and then jump two steps up at once. So f(3) = f(2) + f(1) = 3
• When the steps are 4, if you want to jump to the 3rd step, you either have to jump to the 3rd step and then jump 1 step up or jump to the 2nd step and then take two steps at a time. So f(4) = f(3) + f(2) = 5
• When the steps are 5, the solution is the same.

[4] Determining the boundary

• By exhaustive analysis, we find that when the number of steps is 1 or 2, we can clearly know the frog jumping method. f(1) = 1, f(2) = 2, when the steps n>=3, it has shown the law f(3) = f(2) + f(1) = 3, so f(1) = 1, f(2) = 2 is the boundary of the frog jumping steps.
[4] Find the law and determine the optimal substructure
• When n >= 3, the law f(n) = f(n-1) + f(n-2) has been shown, so f(n-1) and f(n-2) are called the optimal substructure of f(n). What is the optimal substructure? There is this explanation.
• A dynamic programming problem is a recursive problem. Assuming that the current decision is f(n), the optimal substructure makes f(n-k) optimal. The optimal substructure is a property that allows the state transferred to n to be optimal and has no relationship with the later decisions, i.e., to allow the last choices to use the previous local optimal solution in peace of mind.

[4] Write the state transfer equation

We can develop the state transfer equation through the first three steps, exhaustive analysis, and determine the boundary and the optimal substructure.