A very special new tutorial on Dynamic Planning

Source: Internet
Author: User
A very special new tutorial on dynamic planning today I saw an article on dynamic planning on the Internet, taking the 01 backpack as an example. This article is very different from the commentary in the book, which makes me shine, so I reprinted the following ~~~
(To explain, I hate the formula and theorem of teaching materials, but the actual explanation is very boring and difficult to understand. Such Chinese-style education has been extended for thousands of years, is it so difficult to clarify Chinese textbooks? A teacher in high school always thinks that it makes sense: "A talented person is not a real skill. It is true that he can use all doctoral students .")
Attach the original address:
Http://www.cnblogs.com/sdjl/articles/1274312.html


Introduce the dynamic planning through the Gold Mine Model

For dynamic planning, it may take some time for everyone who is new to it to understand, especially when the first contact is always unable to figure out why such a method is feasible. This article aims to help everyone understand dynamic planning, it also explains the main 01 knapsack problem to guide readers in thinking about dynamic planning. This article strives to be easy-to-understand and non-specific, so that readers are not confused and guided to think about it. Therefore, if you find any inaccessibility during your reading, you may encounter errors and understandings, I 'd like to point out something you can hardly understand. Thank you!

---- Section 1 ---- initial knowledge of Dynamic Planning --------

The typical 01 backpack problem is this:
There is a package and N items, the package capacity is m, each item has its own volume and value, Q What is the maximum value that can be obtained when multiple items are selected from the N items and the total size of the items does not exceed the size m of the package? [Each item cannot be retrieved multiple times. It can be retrieved only once at most. The reason is "01 backpack". 0 indicates not to be retrieved, and 1 indicates to be retrieved.]

To illustrate this question in a more vivid and vivid way, I will describe this question in another way, for example:

In one country, all the people are honest and honest. One day they found ten gold mines in their own country, and these ten gold mines lined up in a straight line on the map, the king was very happy to know the news. He hoped that the gold could be dug out for the benefit of the people. First, he numbered the gold mines from the west to the east according to the location on the map, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 in sequence, and then he ordered his hand to continue the exploration of each gold mine, in order to know the amount of manpower required to dig each gold mine and the amount of gold that each gold mine can dig, and then mobilize the people to dig gold.

Question 1: The number of people required to dig each Gold Mine is fixed, and one more person is not enough. The king knows how many people are needed for each gold mine, and the number of people needed for the gold mine I is peopleneeded .
Question 2: The number of gold dug by each Gold Mine is fixed. When the No. 1 Gold Mine has peopleneeded If people dig, they will be able to dig out gold. Gold. Otherwise, no gold can be dug out.
Question 3: After the opening of a gold mine is completed, they will not go to other gold mines again, so one person can only use it once at most.
Question 4: The king recruited only 10000 people nationwide willing to dig gold for the country. Therefore, these people may not be enough to dig out all the gold, but the king hopes that the more gold he has dug, the better.
Question 5: everyone in this country is very honest (including the King) and will not swallow any gold or resort to fraud or lie.
Question 6: when many people obtain this question, their first response is to find out how much gold each gold mine can dig out on average, and then choose from high to low, this method is wrong. If you think so too, consider the backpack model. When there is a backpack with a capacity of 10, there are three items in it, the volume is respectively 3, 3, 5, and the value is respectively 6, 6, and 9. So what you get is the first two items, the total value is 12, however, the maximum value is 15 of the last two items.
Question 7: we only need to know how much gold can be dug up, rather than concerned about which gold mines do not dig.

Then, how does the king know how much gold can be dug in the case of only 10000 people? How does the king think about this problem?

The king first came to the location of the 9th gold mines (note that the first is the last one. Because it was numbered from 0, the gold mine on the West was 9th ), his son told him that it would take 9th people to dig 1500 gold mines, and 9th gold mines could dig 8888 gold. The king laughed and thought that it was very difficult to think about how much gold could be dug out in the case of only 10000 people in ten gold mines, just now, after hearing what his son said, The King knew the maximum amount of gold he could dig out, how can the king know the maximum amount of gold to be dug out without learning about other gold mines? His ministers did not know the mystery, so their ministers asked him: "We have not told you about the other gold mines of the smartest kings, how did you know the answer?"

The proud King smiled and called his most proud "left and right hand" to the front and said, "I don't have to consider what kind of gold mines should be dug to get the most gold, I only need to think about this gold mine in front of me. There are only two options for this mine, either digging or not digging, right?"

"Of course," The Ministers replied.

The king continued: "If I dig 9th gold mines, I can get 8888 gold now, and I will use 1500 people, then I have 8500 people left. My dear subordinate from left, suppose you tell me that when I give you all the remaining 8500 people and all the other gold mines, how much gold can you dig for me at most, so I don't know the maximum number of gold coins that I can get when 9th gold mines are open?"

After hearing this, the king's left-hand team replied, "Your Majesty, what do you mean is that I can use up to 8500 people to offer up to X gold coins in other gold mines, then you can get x + 8888 gold in total, right?"

"Yes, yes ...... Suppose the first gold mine must be open ......" The Ministers nodded and said.

The King smiled and continued to say to his right men, "Dear right men, maybe I am not planning to launch the 9th gold mines, so I still have 10000 people, suppose I have given you all these 10000 people and the remaining gold mines. How much gold can you dig out at most?"

The right men of the King said smartly: "My dear, I have made it clear that you mean it. If I have answered the question that I can buy up to y gold coins, then you can select a bigger one between Y and x + 8888, and this bigger one is the maximum number of gold coins we can finally get. Do you think I understand this?"


The King smiled more brightly and asked his left men: "So dear left men, if I give you 8500 people and other gold mines, can you tell me the maximum amount of gold I can dig out?"

"Please rest assured that this problem is difficult for me ". The left men packed tickets with the king.

The king happily continued to ask his right men: "What about you? If I have given you 10000 people and other gold mines, can you tell me the maximum amount of gold I can dig out ?"

"Of course! Give it to me !" The right team replied as confidently as the left team.

"Please give me two. Now I want to go back to my comfortable Palace and enjoy it. I look forward to your reply ." After the king said, he started to wait for the news. How confident he was that his two Ministers could give him an accurate answer, because the king actually knows that his two ministers are much smarter than him.

The story has evolved here. Are you wondering how the two Ministers of the King can find the answer to the king's comfort? Why are they so confident? In fact, they are indeed smarter than the king, because they learned something from the king, that is, they are full of confidence.

After the king left, the king's left and right came down to the 8th gold mines, where he had long waited for their gold miners to report to the two ministers: "Hello, two smart ministers, 8th gold mines need 1000 talents to get 7000 gold ".

Since the king only gave him 8500 people from the left, the king's left men called two people and asked one of them: "If I give you 7500 people and all the gold mines except 8th and 9th, can you tell me how much gold you can dig?"

Then the king's left men continued to ask another person: "If I give you 8500 people and all the other gold mines except 8th and 9th, can you tell me how much gold you can dig?"

The left half of the king thought in his mind: "If both of them can answer my questions, will the question given to me by the King not be solved? Hahaha !"

Because the king gave him 10000 people under the right, the king's right men also called two people and asked one of them: "If I give you 9000 people and all the gold mines except 8th and 9th, can you tell me how much gold you can dig?"

Then the king's right men continued to ask him another person who had called: "If I have given you 10000 people and all the other gold mines except 8th and 9th, can you tell me how much gold you can dig?"

At this time, the right men of the king, like the left men, are satisfied with their own cleverness.

Of course, the four called people answered questions with the same confidence because they learned the same thing from the two Ministers, the two Ministers who thought they were just as smart returned to their home with a smile and waited for others to answer their questions, now, do you know how these two Ministers solve the problem the king has given to them?

So how do you think the four people called by the Minister have completed the problem that the Minister handed over to them? The answer is, of course, they found the other eight!

It didn't take much effort. This problem has already been spread across the country. Many other people have found more people to solve the problem, but some do not have to ask another two people to help him, who can answer their questions without the help of others?

Obviously, when you are asked how much gold can be dug up for Z people and only 0th gold mines, you don't need help from others, because you know, if Z is greater than or equal to the number of people needed to dig 0th gold mines, the maximum number of gold mined is the amount of gold that can be dug from 0th gold mines, assuming that the Z individual is not enough to open 0th gold mines, the maximum number of gold that can be dug is 0, because the only gold mine is not enough manpower to open the mine. Let's applaud those who can come up with accurate answers without the help of others. This is the legendary bottom-layer working people!

Let's pause the story. Now, let's analyze the story again to give us a rational understanding of dynamic planning.

Sub-questions:
The king had to deduce the maximum amount of gold that he could open, based on the answers of the two ministers and the information of the 9th gold mines. To solve his own problems, he needs to create two other problems for others. These two problems are sub-problems.

The first point of Dynamic Planning-optimal sub-structure:
The king believes that only two of his ministers can answer the correct answer (for the maximum number of gold that can be obtained, that is, the most optimal value at the same time, that is, true ), in addition, his smart inference will surely get the correct answer. We call this subproblem the optimal sub-structure after optimization and selection ".

Think about the second point of Dynamic Planning-overlapping sub-problems:
In fact, the king, the minister, and all people are facing the same problem, that is, to give you a certain number of people, to give you a certain number of gold mines, let you find the maximum number of gold that can be opened. We refer to the case where such a parent problem and a subproblem are essentially the same problem as a "subproblem Overlap ". However, the difference in the problem is the number of workers transmitted between the quilt problem, for example, the number of people and the number of gold mines.

Think about the third point of Dynamic Planning-boundary:
If there are no bottom-layer workers we mentioned earlier, can this problem be solved? Never! We call this subproblem a boundary and an endless loop will occur if there is no boundary.

Thinking about the fourth point of Dynamic Planning-independent sub-problems:
You know, when the two Ministers of the King are thinking about their own problems, they will not care about how the other party calculates how to open a gold mine because they know that, the King will only choose one of the two people as the final scheme, and another person's scheme will not be implemented. Therefore, the decision of one person has no influence on the decision of another person. In this case, when selecting a subproblem, the selected subproblem does not affect each other ".


This is dynamic planning with "optimal sub-structure", "subproblem overlapping", "boundary", and "subproblem independent ", when you find that the question you are thinking about has these four attributes, congratulations, you have basically found a dynamic planning method.

With the above points, we can write the transfer equation for dynamic planning. Now we can write the equation for this problem, assume that gold [minenum] is used to represent the number of gold that can be mined from the minenum Gold Mine, and peopleneeded [minenum] is used to represent the number of people needed to dig the minenum Gold Mine. Use the function f (people, minenum) indicates that when there are people and numbers 0, 1, 2, 3 ,...... If the maximum gold number that can be obtained from the minenum gold mine, what is F (people, minenum? 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 hour f (people, minenum) = f (People-peopleneeded [minenum], mineNum-1) + gold [minenum] greater than F (people, mineNum-1, the first two formulas correspond to the "boundary" of the dynamic planning, and the last one is the "optimal sub-structure" of the corresponding dynamic planning. Please clarify it before proceeding.






---- Section 2 ---- strengths of Dynamic Planning --------

Now, if you have figured out why dynamic planning is the correct method, why do we need dynamic planning? Please continue to appreciate this story:

After the King learned that his two men used the same solution as him to solve the problem he had given to them, he did not feel that his two ministers were being lazy, but he was very happy because he knew that, his minister will certainly find many other people to solve the problem, while many others will find more people, in this way, his smart method will be spread out inadvertently, and the people all over the country will know that this smart method was developed by their great kings. Do you say the kings are unhappy?

However, the King also had some concerns, because he really did not know how many people were needed to complete the project. If there were too many people who helped him solve the problem, it would be too much labor-consuming. "Will it affect this year's harvest ?" The king thought about the problem, so he invited the only two mathematical geniuses in the country, one called Xiaotian and the other called Xiaocai.

The king asked Xiaotian: "Oh, my God, I found this problem a little serious. I know that it can be simply considered a combination problem, that is to say, select several gold mines from ten gold mines for unlocking, to see which combination gets the most gold, and the combination method may be better. Can you tell me how many combinations I have together ?"

"Your Majesty the king, if you use a combination of methods, you have to consider the 10 power of 2, that is, 1024 cases ." After thinking about it, Xiao Tian replied.

"Well ......, If I hand over a person in each case to calculate the amount of gold that can be obtained, I need 1024 people. In fact, there are still quite a lot ." The king once again seems to feel that his approach is correct.

The king looked forward to a better answer from his mind and asked, "Xiao Cai, can you tell me how many people I need in my way? As a matter of fact, I have calculated that the number of people needed is 1 + 2 + 4 + 8 + 16 + 32 + 64 + ......, After all, everyone needs to find two other people to help them ......"

"Dear king, we don't need so many people, because there are many problems that are actually the same, however, we only need to use one person's power for every different problem."

The king asked happily: "How do you say this ?"

"For example, if one person needs to know how much gold can be mined from 1000 people and 3 gold mines, at the same time, if one person needs to know how much gold 1000 people and three gold mines can open, then they can ask the same person, instead of looking for different people to waste manpower."

The king thought and said, "Well, it makes a lot of sense. If the problem is the same, you don't have to ask two different people. That is to say, a different problem requires only one person, how many different problems does one share?"

"Because the number of people in each problem can get from 0 to 10000, and the number of gold mines can get from 0 to 10, a maximum of 10000*10 is equal to 100000 different problems ." Xiaocai calculates and answers.

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

"Please rest assured that we need far less manpower than this number. because not every problem will occur, we may only need one or two hundred manpower to solve the problem, this is mainly related to the number of people needed by each gold mine." Little talent immediately replied.

At the end of the story, the king once again proved to his subjects that he was the smartest person in the country, now we will consider the other two points of thinking about dynamic planning through the second part of the story.

Think about the fifth point of Dynamic Planning-Memorandum:
As mentioned above, when we encounter the same problem, we can ask the same person. To put it bluntly, we can free up the problem in a variable. If we encounter this problem again, we will get the answer directly from the variable. Therefore, each question will only be calculated once, if you don't forget it, dynamic planning has no advantages at all.

Think about the sixth point of Dynamic Planning-time analysis:
As mentioned above, if we use the exhaustive method, we need at least 2 ^ n constant time, because there are 2 ^ n cases in total, we need to consider it. In the case of a backpack, if the package capacity is 1000 and the number of items is 100, consider the case of 2 ^ 100, which is about 30 Power of 10.

If dynamic planning is used, there are only 1000*100 = 100000 different problems at most, which is obviously superior to the 10th power. The actual situation does not have so many different problems. For example, in the gold mine model, if all the gold mines require 1000 people, then the total number of problems is about 100.

In informal mode, we can easily obtain the time required for dynamic planning. Assume that questioncount is the same subproblem, and each problem needs to face the choosecount option, the time required is the constant questioncount * choosecount. In the goldmine model, there are at most people * n subproblems (in which people is the total number of gold mines used in kaixiao and N is the total number of gold mines). Therefore, questioncount = people * n, just as the king needs to consider whether to use the result of the bottom left or the result of the bottom right, each problem faces two choices, so choosecount = 2, therefore, the execution time of the program is t = O (questioncount * choosecount) = O (People * n). Do not forget that the actual time required is smaller than this value, depending on the detailed situation.

This is the magic of dynamic planning. It reduces a lot of computing, so we need dynamic planning!






---- Section 3 ---- thoughts on Dynamic Planning ----------

So what is dynamic planning? I personally think that if a method to solve this problem satisfies the first four of the above six thinking points, this method is a dynamic planning. When thinking about the dynamic planning method, the last two points must also be considered.

In the face of problems, we need to find a dynamic planning method. First of all, we must be clear that dynamic planning is not an algorithm. It is a method that finds the optimal value in the process of an event, therefore, we need to consider the process of this incident. We usually start from the last step of the process, rather than the beginning of the process.

For example, in the above mining problem, we can feel that the entire opening process is from the West to the East (that is, starting from the 0th ), then there will always be two choices for the last gold mine (9th), namely, opening up and not opening up the gold mine, and then determining the second last step when determining the last step, until 0th gold mines are considered (the beginning of the process ).

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

Therefore, if you want to solve a problem by using dynamic planning, you 'd better first think about the process and then consider how to choose the last step of the process, we usually need to construct a process by ourselves, for example, the following exercises.







---- Section 4 ---- summary -------

So how can we solve the problem with dynamic planning? Based on the above analysis, we can consider the following steps:

1. Construct the corresponding process of the problem.
2. the last step in the thinking process is to see which options are available.
3. Locate the sub-Problem in the last step, ensure that the sub-problem overlaps with each other, and set the number of shards for different sub-problems.
4. Make the sub-problem conform to the "optimal sub-structure ".
5. Locate the boundary and consider various processing methods of the boundary.
6. Ensure that the "sub-problem independence" is met. Generally, assume that we select one of the multiple sub-problems as the implementation scheme, rather than implementing multiple schemes at the same time, sub-problems are independent.
7. Consider how to make a memorandum.
8. Check whether the required time meets the requirements.
9. Write the transfer equation.





---- Section 5 ---- exercise -------

Question 1: Buy Books
A bookstore has introduced a set of books with three volumes, each of which is priced at 60 yuan. In order to engage in promotions, the bookstore has launched an activity, for example:

If you purchase one volume separately, you can get a 9.5 discount.
If you purchase two different volumes at the same time, you can get a discount.
If you purchase three different volumes at the same time, you can get a 8.5 discount.

Suppose James wants to purchase 1st X, 2nd y, and 3rd Z, how much does it cost at least? (X, y, and z are three known integers ).

Of course, this question can be solved without dynamic planning at all, but now we want to learn dynamic planning, so please think about how to do it with dynamic planning?

Answer:

1. The process is a one-time purchase. Each purchase may only buy one (there are three solutions) or two (there are also three solutions ), or buy three books together (there is a solution) until you buy all the books you need.
2. In the last step, I will definitely select one of the seven purchase schemes, so I want to select the best one among the seven purchase schemes.
3. The sub-question is, after I select a scheme, how can I make the purchase of the remaining books use the least money? This option does not make the remaining books negative. Both the primary and subquestions are given the purchase volume of three books, and the minimum amount of money is required. Therefore, there are "overlapping subquestions". In this case, the three purchase volumes are set to the number of shards, they are I, j, and K.
4. It does.
5. boundary is the ability to buy a full book at a time. Please consider the solution yourself.
6. A maximum of seven solutions can be selected at a time, and different implementation methods are not implemented at the same time. Therefore, the selection of solutions is independent of each other ".
7. I can use minmoney [J] [k] is used to save the minimum money required to purchase a 1st-Volume I book, a 2nd-volume J book, and a 3rd-volume K book.
8. shared issues with x * y * z. Each problem is faced with seven options: O (x * y * z * 7) = O (x * y * z ).
9. The minmoney (I, j, k) function is used to indicate the minimum money required to purchase 1st-Volume I, 2nd-volume J, and 3rd-volume K. There are:
Minmoney (I, j, k) = min (S1, S2, S3, S4, S5, S6, S7), where S1, S2, S3, S4, S5, S6, s7 is the minimum money used for the corresponding 7 solutions:
S1 = 60*0.95 + minmoney (I-1, j, k)
S2 = 60*0.95 + minmoney (I, J-1, K)
S3 = 60*0.95 + minmoney (I, j, k-1)
S4 = (60 + 60) * 0.9 + minmoney (I-1, J-1, K)
S5 = (60 + 60) * 0.9 + minmoney (I-1, J, k-1)
S6 = (60 + 60) * 0.9 + minmoney (I-1, J, k-1)
S7 = (60 + 60 + 60) * 0.85 + minmoney (I-1, J-1, k-1)





---- Section 6 ---- code quiz ------


The following provides the source code of the goldmine program to help readers understand and provide trial data for everyone to practice.

The input file name is "beibao. In". Because this problem is actually a problem with the backpack, the name of the data file for the trial is retained.
There are two numbers in the first line of the input file. The first is the total number of gold mines that the King can use to open gold mines, and the second is the total number of gold mines found.
The input file 2nd to n + 1 row has two numbers, the first line of the two numbers respectively indicate the number of I-1 gold needs and the number of gold can be obtained.

The output file contains only one integer, indicating the maximum available gold.

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

Output example:
133

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.