It's a special one. An introductory tutorial on dynamic planning

Source: Internet
Author: User

It's a special one. An introductory tutorial on dynamic planning(2016-03-10 17:05:48) reproduced
Tags: cpp Dynamic Planning Category: CPP Featured
It's a very special one. Dynamic planning Introductory Tutorial today on the Internet to see a dynamic planning of the article, is a 01-pack For example, this article and the explanation of the book is very different, so that my eyes a bright, so reprint a bit next ~ ~ ~
(Explain, I very much hate the textbook formula theorem fly, the actual explanation is very kuse difficult to understand, this kind of Chinese education has been stretching for thousands of of years, and now China's textbooks still this look, speak clearly some understand it is so difficult? High school has a teacher said a word has always felt very reasonable: "Teach genius is not true ability, can be a PhD student's things to the elementary school students will use that is the true level." ”)
Attached to the original address:
Http://www.cnblogs.com/sdjl/articles/1274312.html


Introduction of dynamic programming through gold mining model

For dynamic planning, it takes a while for each person in contact to understand, especially when the first contact is not always possible, this article is designed to help you understand dynamic planning and to guide readers on how to think about dynamic planning by explaining basic 01 knapsack questions. This article strives to be easy to understand, the non-sexual, does not allow the reader to feel puzzled, guides the reader to ponder, therefore if you in the reading found has not fluent the place, lets you produce the wrong understanding place, lets you the rare reading place, please keep abreast point, thanks!

----The first section----the initial knowledge of dynamic planning--------

The classic 01 backpack problem is this:
There is a package and n items, the capacity of the package is m, each item has its own volume and value, asked when the number of items from these n items placed in the package and the total volume of the item does not exceed the capacity of M, what is the maximum value to get? [For each item can not be taken multiple times, the maximum can be taken once, the reason is called 01 backpack, 0 means no, 1 means take]

In order to explain this problem in a vivid and more graphic way, I describe it in another way, as follows:

There was a country where all the people were very honest, and one day they found 10 gold mines in their own country, and the 10 gold mines lined up on the map, and the king was delighted to know the news, and he hoped that the gold would be dug out for the benefit of the nation. First he numbered the gold mines on the map from west to east, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and then he ordered his hands to go down and survey every gold mine in order to know how much manpower to dig each gold mine and how much gold each gold mine could dig. And then mobilize the nation to dig for gold.

Add 1: The number of people who need to dig every gold mine is fixed, and one person is less than one. The King knows how many men each gold mine needs, and the number of gold mines I need is peopleneeded.
Supplementary 2: The number of gold dug up in each gold mine is fixed, and when the peopleneeded is dug in the first gold mine, it will be able to dig up gold gold. Otherwise a gold can not be dug out.
Supplement 3: People who have mined a gold mine will not be able to exploit other gold mines once they have finished their work, so a person can only use it once.
Supplementary 4: The King recruited only 10,000 people who were willing to dig gold for the country, so these people might not be able to dig out all the gold, but the more gold the king wanted to dig, the better.
Title 5: Everyone in this country is honest (including the King), will not pocketed any gold, will not cheat, will not lie.
Topic Add 6: A lot of people get this problem after the first reaction is to find out how much gold each gold mine, and then choose from high to low, here to emphasize this method is wrong, if you think so, please consider the backpack model, when there is a backpack capacity of 10, a total of 3 items, The volume is 3, 3, 5, respectively, the value is 6, 6, 9, then your method takes the first two items, the total value is 12, but the obvious maximum is the latter two items composed of 15.
Add 7: We just need to know how much gold can be dug up, without worrying about which gold mines to dig.

How, then, does the king know how much gold can be dug up in the case of only 10,000 people? How did the king think about the problem?

The king first came to the seat of the 9th gold Mine (note that the 9th was the last one, since it was numbered from 0, the most west of the gold Mine was No. 0), and his courtiers told him that it would take 1500 people to dig up the 9th gold mine, and that 8,888 gold would be dug up in the 9th gold mine. Heard the king laughed, because he thought it was hard to think about how much gold the 10 gold mines could dig up in the case of only 10,000 people, but the king had known how much gold could be dug up in total, just after listening to the words of his courtiers. How did the king know how much gold he could dig up without knowing the other gold mines? His courtiers did not know the mystery, so his courtiers asked him, "the Smartest King of kings, we have not told you about other gold mines, how did you know the final answer?" ”

The proud king smiled and then called his proudest "left and right hand" to the front and said, "I don't have to think about the gold that I'm going to dig to get the most gold, I just need to think about the gold mine before me, but there are only two options for the gold mine in front of me, either digging or not digging, right?" ”

"Of course, of course," the ministers answered.

The King continued: "If I dig the 9th gold mine then I can get 8,888 gold now, and I will use 1500 people, then I have 8,500 people left." My dear left, if you tell me that when I give you all the remaining 8,500 people and all the rest of the other gold mine to mine, how much gold can you dig up for me, then I don't know the maximum number of gold coins that can be obtained in the case of a 9th gold mine? ”

The king's left-hand replied, "Your Majesty, you mean that if I can extract up to x gold coins in other gold mines with 8,500 people, then you will be able to get X + 8,888 gold, right?" ”

"Yes, yes ... If the 9th gold mine must be mined ... "the ministers nodded.

The king smiled and continued to his right-hand, saying, "Dear right, perhaps I am not going to mine this 9th gold mine, so I still have 10,000 people, and if I give you these 10,000 people and the remaining gold mine, how many gold can you dig out for me?" ”

The King's right-hand man wisely said, "Dear King, I understand what you mean, if I answer the most can buy the mining of y gold, then you can choose between Y and x+8888 a larger, and this larger is the ultimate we can get the maximum number of gold, you see I understand that right? ”


The king smiled more brightly, and asked his left: "Then dear left, I give you 8,500 people and the rest of the gold mine." Can you tell me how much gold I can dig up? ”

"Please be assured that this problem is hard for me." The left subordinates said to the king.

The King was pleased to continue to ask his right men: "What about you, if I give you 10,000 people and the rest of the gold mine, can you tell me how much gold I can dig up?" ”

"Of course it does!" Give it to me! The right men, as well as the left, answered confidently.

"Then please give you two, now I will go back to my comfortable palace to enjoy, I look forward to your reply." When the king had finished, he began to go back to the news, how he believed that his two ministers could give him an accurate answer, because the king actually knew that his two ministers were much smarter than him.

The story is here, are you wondering how the King's two ministers found the answer to the king's satisfaction? Why would they be so confident? In fact they are really smarter than the king because they have learned a little from the king, and that is what makes them confident.

The king's left and right men came to the 8th gold mine, where they had been waiting for their gold Scout to report to two ministers: "The wise two ministers, you are good, the 8th gold mine requires 1000 people to be mined, and 7,000 gold can be obtained."

Since the king gave only 8,500 men to his left, the king's left men called two men, and one of them asked, "If I give you 7,500 people and all the other gold mines except the 8th and 9th, can you tell me how much gold you can dig up?" ”

Then the king's left men continued to ask the other man, "If I give you 8,500 people and all the other gold mines in the 8th and 9th, can you tell me how much gold you can dig up?" ”

The King's left men thought, "if they both could answer my question, would the king's question not be solved?" Haha, huh! ”

Because the king gave his right 10,000 men, the King's right men also called two people, and asked one of them: "If I give you 9,000 people and all the other gold mines except the 8th and 9th, can you tell me how much gold you can dig up?" ”

Then the king's right men continued to ask the other man he had called: "If I give you 10,000 men and all the other gold mines except the 8th and 9th, can you tell me how much gold you can dig up?" ”

At this point, the King's right men, like his left, were all satisfied with their cleverness.

Of course, the four people who were called were equally confident in answering no questions, as they learned the same thing from the two ministers, and two of the ministers who thought they were as smart, proudly returned to their mansion, waiting for others to answer the questions they had raised, Now do you know how the two ministers solved the problem the king confessed to them?

So how do you think the four people who have been called by the minister are going to do what the minister has given them? The answer is, of course, they found eight other people!

Not much effort, the problem has been spread around the country, more people have found more people to solve the problem, and some people do not need to find another two people to help him, who do not need the help of others can answer their questions?

Obviously, when asked to give you Z-man and only a No. 0 gold mine when the maximum amount of gold can be dug up, you do not need the help of others, because you know, if z is greater than the number required to dig the No. 0 gold mine, then the largest number of gold dug out of the No. 0 gold mine can be dug out of the number of gold, If this z-man is not enough to exploit the No. 0 gold mine, then the maximum amount of gold that can be dug is 0, because the only gold mine is not human enough to exploit. Let us applaud those who do not need the help of others to get an accurate answer, this is the legendary bottom working people!

The story of the first pause here, we now re-analyze the story, let us have a rational understanding of dynamic planning.

Sub-Problem:
The king needed to determine how much gold could be mined, based on the answers of two ministers and the information of the 9th gold mine. In order to solve his problems, he needs to create two other problems for others, both of which are sub-problems.

Consider the 1th----optimal substructure of dynamic programming:
The king believed that as long as his two ministers were able to answer the correct answer (and that the maximum amount of gold that could be mined was the most optimal), and that his clever judgment would surely get the right answer. We put this seed problem optimal when the mother problem by optimizing the selection of a certain optimal situation is called "optimal substructure."

Consider the 2nd----sub-problem overlap in dynamic planning:
In fact, the king or the minister, all face the same problem, that gives you a certain number of people, give you a certain number of gold mines, let you find out the maximum amount of gold can be mined. We refer to the fact that this mother problem is essentially the same problem as the sub-problem, which is called "sub-problem overlap". However, the differences in the problem are often the parameters passed between quilt problems, such as the number of people here and the number of gold mines.

Consider the 3rd----boundary of dynamic planning:
Think about this problem if we don't have the bottom-up workers we mentioned earlier? It's never going to happen! We put this seed problem at a certain time no longer need to raise the sub-sub-problem of the situation is called the boundary, no boundary will be a dead loop.

Thinking about the 4th----Sub-problem of dynamic planning is independent:
You know, when the King's two ministers are thinking about their own problems, they don't care how the other person calculates how to mine gold, because they know that the king chooses only one of the two people as the final option, and the other's plan is not implemented, so one's decision has no effect on the other's decision. We put this kind of a female problem in the sub-problem selection, the currently selected sub-problem 22 does not affect the situation is called "sub-problem independent."


This is dynamic planning, with "optimal substructure", "sub-problem overlap", "Boundary" and "sub-problem independence", when you find that you are thinking of the problem with these four properties, then congratulations, you have basically found a dynamic planning method.

With these points above, we can write the transfer equations for the dynamic programming, and now we will write the equations that correspond to this problem, if we use Gold[minenum] to represent the number of gold that can be dug in the Minenum gold Mine, with Peopleneeded[minenum] Represents the number of people required to dig the Minenum gold mine, using function f (people,minenum) to indicate the maximum number of gold that can be obtained when there are people individuals and gold mines numbered 0, 1, 2, 3 、......、 minenum, F (people,minenum) equals what? What about the transfer equation of f (people,minenum)?

The answer is:
When minenum = 0 and people >= Peopleneeded[minenum] f (people,minenum) = Gold[minenum]
When minenum = 0 and people < Peopleneeded[minenum] f (people,minenum) = 0
When Minenum! = 0 o'clock F (people,minenum) = f (People-peopleneeded[minenum], mineNum-1) + Gold[minenum] with the larger of f (people, mineNum-1) , the first two formulas correspond to the "boundary" of the dynamic programming, and the latter one corresponds to the "optimal substructure" of the dynamic programming.






----Section Two----the benefits of dynamic planning--------

Now I assume that the reader you have figured out why dynamic planning is the right approach, but why do we need to use dynamic planning? Please continue to enjoy this story:

When the king learned that his two men had used the same method to solve their problems, not only did he not think that his two ministers were slacking off, but rather pleased, because he knew that his minister would inevitably find more people to solve the problem together, and more people would find more people, So that his clever method would inadvertently spread, and the people of the country will know that this clever way is their great king to come out, you say the King can not be happy?

But the king also had some concerns because he did not know how many people the project was going to take, and it was too costly if there were too many people to help him solve the problem. "Will it affect the harvest this year?" The king was thinking about the problem, so he invited the only two mathematical geniuses in the whole country, one called the small day, the other a small talent.

The king asked the small day: "Small days ah, I found this problem is a bit serious, I know that this can be seen as a combination of problems, that is, from 10 gold mines to select a number of gold mining, to see which combination of the most gold, perhaps a better combination method." Can you tell me the total number of combinations? ”

"Your Majesty, if you use the combination method, you have to consider 2 of the 10-time situation, that is, 1024 kinds of situations." The little day thought for a while to answer.

"Well ..., if I give a person to calculate the number of gold I can get, then I want 1024 of them, which is actually quite a lot." "The king seemed to feel the right way again."

The king is looking forward to the small can give it a better answer, asked: "Small, ah, then you can tell me how many people need to use my method in total?" In fact, I have calculated that it seems that the number of people needed is 1+2+4+8+16+32+64+ ..., after all, everyone really needs to find another two to help them ... "

To live up to the king's expectations, little smiled and said, "Dear King, we do not need so many people because there are many problems that are the same, and we only need to use one manpower for each of the different problems." ”

The King was pleased to ask: "How to say this?" ”

"For example, if a person needs to know how much gold the 1000 people and 3 gold mines can extract, while another needs to know how much gold can be mined by 1000 people and 3 gold mines, then they can ask the same person instead of having to find different people to waste their manpower." ”

The King thought, "Well, it makes sense, if the problem is the same, then there is no need to ask two different people, that is, a different problem requires only one human, so how many different problems?" ”

"Because the number of people per question can be taken from 0 to 10000, and the number of gold mines can be taken from 0 to 10, up to about 10000 * 10 equals 100,000 different problems. The little one was counted as one side to answer.

What 100,000 questions? 100,000 manpower? The king was a little disappointed.

"Please be assured that the fact that we need more manpower than this number, because not every problem will encounter, perhaps we need only one or 200 people can solve the problem, which is mainly related to the number of gold mines required." The little was answered immediately.

At the end of the story, it is natural that the king once again proves to his subjects that he is the smartest man in the country, and now we are thinking through the second part of the story about the other two points of view of dynamic planning.

Think of the 5th----of dynamic Planning as a memo:
As mentioned above, when we encounter the same problem, we can ask the same person. The popular point is that we can liberate the problem in a variable, if we encounter this problem directly from the variable to get the answer, so each problem will only be calculated once, if you do not make a memo, dynamic planning will not have any advantage to say.

Consider the 6th----time Analysis of dynamic planning:
As mentioned above, if we use the method of exhaustive, at least 2^n a constant time, because there is a total of 2^n situation to consider, if in the knapsack problem, the package capacity is 1000, the number of items is 100, then need to consider the 2^100 situation, the number is about 10 30 times.

And if the dynamic planning, up to probably only 1000*100 = 100,000 different problems, this and 10 of the 30-side advantage is very obvious. The reality is that there are not so many different problems, such as in the gold model, if all gold deposits require 1000 people, the total number of problems is about 100.

Informally, we can easily get the time required for dynamic planning, and if there is a common questioncount of the same sub-problem, and each problem needs to face choosecount choice, we need time for Questioncount * Choosecount constants. In the gold model, the sub-problem has a maximum of about people * N (where people is the total number of people used to mine gold, n is the total number of gold mines), so Questioncount = People * N, and just as the king needs to consider whether to use the left subordinate results or the results of the right subordinates, Each problem faces two choices, so Choosecount = 2, so the program runs at T = O (Questioncount * choosecount) =o (People * N), don't forget that the actual time required is less than this value, depending on the specific situation encountered.

This is the magic of dynamic planning, which reduces a lot of computation, so we need dynamic planning!






----The third section----The thinking angle of dynamic planning----------

So what is dynamic planning? I personally think that if a problem-solving approach satisfies the top four of the six points above, then this approach is dynamic planning. In considering the dynamic programming method, the following two points also need to be considered.

In the face of the problem to find a dynamic planning method, first of all, it is clear that dynamic programming is not an algorithm, it is a method, it is in the process of the occurrence of a thing to find the best value of the method, so we need to take into account the process of this thing happened. Instead of thinking about the beginning of the process, we usually start with the final step of the process.

For example, the gold mining problem above, we can think that the entire mining process from the west to the east of the mining (that is, starting from the No. 0), then there is always the time to face the last gold Mine (9th), the Gold mine is two options, mining and non-mining, in the final step to determine the second-penultimate step, Until the No. 0 gold mine is considered (the beginning of the process).

The beginning of the process, that is, the last step of consideration, is the boundary.

So when you have a problem that you want to solve with a dynamic programming approach, think about what the process is, and then consider how the final step of the process is chosen, and usually we need to construct a process, such as the following exercises.







----Section Fourth----summary-------

Then how to solve the problem with dynamic planning? According to the above analysis, we can follow the following steps to consider:

1, the structure of the corresponding process.
2. Consider the final step of the process and see what options are available.
3, find the last step of the sub-problem, to ensure that the "sub-problem overlap", the problem is not the same place set as parameters.
4, so that sub-problems in line with the "optimal sub-structure."
5. Find the boundary and consider the various processing methods of the boundary.
6, to ensure that "sub-problem independent", in general, if we are in a number of sub-problems to choose one as an implementation, and not implement multiple scenarios at the same time, then the sub-problem is independent.
7. Consider how to make memos.
8. Whether the time required for analysis meets the requirements.
9, write the transfer equation.





----The fifth section----practice-------

Title One: Buying books
There is a bookstore introduced a set of books, a total of 3 volumes, each book price is 60 yuan, bookstore in order to engage in promotion, launched an event, the following activities:

If you buy one of the volumes separately, you can call 95 percent.
If you buy two volumes differently, you can call 90 percent.
If you buy three volumes differently, you can call 85 percent.

If Xiaoming wants to buy the 1th volume X, 2nd Volume y, 3rd volume Z, then at least how much money? (x, Y, Z are three known integers).

Of course, this problem can not be solved by dynamic programming, but now we are learning dynamic planning, so think about how to use dynamic planning to do?

Answer:

1, the process for a one-time purchase, each purchase may only buy one (there are three options), or buy two (there are three options), or three buy together (this has a plan), and finally until the purchase of all the necessary books.
2, the last step I will definitely choose one of 7 purchase options, so I want to choose one of the best in 7 purchase scenarios.
3, sub-question is, I chose a scheme, how to make the purchase of the remaining books can use the least money? And this choice does not make the remaining books negative. Mother problem and sub-problem are given three volumes of book purchases, the minimum need to use the money, so there is "sub-problem overlap", the problem of three purchase amount is set to parameters, respectively, I, J, K.
4. It does match.
5, the border is a purchase can buy all the books, processing methods please the reader's own consideration.
6, the choice of a maximum of 7 options, and will not be implemented at the same time, so the choice of options do not affect each other, so there is "sub-problem independent."
7. I can use minmoney[j][k] to save the minimum money required for the purchase of volume 1th I, Volume 2nd J, volume 3rd K.
8, a total of x * y * z questions, each problem face 7 choices, time is: O (x * y * z * 7) = O (x * y * z).
9, using the function Minmoney (i,j,k) to represent the purchase of the 1th Volume I, 2nd Volume J, 3rd volume of the minimum required for the money, then there are:
Minmoney (i,j,k) =min (S1,S2,S3,S4,S5,S6,S7), where S1,S2,S3,S4,S5,S6,S7 is the least money used for the corresponding 7 scenarios:
S1 = * 0.95 + Minmoney (i-1,j,k)
S2 = * 0.95 + Minmoney (i,j-1,k)
S3 = * 0.95 + Minmoney (i,j,k-1)
S4 = (+ +) * 0.9 + Minmoney (i-1,j-1,k)
S5 = (+ +) * 0.9 + Minmoney (i-1,j,k-1)
S6 = (+ +) * 0.9 + Minmoney (i-1,j,k-1)
S7 = (+ +) * 0.85 + Minmoney (i-1,j-1,k-1)





----section Sixth----Code reference------


The following program source code for gold issues helps the reader to understand and provides test data for everyone to practice.

The input file name is "Beibao.in", because this problem is actually a knapsack problem, so the test data file name is reserved.
Input file The first line has two numbers, the first is the total number of kings available for mining gold, and the second is the total number of gold mines found.
The 2nd to N+1 line of the input file has two numbers per line, and the two numbers in line I represent the number of people required and the number of gold that can be obtained for the I-1 gold mine respectively.

The output file is only an integer representing the maximum number of gold that can be obtained.

Input Sample:
100 5
77 92
22 22
29 87
50 46
99 90

Sample output:
133

It's a special one. An introductory tutorial on dynamic planning

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.