Classic 01 backpack Problems
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 this method is feasible. This article aims to help everyone understand dynamic planning, it also explains the basic 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. If you find any inaccessibility during your reading, you may have a wrong understanding, point out something that makes it hard for you to understand ~
---- Section 1 ---- initial knowledge of Dynamic Planning --------
The typical 01 backpack problem is as follows:
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 volume of items does not exceed the capacity M of the package? [Each item cannot be retrieved multiple times. It can only be retrieved 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, as shown below:
In one country, all the people are very 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 to dig out the gold 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 go down and survey 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 the gold.
Question 1: The number of people required to mine Each Gold Mine is fixed, but not one more person. The king knows how many workers each gold mine needs, and the number of gold mines I need is P [I].
Question 2: The number of gold dug out by each Gold Mine is fixed. When a person from block I gold mine goes to dig, we will be able to dig out G [I] Gold. Otherwise, no gold can be dug out.
Question 3: After the mining of a gold mine is completed, they will not mine other gold mines again, so a 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 able 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 honest (including the King) and will not swallow any gold or resort to frauds 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, it should be emphasized that 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 3 items in total, the volume is 3, 3, 5, and the value is 6, 6, and 9 respectively. So what you get is the first two items with a total value of 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.
How can the king know the maximum amount of gold that can be dug out when there are only 10000 people? How does the king think about this problem?
The king first came to the location of 9th gold mines (note that the first is the last one, because it was numbered from 0, and the gold mine on the West is 9th ), his son told him that 9th people would be needed to dig 1500 gold mines, and 9th gold mines would be enough to dig 8888 gold. The king laughed, because he thought it was hard to think about how much gold could be dug out in the case of only 10000 people in ten gold mines. However, 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 that can 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 do you know the final answer?"
The proud King smiled and called his "left and right hand" to the front. He said, "I don't need to consider which gold mines will be used to obtain the most gold, I only need to think about this gold mine in front of me. There are only two options for this mine in front of me, either digging or not digging, right?"
"Of course," The Ministers replied.
The king continued: "If I had dug 9th gold mines, I would have had 8888 gold now, and I would have used 1500 people, then I would have 8500 people left. My dear subordinate from left, if you tell me how much gold you can dig out at most when I hand over all the remaining 8500 people and all the other gold mines to you, so I don't know the maximum number of gold coins I can get when 9th gold mines are mined?"
After hearing the answer from the king's left, he said, "Your Majesty, what do you mean if I can mine up to X gold coins from 8500 other gold mines, then you can get x + 8888 gold in total, right?"
"Yes, yes ...... If 9th gold mines are mined ......" 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 mine these 9th gold mines, so I still have 10000 people, how much gold can you dig out if I have given you all the 10000 people and the remaining gold mines?"
The right men of the King cleverly said, "My majesty the king, I understand what you mean. If I can buy at most y gold coins, then you can select a bigger one between Y and x + 8888, and the bigger one is the maximum number of gold coins we can get in the end. 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 in the right? If I give you 10000 people and other gold mines, can you tell me the maximum amount of gold I can dig ?"
"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 began to leave for the news. How confident he was that his two Ministers could give him an accurate answer, because the king 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 satisfaction of the King? Why are they so confident? In fact, they are indeed smarter than the king, because they have learned something from the king, which makes them confident.
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 require 1000 people to mine, and 7000 gold is available ".
Because 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, you can tell me that you can dig at most: "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 subordinate continued to ask another person: "If I give you 8500 people and all the gold mines except 8th and 9th, can you tell me how much gold you can dig?"
"If both of them can answer my questions, will the problem the king handed over to me be solved? Hahaha !"
Because the king gave 10000 people to his right subordinates, the king's right subordinates 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 right men of the King continued to ask another person he called: "If I give 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 confidence because they also learned the same thing from the two Ministers, the two Ministers who think they are as smart as themselves 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?
How do you think the four people called by the Minister have solved 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. More people have found more people to solve this problem, while some do not need to ask another 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 the help of 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 number of gold that can be dug from 0th gold mines, if this Z individual is not enough to mine 0th gold mines, the maximum number of gold that can be dug is 0, because the only gold mine is not enough manpower to mine. Let's applaud those who can get the answer accurately without the help of others. This is the legendary bottom-layer working people!
The story is paused here. Now we can analyze the story again to give us a rational understanding of dynamic planning.
Sub-questions:
The king needs to determine the maximum amount of gold to be mined 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 as long as the two ministers can answer the correct answer (for the maximum number of gold that can be mined, that is, the optimum, that is, the correctness ), in addition, his smart judgment will surely get the final correct answer. We call the optimal sub-structure after the optimal choice of this seed problem ".
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 mined. We refer to this parent problem and subproblem as "overlapping subproblems" in essence ". However, the difference in the problem is often the parameter transmitted between the quilt problem, such as 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 above, can this problem be solved? Never! We call this seed problem a boundary without having to raise sub-problems. 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 think about their own problems, they will not care about how the other party calculates how to mine gold because they know, the King will only choose one of two people as the final solution, and the other person's solution will not be implemented, so the decision of one person has no impact on the decision of another person. In this case, when selecting a subproblem, the selected subproblem does not affect each other. This is called "subproblem independence ".
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 of dynamic planning. Now we can write the equation corresponding to this problem, if G [I] is used to represent the number of gold that can be mined from the I-th Gold Mine, P [I] is used to represent the number of people needed to dig the I-th gold mine. The F (p, I) function is used) indicates that when there are P individuals and numbers 0, 1, 2, 3 ,...... What is F (p, I) equal to the maximum amount of gold that can be obtained from the I gold mine? Or what is the transfer equation of F (p, I?
The answer is:
When I = 0 and P> = P [I], F (p, I) = g [I]
When I = 0 and P <p [I], F (p, I) = 0
When I! = 0 F (p, I) = f (p-p [I], I-1) + G [I] and F (p, I-1) in the larger, the first two formulas correspond to the "boundary" of the dynamic planning, and the last one corresponds to the "optimal sub-structure" of the dynamic planning. Please understand them before proceeding.
---- Section 2 ---- advantages of Dynamic Planning --------
Now, I assume that you have figured out why dynamic planning is the correct method, but why do we need dynamic planning? Please continue to enjoy this story:
After the King learned that his two men used the same method as him to solve the problem he had given to them, not only did he not think that his two ministers were lazy, but he was very happy because he knew that, his minister will certainly find more people to solve the problem, and more people 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 has some concerns, because he does not know how many people will be involved in this project. If there are too many people to help him solve this problem, it would be too costly. "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 it can be simply considered a combination problem, that is to say, it is better to select several gold mines from ten gold mines to find out which combination gets the most gold. Can you tell me the total number of combinations ?"
"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 ......, In each case, if I give it to a person 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 expected the minor to give it a better answer and asked, "Xiao Cai, can you tell me how many people I need in my way? In fact, I also 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 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, and the other needs to know how much gold can be mined from 1000 people and 3 gold mines, then they can ask the same person, instead of looking for different people to waste manpower."
The king thought and said, "Well, it makes sense. If the problem is the same, you don't need to ask two different people. That is to say, only one person is required for a different problem, so how many different problems are there?"
"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, in fact, we need far less manpower than this number, because not every problem will occur. Maybe we only need one or two hundred manpower to solve this problem, this is mainly related to the number of gold miners." Then the minor answered immediately.
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, every question will only be calculated once, if you do not forget it, dynamic planning has no advantages.
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, if the problem is in the 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.
However, if dynamic planning is used, there are only 1000*100 = 100000 different problems at most, which is obvious in comparison with 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, the total number of problems is about 100.
Informal, we can easily get the time required for dynamic planning. If questioncount has 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 a maximum of p * n subproblems (where p is the total number of gold mines used for mining and N is the total number of gold mines). Therefore, questioncount = p * n, just as the king needs to consider whether to use the result of the left or the result of the right, each problem is faced with two choices, so choosecount = 2, so the program running time is t
= O (questioncount * choosecount) = O (p * n). Don't forget that the actual time required is less than this value. It varies depending on the actual 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 solution to the problem meets 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 also need to 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 think that the whole mining 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): mining and not mining. When the last step is determined, the last step is to determine 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 may first consider what the process is like and then consider how the last step of the process is selected, we usually need to construct a process by ourselves, such as 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 process corresponding to 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 different points in the sub-problem as parameters.
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, If we select one of the multiple sub-problems as the implementation scheme, we will not implement 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 a total of 3 volumes. Each volume is priced at 60 yuan. The bookstore has launched an activity to engage in promotions. The activity is as follows:
If you purchase one of these volumes separately, you can enjoy 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.
If James wants to purchase 1st-Volume X, 2nd-volume Y, and 3rd-volume Z, what is the minimum cost? (X, y, and z are three known integers ).
Of course, this question can be solved without dynamic planning. But now we want to learn dynamic planning. So, please think about how to use 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), and finally buy all the required books.
2. In the last step, I will definitely select one of the seven purchase schemes. Therefore, 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". The three purchase volumes in the question are set as parameters, they are I, j, and K.
4. It does.
5. the boundary is to buy all the books at a time. Please consider the solution yourself.
6. A maximum of seven solutions can be selected at a time, and multiple solutions will not be implemented at the same time. Therefore, the selection of solutions does not affect each other. Therefore, "sub-problems are independent ".
7. I can use minmoney [I] [J] [k] To save the minimum money required to purchase 1st-Volume I, 2nd-volume J, and 3rd-volume K.
8. There are a total of x * y * Z problems. Each problem has 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 by 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, J-1, k-1)
S7 = (60 + 60 + 60) * 0.85 + minmoney (I-1, J-1, k-1)
---- Section 6 ---- code reference ------
The following provides the source code of the goldmine program to help readers understand and provide test data for everyone to practice.
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 mine, and the second is the total number of gold mines found.
In the input file, rows 2nd to n + 1 have two numbers in each row, and the two numbers in row I represent the number of gold miners in the I-1 and the number of gold available.
Only one integer is output, indicating the maximum number of gold.
Input example:
100 5
77 92
22 22
29 87
50 46
99 90
Output example:
133
The reference code is as follows:
/* Subject: 01 algorithm used by the gold mine model of backpack: Dynamic Planning Algorithm Run Time: O (p * n) [p is the number of people, n is the number of gold mines] */# include <iostream> # include <fstream> using namespace STD; # define max (A, B) A> B? A: bconst int max_n = 100; // maximum number of gold mines supported by the program. Const int max_p = 10000; // maximum number of workers supported by the program int N; // number of gold mines int ptotal; // The number of people who can be used to dig gold int pneed [max_n]; // The number of people needed for each gold mine int G [max_n]; // The number of gold that each gold mine can mine. Int maxg [max_p] [max_n]; // maxg [I] [J] stores the maximum number of gold that I can obtain from the first J gold mines. If it is equal to-1, it indicates unknown // initialized data void Init () {int I; CIN> ptotal> N; for (I = 0; I <n; I ++) CIN> pneed [I]> G [I]; for (I = 0; I <= ptotal; I ++) for (Int J = 0; j <n; j ++) maxg [I] [J] =-1; // when it is equal to-1, it indicates unknown [corresponding to "do Memorandum]} // obtain the maximum number of gold that can be obtained when only p persons and the first I gold mines exist, note that the "first number" is also the int getmaxg (int p, int I) numbered from 0 {// declare the maximum number of gold returned int retmaxg; // if this problem has been computed ["Memorandum" in the corresponding dynamic plan] If (maxg [p] [I]! =-1) {// obtain the saved value retmaxg = maxg [p] [I];} else if (I = 0) // if there is only one gold mine, [corresponding to the "boundary" in the dynamic planning] {// when the number of people given is sufficient to mine this gold mine, if (P> = pneed [I]) {// The maximum value is the gold count of this gold mine. retmaxg = G [I];} else // otherwise this unique gold mine cannot be mined {// The maximum value obtained is 0 gold retmaxg = 0 ;}} else if (P> = pneed [I]) // If the given person is sufficient to mine the gold mine [corresponding to the "optimal sub-structure" in the dynamic plan] {// mining and not mining are considered, maximum retmaxg = max (getmaxg (p-pneed [I], I-1) + G [I], getmaxg (P, I-1 ));} else // otherwise, the user is not enough to mine this gold mine [corresponding to the "optimal substructure" in the dynamic plan] {// only consider the case of not mining retmaxg = getmaxg (p, i-1);} // create a memorandum maxg [p] [I] = retmaxg; return retmaxg;} int main () {// initialize the data Init (); // output the maximum number of gold that can be obtained from the given ptotal and N gold mines. The reminder number starts from 0 again, so the last gold mine number is n-1 cout <getmaxg (ptotal, n-1) <Endl; return 0 ;}