Algorithm learning--Dynamic programming 1

Source: Internet
Author: User

As we all know, the most difficult in the interview, but also the most easy to test is the dynamic planning, so today intends to open tear dynamic planning, before just for a single problem to understand the solution, but a long time to forget, today began to thoroughly understand the methodology to practice, I hope God bless can be done a week!

I. Learn the methodology through a small example:

An example, such as:

To find the shortest path from the starting point to the end point:

This is one of the most basic dynamic planning problems, if you do not use dynamic planning can be used to solve the problem, the path is about 2 k-th square. Use dynamic programming to solve the main steps:

1. Find out the initial state of the plan: for this, find out the solution of the first sub-problem so that the solution can be used in the later content as the condition of the later state, in which the initial state is the shortest distance from the C point to the end point of the most neighboring focus.

2. Planning the decision-making route, the decision-making route for the problem is to move the boundary from the starting point to the end, as in. Some problems are moving from the starting state to the final state, knowing that the final

State.

2. Transformation equation: For the dynamic programming of its solution is, sub-sub-problem----the problem, the use of the previous state to solve the next state, find out the relationship between before and after the final solution. For example, the relationship in this topic is very simple, which is to use the solution of the previous sub-problem plus the shortest route from the current node to the next.

3. Convergence condition: Find out where the final state ends

4. Applicable optimization principle: Any subsequence of an optimal decision sequence must itself be the optimal decision sequence relative to the initial and end states of the subsequence itself.

Two. Design elements for dynamic planning

In the first part, I wrote some personal views on solving dynamic programming, and the following are some examples of instructional videos summarizing and enumerating the basic steps of dynamic planning:

1. Problem modeling, what is the objective function of optimization? What are the constraints?

2. Dividing sub-issues (boundaries)

3. Dependency of sub-problem and parent problem (recursive equation)

4. Satisfy the optimization and determine whether the optimization principle is satisfied

5. Minimum sub-structure, what is the minimum function value, and what is the initial values?

The above five points can be summed up as a dynamic planning methodology, in the future, not all problems in the brush problem can be based on the above five textbook copy, but the system analysis of dynamic programming problems, the above five is for the analysis of dynamic planning problems are very important steps, especially for beginners. For example, using a matrix chain multiplication case, the dynamic programming method is used to solve this problem.

1. Problem modeling, what is the objective function of optimization? What are the constraints?

The objective function of this problem is to find out the matrix multiplication to find the smallest result, namely:

min = P[0,i]*p[i+1,n] to get the minimum value.

2. Dividing sub-issues (boundaries)

Sub-problem boundary, first the initial division of the boundary of the I value from 0-n all over again, find the minimum value multiplied, at this time I is divided into the sub-interval, and then for [0,i] and [i+1,n] continue to follow the previous traversal, and so on, and so on, until not divided. The local minimum value is found to obtain the optimal solution.

3. Dependency of sub-problem and parent problem (recursive equation)

Recursive equations, such as:

The most important thing in the so-called recursive equation is the progression of one layer to the final result, traversing all the elements in each layer, and finding the optimal solution for the sub-problem and applying it in the next layer. Until the final state is reached, this is called memory programming.

4. Satisfy the optimization and determine whether the optimization principle is satisfied

Sub-problem optimization, the whole problem will also be optimized, the solution of the sub-problem is also the optimal solution of the sub-process.

5. Minimum sub-structure, what is the minimum function value, and what is the initial values?

The smallest sub-structure is the first time for the entire input array to be traversed to find out, separated by the smallest point I, which is the initial value.

Three. Recursive implementation of dynamic programming:

1. Partial pseudo-code

The above is the pseudo-code for recursive implementation of the matrix chain multiplication of the previous problem:

1. Use m to record the final result size Q

2.s to record the location of the Division K

3-7. Traverse I to K and k+1 to J and calculate by recursive call, and constantly refresh values of K and q

8. Return to final result

2. About complexity:

3. The process of dividing the sub-problem:

The main limitation of recursive implementation is that the same sub-problem will be implemented multiple times, so its time complexity will be high, to solve this problem can be achieved by recording the results of sub-problems, the following dynamic programming iterative implementation.

Four. Iterative implementation of dynamic planning:

The iterative implementation of dynamic programming is the most common way of solving the problem, and the time complexity of iteration is much smaller than the recursive method, but a data structure is required as a memo to record the results of each sub-problem, which avoids repeated computations:

1. Key to iterative computing:

1. Each sub-problem is counted only once

2. From the smallest sub-problem, consider the order of calculation, to ensure that the results of the previous calculation can be used.

3. Using data structures as memos for storage

4. Set the marker function, mark the solution of each step with the marker function, finally get the final solution.

2. The Division of sub-problems can be extended recursively to extend its length:

, the sub-problem length from 2 to 8, for each length of the division can list all possible solutions

There are 7 kinds of r=2

There are 6 kinds of r=3

There are 5 kinds of r=4

There are 4 r=5

And so on, you can find the minimum value each time you delimit r

3. Related pseudo-code

1. Initialize the original sub-structure

2. Traverse R possible chain lengths from 2 to n

3-4. When the chain length is fixed, the distance between the left and right boundaries I and J,i and J is always R

5. The current result is derived from the previous sub-result in the memo, the initial result of a division

6. Record Division location K

7. Divide by point K and then between I and J

8-11. Find the optimal sub-division of the position K and value T, the eighth Behavior state transfer equation

4. Complexity of Time:

5. Memo m, Mark table n

Five. Summary

Today seriously systematic study of dynamic planning, found that the previous study and brush problems are like dragonfly water, wasted a lot of time, and no avail. At present, the distance fully understand the dynamic planning still has a long distance to learn, but the first part of the open system learning, really important, the following is a few of my teaching video on the summary:

1. The most important thing is the division of the dynamic planning state, what the final state is, from what state can reach the final state, and from the initial state how to achieve that state. In other words, it is the division of sub-problems, the problem solving into multi-step operation or multi-step judgment process.

2. Determine whether the problem can use dynamic programming, with minimum or maximum values as a judgment

3. The key to solving the problem is to define the transformation equation, for example, in the smallest matrix chain, a question result can be selected in several sub-options of a minimum scheme, plus the current value in the memo

4. Using data structures to build memos, common arrays, matrices, and sometimes the existence of a hash table

5. It is sometimes necessary to establish a state transition matrix, as it may be more than just numeric values, but there are also states that correspond to values, such as indexes and other attributes that need to be recorded.

Very long,,,, the first time to write so seriously, over a period of time to review hope may understand, if a year ago began to contact algorithm problem can do so, I may heart in also have work,,,, heart plug

Algorithm learning--Dynamic programming 1

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.