1. Intro
We humans are a greedy animal, if we give you a certain amount of backpack and a number of different sizes of goods, pack inside the belongings to you, encountered such a good thing you will not miss, hard plug is not necessarily the best way to use the brain, the following teach you how to solve such problems, in order to get more prizes.
2. Application Scenarios
Finding a subset in an object vector satisfies the following conditions:
1 The size of this subset can not be larger than the specified threshold
2 The value of a subset of this item is the largest of all subsets of the vector v that satisfy the condition 1.
3. Analysis
Knapsack problem has many versions, this article only studies the 0/1 version, namely to an object either chooses, abandons, cannot carry on one object to subdivide again the situation. The easiest way to do this is to find all subsets of this vector, it's like finding a subset of power sets, but this traversal is probably not going to be smart for us, and now the TV stations that hold these events are smart enough not only to put things in, but also to specify the operating time, So when you slow into the pouring out of time, I'm afraid it will be early, eventually you may have nothing, this is not what we hope, we need to use a number of strategies: the first time we can be smaller than the size of the backpack to pick one of the items, so as to buy time, Select each one after the first we want it to be optimal, which can save a certain amount of time. Suppose there is such a group of items whose size and value are shown in the following table:
Item
Number |
size |
value |
1 |
2 |
1 |
2 |
3 |
4 |
3 |
4 |
3 |
4 |
5 |
6 |
5 |
6 |
8 |
Give us a backpack with a capacity of 12, let us load the above items, we can use the following method to solve the problem of finding the optimal combination
Create a two-circle array that contains n rows (n is the number of items) and capcity+1 columns
First of all, we choose the first item, because the item 1 size of 2, the first item 1 into the backpack, the size of the item 1 is 2, then cap>=2 can accommodate item1, this time the value of the backpack inside the item1. Value=1, get the following array
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
Next deal with items 1 and a subset of 2, item2 size of 3, then only cap=3 to accommodate the item2, when the cap=3 can accommodate the item2, when the backpack inside the value item2.value=4, and the remaining space of 0, when the cap=4, Can accommodate item2, and the remaining space of 1, can not be item1, when the cap=5 of time to accommodate item1+item2, at this time the value of 1+4 = 5, get the second line
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
0 |
0 |
1 |
4 |
4 |
5 |
5 |
5 |
5 |
5 |
5 |
5 |
5 |
The following analysis of goods three, goods two, a subset of goods, three of the size of 4, when the cap=4 can accommodate item3, but at this time the value of the backpack inside 3, significantly less than the cap=4 value of the previous line (3<4), so cap=4 can not be put in Item3, So the third row 4 position should and the second row of 4 position, when cap=5 can accommodate item3, and the remaining space is 1, and cap=4 situation, copy the value of the same position on the same line, when cap=6, place item3 after 2, can accommodate item1 and item4, The total value of both: 1+3=4<5, so copy the value of the same position on the line, cap=7, can accommodate item2+item3, the total value of 7, greater than >5, so cap=8 when the value of 7,cap=9 is still able to tolerate difficult item3+item2, When Value=7,cap=8, can accommodate item1+item2+item3, and the total value of 8, greater than the same position on the line of value, so cap>=9 time, the total value of 8, the third line:
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
0 |
0 |
1 |
4 |
4 |
5 |
5 |
7 |
7 |
8 |
8 |
8 |
8 |
This logic allows you to get the following two columns, and the last two surround arrays are
0,0,1,1,1,1,1,1,1,1,1,1,1
0,0,1,4,4,5,5,5,5,5,5,5,5
0,0,1,4,4,5,5,7,7,8,8,8,8
0,0,1,4,4,6,6,7,10,10,11,11,13
0,0,1,4,4,6,8,8,10,12,12,14,14
After we get this array, we need to produce a subset of the best items based on this binary array, which means
Starting at Len Line, compares the value of the last line of CAP index position to a value greater than the same position on the previous line, such as comparing the value of position 12 at line fifth (14) with the value of line fourth 12 (13), because 14!=13, so ITEM5 is placed in the optimal set, and the ITEM5 size is 6. So the value on the position of the fourth line cap-6=6 is worth the same position as the previous line because 6! = 5, so item4 can be placed to the optimal set, and the next place to compare is the CAP = 6-item4. Size=6-5=1, the third row position 1 and the second line position 1 the same, so item3 can not be placed to the optimal set, the second row and the first line of the first position of the same value, so item2 can not be placed in, and finally determine whether the item1 should be in the optimal set, ITEM5+ITEM4, The remaining space is 1 and cannot accommodate item1, so the optimal set is {ITEM4,ITEM5};
Combining the above analysis, we can get such a process
1 first to establish a NX (cap+1) of the two-surround array
2 The first line starts with trying to select the first item
3 for future rows, for each capacity 1<=cap<=capacity, first copy the value of the same position on the previous line, if Itemi. Size<=cap and the previous line (Cap-itemi. Size) position on the value with Itemi. Value and (Tempmax) are greater than the copied values, the copied values are replaced with the previous line (Cap-itemi. Size) position on the value with Itemi. Value of the and (Tempmax)
4 after getting the full array, we can determine the optimal set based on the array, starting with the last position, and comparing it to the same position on the previous line, and if it is the same, the index of the row should not be placed in the backpack, otherwise put in the backpack, and start comparing the previous line with The previous row in the current backpack remaining space index values, such as unequal, then the corresponding items can be placed, so, until the second row and the first row of the comparison to complete, and then according to the current backpack surplus capacity and the size of the first item to determine whether the item one can be placed into the backpack
4. SOURCE program
Http://xiazai.jb51.net/201606/yuanma/Knapsack (jb51.net). rar
5. Conclusion
The above is a dynamic programming approach to dealing with this type of knapsack problem, the above article in the brothers also mentioned that using recursive algorithm time complexity problem, that the recursive algorithm is inefficient, this doubt is understandable, but the recursive algorithm has its advantages, many problems can be solved by recursion, I am currently learning to use this algorithm to solve some common problems, for other algorithms, such as this problem can also use greedy algorithm, genetic algorithm, etc. to be better resolved, but this article is not discussed, in the future, we will be able to implement these algorithms and detailed comparison of its pros and cons.