The 01 knapsack problem is the most classic example used to introduce the dynamic programming algorithm. This article strives to explain the 01 knapsack problem in the simplest way and with the least formula.
01 The state transition equation of the backpack f[i,j] = Max{ f[i-1,j-Wi]+Pi, f[i-1,j] } ( j >= Wi )
Topic description:
Suppose there are 5 treasures a, b, c, d and e (not 5 treasures) in the cave, their weights are 2, 2, 6, 5, 4, and their values are 6, 3, 5, 4,6, now I will give you a backpack with a load-bearing capacity of 10. How to pack a backpack can take away the most wealth.
There are five items numbered a, b, c, d, e, their weights are 2, 2, 6, 5, 4, and their values are 6, 3, 5, 4, 6, and now give You have a backpack with a load capacity of 10, how to make the items loaded in the backpack have the greatest total value?
name | weight | value | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
a | 2 | 6 | 0 | 6 | 6 | 9 | 9 | 12 | 12 | 15 | 15 | 15 |
b | 2 | 3 | 0 | 3 | 3 | 6 | 6 | 9 | 9 | 9 | 10 | 11 |
c | 6 | 5 | 0 | 0 | 0 | 6 | 6 | 6 | 6 | 6 | 10 | 11 |
d | 5 | 4 | 0 | 0 | 0 | 6 | 6 | 6 | 6 | 6 | 10 | 10 |
e | 4 | 6 | 0 | 0 | 0 | 6 | 6 | 6 | 6 | 6 | 6 | 6 |
As long as you can manually fill out the above table by finding the rules, you can understand the dynamic programming algorithm of the 01 knapsack.
First of all, it must be clear that this table is generated from bottom to top and from left to right.
For the convenience of description, the cell e2 is used to represent the cell in row 2 of column e. The meaning of this cell is to indicate that when there is only item e, there is a backpack with a load-bearing capacity of 2, then the maximum value of this backpack is 0, because e The weight of the item is 4 and cannot be backpacked.
For cell d2, it means that when there are only items e and d, the maximum value that can be loaded into a backpack with a load-bearing capacity of 2 is still 0, because neither item e nor d can be loaded by this backpack.
Similarly, c2=0, b2=3, a2=6.
For a backpack with a load-bearing capacity of 8, how does a8=15 come out?
According to the state transition equation of the 01 backpack, two values need to be investigated,
One is f[i-1,j], which for this example is the value 9 of b8, and the other is f[i-1,j-Wi]+Pi;
it's here,
f[i-1,j] means that I have a backpack with a load-bearing capacity of 8. When only four items b, c, d, and e are optional, the maximum value that this backpack can fit
f[i-1,j-Wi] means that I have a backpack with a weight of 6 (equal to the current backpack weight minus the weight of item a), when only four items b, c, d, and e are optional, this backpack Maximum value that can be loaded
f[i-1,j-Wi] refers to cell b6, the value is 9, and Pi refers to the value of item a, that is, 6
Since f[i-1,j-Wi]+Pi = 9 + 6 = 15 is greater than f[i-1,j] = 9, item a should be put into a backpack with a load capacity of 8
Below is the code for actionscript3
- public function get01PackageAnswer(bagItems:Array,bagSize:int):Array
- {
- var bagMatrix:Array=[];
- var i: int ;
- var item:PackageItem;
- for(i=0;i<bagItems.length;i++)
- {
- bagMatrix[i] = [0];
- }
- for(i=1;i<=bagSize;i++)
- {
- for(var j:int=0;j<bagItems.length;j++)
- {
- item = bagItems[j] as PackageItem;
- if(item.weight > i)
- {
- //i backpack cannot transfer item
- if(j==0)
- {
- bagMatrix[j][i] = 0;
- }
- else
- {
- bagMatrix[j][i]=bagMatrix[j-1][i];
- }
- }
- else
- {
- //The sum of the value after loading the item into the backpack
- var itemInBag: int ;
- if(j==0)
- {
- bagMatrix[j][i] = item.value;
- continue;
- }
- else
- {
- itemInBag = bagMatrix[j-1][i-item.weight]+item.value;
- }
- bagMatrix[j][i] = (bagMatrix[j-1][i] > itemInBag ? bagMatrix[j-1][i] : itemInBag)
- }
- }
- }
- //find answer
- var answers:Array=[];
- var curSize:int = bagSize;
- for(i=bagItems.length-1;i>=0;i--)
- {
- item = bagItems[i] as PackageItem;
- if(curSize==0)
- {
- break;
- }
- if(i==0 && curSize > 0)
- {
- answers.push(item.name);
- break;
- }
- if(bagMatrix[i][curSize]-bagMatrix[i-1][curSize-item.weight]==item.value)
- {
- answers.push(item.name);
- curSize -= item.weight;
- }
- }
- return answers;
- }
PackageItem class
- publicclass PackageItem
- {
- public var name:String;
- public var weight:int;
- public var value:int;
- public function PackageItem(name:String,weight:int,value:int)
- {
- this.name = name;
- this.weight = weight;
- this.value = value;
- }
- }
test code
- var nameArr:Array=['a','b','c','d','e'];
- var weightArr:Array=[2,2,6,5,4];
- var valueArr:Array=[6,3,5,4,6];
- var bagItems: Array = [];
- for(var i:int=0;i<nameArr.length;i++)
- {
- var bagItem:PackageItem = new PackageItem(nameArr[i],weightArr[i],valueArr[i]);
- bagItems [i] = bagItem;
- }
- var arr: Array = ac.get01PackageAnswer (bagItems, 10 );