Go: Dynamic planning

Source: Internet
Author: User

Finally came to the most interesting part of the algorithm design idea, in last year's Google written test, 7 algorithm design questions have 2 dynamic programming (programming).

Look at the algorithm for so long, this part is the only feeling of the more difficult place,

Starting with this article, we will take a continuous length of time to discuss some of the understanding of dynamic planning and its problems. This includes some examples: The calculation of the two-polynomial coefficients, the warshall algorithm for the transitive closure, the Floyd algorithm to find the complete shortest path, the construction of the most

There are two forks to find the tree, knapsack problem and memory function. Also includes some other problem solving reports (dynamic programming is really hard, for the content of this chapter, I will search for some other types of questions to write problem-solving reports to Real

Understanding dynamic planning), such as matrix multiplication, the longest common child column, and so on.

--------------------------------------------------------------------------------------------------------------- -----------------------------------


1, what is Dynamic planning (DP)?


Very IMPORTANT! Don't think the concept is not important, understand profoundly, you just knowWhat kind of questions to consider is there a method for dynamic planning, and how to use dynamic programming.


1) dynamic programming is used in operational research to seekThe optimal mathematical method in the process of solution decision。 Of course, what we're focusing on here is as an algorithmic design technique, as a way to useA general method for optimal multi-stage decision-making process.

It is used in applied mathematics toAn important tool for solving certain types of optimization problems.


2) If the problem is caused byOverlapping sub-problems, we can use dynamic programming techniques to solve it, in general, such sub-problems appear in the solution of a given problemIn a recursive relationship,This recursive relationship contains the phase

The solution to the problem of the more small boys。 The dynamic programming method suggests that instead of solving overlapping problems once and again, it is better to solve each smaller sub-problem only once and record the result in the table (dynamic planning is also the space-changing time

) so that the solution to the original problem can be obtained from the table.


Keywords:

It is often the solution to the optimization problem

Problems can be expressed as multi-stage decisions (go online to find out what multi-stage decisions are!) )

Overlap Sub-Problem: What is overlapping sub-problem, the most sub-structural properties.

What is the idea of dynamic programming: memory, space-changing time, non-repetition solution, and the solution of the larger problem is solved by the overlapping sub-problem from the smaller problem.


--------------------------------------------------------------------------------------------------------------- ----------------------------------


The Fibonacci sequence can be used as a simple example to explain the idea of dynamic programming, as mentioned in the previous Fibonacci sequence, no longer described.


In general, a classic dynamic programming algorithm when the bottom-up (from the solution of the smaller problem, by the overlapping nature, the solution of the larger problem of the gradual decision-making), it needs to solve the given problem of all the smaller sub-problems. Dynamic planning of

A variant is an attempt to avoid solving unnecessary sub-problems. If a top-down recursive solution is used, the solution to unnecessary sub-problems is avoided (as opposed to dynamic programming), but recursion leads to

The same sub-problem is solved multiple times (as opposed to dynamic programming), so combining recursion and dynamic programming, you can design a


----------------------------------------------------------------------------------------------- -------------------------------------------------


calculates two-item coefficients:

in the permutation group, we have the following formula (which is easy to prove with the definition of a combination): The



Formula expression in order to ( question description) C (n-1, K- 1) and C (n-1, K) two small overlap problem.

initial conditions: C (n, N) = C (n, 0) = 1

We can use the following


What is the time complexity of the algorithm? It can be estimated that only the following triangular matrix is filled, for n*k/2  =  n*k, the number of times is:



How to fill in the Matrix ( fill in the order of the matrix)?

fills the matrix by line: The algorithm pseudo-code:



The 1th for is the control line, to be filled with the nth row. The 2nd for to control where each line is filled, the smaller values to I and K. From these 2 for you can also see that the complexity is n*k.

Implementation:

PackageSection8;


/*The eighth chapter dynamic programming calculates two coefficients*/

PublicClassBinocoeff {

/**
*@paramArgs
*/
PublicStaticvoidMain (string[] args) {
//TODO auto-generated Method Stub
IntResult=Binomial (8,3);
System.out.println ("Two-polynomial coefficients for output 8:");
For(IntI=0; I<=8; I++)
System.out.println ("C"+"("+8+","+I+")"+"————"+Binomial (8, i));
}

PublicStaticIntBinomial (IntNIntK) {
//Calculation of two-term coefficient C (n,k)
Int[[] Result=NewInt[n+1][n+1];
For(IntI=0; I<=N;i++)//Fill in the Matrix by line
{
For(IntJ=0; j<=Min (i,k); j++)//Min (i,k) is the number of columns to fill in this line
{
//if (j = = 0 | | j = = k)//It's wrong in the book, Motherfucker.
If(j==0||J==I
RESULT[I][J]=1;
Else
RESULT[I][J]=Result[i-1][j-1]+ result[i -1][J];
}
}

return Result[n][k];
}

privatestatic int min (int I,int K) {
if (i < K)
return< Span style= "color: #000000;" > I;
return K;
}

} /span>



Results:

Two-polynomial coefficients for output 8:
C (8,0) ———— 1
C (8,1) ———— 8
C (8,2) ———— 28
C (8,3) ———— 56
C (8,4) ———— 70
C (8,5) ———— 56
C (8,6) ———— 28
C (8,7) ———— 8
C (8,8) ———— 1

You can actually return the entire matrix so that you can calculate all C (n, K) at once.



--------------------------------------------------------------------------------------------------------------- -----------------------------------


See Dynamic planning again:

The above brown font is marked by a few of the dynamic programming algorithmsKey points:

1) HowDescribe the problem, to describe the problem as overlapping sub-problem

2) Overlapping sub-problemsInitial conditions(Boundary conditions)

3) Dynamic planning often manifests itself in the form ofFill MatrixIn the form (see later, some canOptimization of spatial complexity, open an array,Optimization is also based on a recursive form of dependency, followed by an article detailing)

4) What does the form (or order) of the matrix indicate? -it shows the process of this dynamic planning from small to large, and the professional point is thatThe recursive form of dependency determines the order of filled matrices




--------------------------------------------------------------------------------------------------------------- ------------------------------------




Exercise 8.1 Decides that the exercises in this chapter are done in earnest.






1

A, the same point is that the dynamic programming and division method are divided for the solution of the smaller scale problem

b, the difference is that the smaller sub-problems of dynamic programming are overlapping, and the solution of storing smaller sub-problems


2

A, see code, implemented

B,You can also fill in the Matrix by column (think about why?) )---In fact, this problem shows that we should look at the 4th Dynamic programming (the way to fill in the matrix shows what)


3

Easy, in the explanation has been pointed out in many places


4

A, space efficiency is also NK, see Code, or it can be seen from the matrix

B,OKThis problem shows the second view of dynamic programming 3rd (Optimization of spatial complexity)

---Why can optimization, as mentioned above, be able to optimize, and how to optimize the spatial complexity depends on its recursive form:

---from the graph of the fill matrix, it can be seen that this dynamic planning to produce the process (if the line is filled) is the last line of i-1 and item I add up to produce the next line of item I, traditionally, we left to right.

---in fact, depending on its production process (which relies on the mathematical characteristics of recursion itself), it canFrom right to left, so open an array on the line, on the original array of locally fixed number of fills, from right to left to fill

To ensureA location will not be used again after the overwrite (this is determined by the recursive properties, it is necessary to draw a picture to see more clearly).


So open a K-large array on the line, the specific implementation will not write, has been analyzed very clearly, the implementation is not difficult



--------------------------------------------------------------------------------------------------------------- ------------------------------------


From the above analysis, add exercise, I believe that the dynamic planning in the end is what, core ideas, specific operational details, as well as the understanding of dynamic planning has deepened it,

There are 2 points that I think are very important, one is the order of the Matrix, and the other is the optimization of the spatial complexity of the dynamic programming: These 2 points are with the recursiveDependence (which is the essence), which is manifested in the form ofwhen you fill in the Matrix, your

order to make sure that the locations you use (that is, what it depends on) are filled in each new location.Inspace optimization shows that you can't overwrite a position when it's still useful in the future.


These 2 conclusions are very important, is a profound understanding of the dynamic planning of an important ladder, I am also a long time to realize that, of course, they have more professional expression, in the next article I will post.


--------------------------------------------------------------------------------------------------------------- ------------------------------------


Let's take a look at 2 practical exercises, which is also relatively simple:



9, rely on, electronic version unexpectedly with the paper version of the same, the electronic version of the book does not have this problem, not cut the map, copy it:


Problem:

A chess car can move horizontally or vertically, and a car moves from one corner of a chessboard to the other corner of the diagonal, how many shortest paths are there?

A, using dynamic programming algorithm to solve

b, solving with elementary permutation and combinatorial knowledge


b) First say B bar, this is a very simple high school arrangement of the problem, assuming the chessboard size is n*n (embarrassed, chess board how big this has to think to know, say N bar), the answer is C (2n, N)


A) Use a method to do it (mainly the development of how to build a dynamic programming of the recursive type)

The question is how many methods are there to move from (0,0) to (n,n)? (Shortest way, that is, horizontal n vertical N, can not be returned)


Set C[i, j] represents the number of methods (I, J) moved from (0,0) to (describe the problem,How to portray the meaning of C[i, J] is a key point of dynamic planning):

So how do you get to (I, j), and its previous step must be (I-1, j) or (I, j-1)-------(Analysis of dynamic programming problemsReverse thinking, very important, to be said later)

This describes the problem in order to overlap the sub-Problem:

C[i, j] = C[i-1, j] + C[i, J-1] (The meaning of C[i, J])


What we're asking for ISC[n, N]


Initial:

C[0, J] = J J from 0 to N

C[i, 0] = i i from 0 to n

That is, the first column of the first row determines.


Fill in the form of a matrix: you can press rows or columns.


The above analysis to draw a diagram is easy to see. The rest of the implementation is very simple.




10





The first question is a probability problem, sounds more awkward, in fact, is not difficult, belong to high school probability level:



With a recursive, found that in fact, with a question exactly the same, is the recurrence of the multiplication of a probability value, no wonder that the electronic version omitted a question, the remaining slightly


--------------------------------------------------------------------------------------------------------------- -----------------------------------


Very Important:

After solving these two problems, we should know how the design process of a dynamic planning is:

Personal experience is the difficulty of dynamic planning lies in the early design:


A) How to describe the problem so that it can be expressed as a dynamic programming problem (what characteristics?)Most sub-structure, multi-stage decision-makingThinking

b) Recursive write-out (reverse thinking to analyze or forward thinking to recursion) to determine which value you are asking for

c) There is a recursive type can draw a matrix of the graph (generally only from the formula is not easy to see, of course, for the cattle can be despised), in the figure of the following two points:

Initial

Fill in the Order of the matrices (i.e. how to write code control statements)


With these, in fact, the dynamic programming code is very simple, its difficulty lies in the problem of the description and resolution phase, rather than the stage of writing code, the rest of the writing code is basically to follow the formula to fill the matrix.



--------------------------------------------------------------------------------------------------------------- -----------------------------------


Almost forgot one important question:

Let's take a look at the dynamic programming description of the chess problem, why it can be described as dynamic planning?

about what can be described as overlapping sub-problems, which have been analyzed above,


Let's say it again.most sub-structure and multi-stage decision making

Optimal sub-structure: there is an accurate definition, you can see some of the information, my own description is: In the process of dynamic programming, sub-problem generation of the solution for sub-problem is also an optimal solution

Multi-stage decision making: one-step decision-making, no-validity, decision-making depends only on the current state, not on the previous state.


Look at the best sub-structural properties of the chess problem: the minimum number of methods traveled by any (I, j) point before reaching the finish line.

Multi-stage decision-making: It's not about where you're going, it's about where you are at the moment.

Go: Dynamic planning

Related Article

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.