Get started with dynamic planning at a glance

Source: Internet
Author: User
Tags advantage

Today on the Internet to see a dynamic planning of the article, is a 01 backpack For example, this article and the explanation of the book is very different, so that my eyes a bright, so reproduced 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, now China's textbooks still this look, speak clearly some understand 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 comprehension place, please keep abreast point, thanks. 

--first section--Initial knowledge dynamic programming--–

   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 in the package and the total volume of the item does not exceed the size of the package m, the maximum value can be obtained. [For each item can not be taken several times, up to only one time, the reason is called 01 backpack, 0 means no, 1 means take] in order to use a vivid and more image of the way to explain the problem, I put this problem in another way to describe, as follows: There is a country, all the people are very honest and frank, One day they found 10 gold mines in their own country, and the 10 gold mines in a straight line on the map, the king was very happy to know the news, he would like to be able to dig out the gold for the benefit of the people, first he put these gold mines on the map from the west to east numbering, followed by 0, 1, 2, 3, 4, 5,

   6, 7, 8, 9, and then he commanded his hand to go down and survey every gold mine in order to know how much manpower is needed to dig each gold mine and how much gold each gold mine can 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. So how does the king know how much gold can be dug up in the case of only 10,000 people? KingHow to 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, then he called his most proud "left and right hand" to the front, said: "I do not have to consider the final mine to get the most gold, I only need to consider the gold mine before me can be, for the gold mine in front of me there are only two options, either dig, or not dig, 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 mines you can dig up as much gold as you could, 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 man replied, "Your Majesty, what you mean is that if I could extract up to x gold in other gold mines with 8,500 people, you would 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 say to his right-hand man, "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 men and the rest of the gold mine, how much gold can you dig up for me?" The King's right-hand man wisely said, "Dear King, I understand what you mean, if I can answer the most to purchase y gold coins, 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 men: "So dear left, I give you 8,500 people and the rest of the gold mine. Can you tell me what I can dig up?How much gold did you give? "Please be assured that this problem is difficult 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 I can." 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. As the story develops here, are you wondering how the King's two ministers found the answer to the king's satisfaction? Why they can 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 men 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 another man, "If I give you 8,500 people and all the other gold mines except the 8th and 9th, can you tell me how much gold you can dig up?" The King's left men thought, "if both of them can answer my question, then the question that the king has given me will not be solved." hahaha. "Because the king gave his right men 10,000 people, so the king's right men also called two people, to one of them asked:" If I give you 9,000 people and all other than the 8th and 9th of all other gold, you can 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 their left, are 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 you know how the two ministers solved the problem the king confessed to them.

   So how do you think the four people who are called by the minister are going to finish the question that 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, which is the legendary bottom-up 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 needs to be able to determine the maximum amount of gold that can be mined, based on the answers of two ministers and the information of a 9th gold mine.

   In order to solve his problems, he needs to create two other problems for others, both of which are sub-problems. Thinking about the 1th----optimal substructure of dynamic programming: The King believes that as long as his two ministers can answer the correct answer (and that is, the most optimal and the best of the gold that can be mined), and that his clever judgment will surely get the final correct answer.

   We put this seed problem optimal when the mother problem by optimizing the selection of a certain optimal situation is called "optimal substructure." Thinking about the 2nd----of dynamic planning overlap: 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, let you find out the most 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. Think of the 3rd----boundary of dynamic planning: Think about this problem if there is no such thing as the bottom-line 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----of dynamic programming Independence: 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 the gold, because they know that the king chooses only one of the two to be the last.case, another person's plan is not to be implemented, so one's decision has no effect on the other person'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? "

   Or what is 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.

--second section--The advantages of dynamic programming--–

   Now I assume that the reader you have figured out why dynamic planning is the right approach, but why we need to use dynamic planning. Please continue to appreciate this story: the King learned that his two men used the same method to solve their problems, not only did not think his two ministers in the lazy, but very happy, because he knew that his minister will find more people to solve the problem together, and more people will 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 how many combinations there are in all of them? "The King, if you use a combination of methods, 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 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 can be mined for 1000 people and 3 gold mines, while another needs to know how much gold the 1000 people and 3 gold mines can extract, 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, then aHow many different questions are there? "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 human.

   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 we need to consider the 2^100 situation,

   This number is about 10 of the 30-time square. 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 issue faces two choices, so Choosecount = 2, so the program runs at T = O (QuestIoncount * choosecount) =o (People * N), do not forget that the actual time required is less than this value, depending on the situation encountered vary.
 This is the magic of dynamic programming, which reduces a lot of computation, so we need dynamic planning.

--The third section--The thinking angle of dynamic programming ———-

   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, determine the second-to-last step until you consider the No. 0 gold mine (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.

--The fourth section--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 construction 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: Buy books have 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 activity, activities as follows: If you buy one of the volumes, then you can hit 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 money required, then there are: Minmoney (i,j,k) =min (S1,S2,S3,S4,S5,S6,S7), of which S1,S2,S3,S4
           , s5,s6,s7 the minimum money for each of the corresponding 7 schemes: S1 = $ * 0.95 + Minmoney (i-1,j,k) s2 = $ * 0.95 + Minmoney (i,j-1,k) S3 = 60 * 0.+ 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)

--sixth section--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: 5-A-133-in-a-kind

   Example:
   

Transferred from: http://blog.csdn.net/woshioosm/article/details/7438834

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.