4.1 Algorithm 1- traditional 0-1 Knapsack Algorithm | |
Input | Witem : Weight of each item ,Pitem: Profit of each item, C : Capacity |
Output | SP : array of subproblem to find an optimal solution, X : 0 or 1 select item or not |
Step 1 | SPij ← {0, 0} // item : number of item , TN :total number of item |
Step 2 | for item = 1 to TN do // Capcount: : Capacity count For Capcount ← 0 to C do If Witem ≤ C then // SP : subproblem If Pitem + SPD[item -1, Capcount - Witem] > SP [item-1, SPD[item -1, Capcount] ← Pitem + SP[i - 1, j - Witem] Else SP [item , Capcount] ← SP [item -1, Capcount] // Witem > C Endif End for // Capcount End for // item |
Step 3 | item← TN, Capcount ← C |
Step 4 | while item and Capcount > 0 If SP [item - 1 , Capcount] ≠ SP [item , Capcount] then item ← item-1, Capcount ← Capcount- Witem Xtem = 1 else item ← item-1 Xtem = 0 end if Endwhile |