Solving the Knapsack Problem Using Dynamic Programming

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 )

f[i,j] represents the maximum value that can be obtained by selecting several items from the first i items and placing them in a knapsack with a load of j.
Pi represents the value of the ith item.
Decision: In order to maximize the total value of the items in the knapsack, should the ith item be put in the knapsack?

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

[java] view plain copy
  1. public function get01PackageAnswer(bagItems:Array,bagSize:int):Array  
  2. {  
  3.     var bagMatrix:Array=[];  
  4.     var i: int ;  
  5.     var item:PackageItem;  
  6.     for(i=0;i<bagItems.length;i++)  
  7.     {  
  8.         bagMatrix[i] = [0];  
  9.     }  
  10.     for(i=1;i<=bagSize;i++)  
  11.     {  
  12.         for(var j:int=0;j<bagItems.length;j++)  
  13.         {  
  14.             item = bagItems[j] as PackageItem;  
  15.             if(item.weight > i)  
  16.             {  
  17.                 //i backpack cannot transfer item  
  18.                 if(j==0)  
  19.                 {  
  20.                     bagMatrix[j][i] = 0;  
  21.                 }  
  22.                 else  
  23.                 {  
  24.                     bagMatrix[j][i]=bagMatrix[j-1][i];  
  25.                 }  
  26.             }  
  27.             else  
  28.             {  
  29.                 //The sum of the value after loading the item into the backpack  
  30.                 var itemInBag: int ;  
  31.                 if(j==0)  
  32.                 {  
  33.                     bagMatrix[j][i] = item.value;  
  34.                     continue;  
  35.                 }  
  36.                 else  
  37.                 {  
  38.                     itemInBag = bagMatrix[j-1][i-item.weight]+item.value;  
  39.                 }  
  40.                 bagMatrix[j][i] = (bagMatrix[j-1][i] > itemInBag ? bagMatrix[j-1][i] : itemInBag)  
  41.             }  
  42.         }  
  43.     }  
  44.     //find answer  
  45.     var answers:Array=[];  
  46.     var curSize:int = bagSize;  
  47.     for(i=bagItems.length-1;i>=0;i--)  
  48.     {  
  49.         item = bagItems[i] as PackageItem;  
  50.         if(curSize==0)  
  51.         {  
  52.             break;  
  53.         }  
  54.         if(i==0 && curSize > 0)  
  55.         {  
  56.             answers.push(item.name);  
  57.             break;  
  58.         }  
  59.         if(bagMatrix[i][curSize]-bagMatrix[i-1][curSize-item.weight]==item.value)  
  60.         {  
  61.             answers.push(item.name);  
  62.             curSize -= item.weight;  
  63.         }  
  64.     }  
  65.     return answers;  
  66. }  



PackageItem class
[java] view plain copy
  1. publicclass PackageItem   
  2. {  
  3.     public var name:String;  
  4.     public var weight:int;  
  5.     public var value:int;  
  6.     public function PackageItem(name:String,weight:int,value:int)  
  7.     {  
  8.         this.name = name;  
  9.         this.weight = weight;  
  10.         this.value = value;  
  11.     }  
  12. }  

test code
[java] view plain copy
  1. var nameArr:Array=['a','b','c','d','e'];  
  2. var weightArr:Array=[2,2,6,5,4];  
  3. var valueArr:Array=[6,3,5,4,6];  
  4. var bagItems: Array = [];  
  5. for(var i:int=0;i<nameArr.length;i++)  
  6. {  
  7.     var bagItem:PackageItem = new PackageItem(nameArr[i],weightArr[i],valueArr[i]);  
  8.     bagItems [i] = bagItem;  
  9. }  
  10. var arr: Array = ac.get01PackageAnswer (bagItems, 10 ); 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325722192&siteId=291194637