Dynamic planning 0-1 knapsack problem

Source: Internet
Author: User

01 knapsack problem, is used to introduce dynamic programming algorithm the most classic example, this article strives to do in the simplest way, the least formula to explain the 01 knapsack problem thoroughly.
(The important reason to be able to push from the bottom up is: optimal substructure + no-no-effect)

01 The state transition equation of the backpackF[i,j] = max{F[i-1,j-wi]+pi (j >= Wi), F[i-1,j]}F[I,J] Indicates the maximum value that can be achieved by selecting several pieces in a backpack loaded with J in the first I-item. Pi denotes the value of the article I item. Decision: To maximize the total value of the item in the backpack, should I put the item in the backpack?

Title Description:

There are five items numbered a,b,c,d,e, their weight is 2,2,6,5,4, their value is 6,3,5,4,6, now give you a load-bearing of 10 of the backpack, how to make the backpack loaded items have the greatest value of the sum?

value 2 4 6 8 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 looking at the rules, even if you understand the 01 backpack dynamic programming algorithm.

The first thing to make clear is that the table is bottom-up, from left to right.

For the sake of convenience, using E2 cell to represent e Row 2 column cell, the meaning of this cell is used to indicate that only the item E, there is a load bearing 2 of the backpack, then the maximum value of this backpack is 0, because the weight of the E-item is 4, the backpack can not be loaded.

For D2 cell, it means that only the item e,d, the load-bearing 2 backpack, the maximum value that can be loaded, is still 0, because the item e,d is not the backpack can be installed.

In the same vein, c2=0,b2=3,a2=6.

For the 8 load-bearing backpack, a8=15, how to draw?

According to the state transition equation of 01 knapsack, we need to examine two values,

One is f[i-1,j], for this example is the value of B8 9, the other is f[i-1,j-wi]+pi;

Over here

F[I-1,J] says I have a backpack that weighs 8, the maximum value that the backpack can fit when only four pieces of the item b,c,d,e are optional.

F[I-1,J-WI] means that I have a backpack that weighs 6 (equal to the weight of the current backpack minus item a), the maximum value that the backpack can fit when only four pieces of the item b,c,d,e are optional.

F[I-1,J-WI] refers to cell B6, the value of 9,pi refers to the value of a item, 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 in a load-bearing 8 backpack

Package com.algorithm;
Dynamic planning solves 01 knapsack problem

/*
* Test Data
* The backpack can accommodate up to 10 kilograms of items, existing 3 items,
* Weight and value are respectively
* 3, 4
* 4, 5
* 5, 6
*/
public class Backpack_01 {

private static int capacity = 10;//Backpack capacity
private static int num_items = 3;//Item Quantity

public static void Main (string[] arg) {
Int[] Weigth ={3,4,5};
Int[] Value ={4,5,6};

int[][] max = new int[num_items+1][capacity+1];
Initial array
for (int i=0;i<=num_items;i++) {
for (int j=0;j<capacity;j++) {
max[i][j]=0;
}
}
//
for (int i=1;i<=num_items;i++) {
for (int j=1;j<=capacity;j++) {
if (J<weigth[i-1]) {
When the current item is not loaded, the maximum value of the backpack is equal to the original
MAX[I][J]=MAX[I-1][J];
}else{
Whether the current item is loaded depends on the maximum value between the two
Max[i][j]=math.max (Max[i-1][j-weigth[i-1]]+value[i-1], max[i-1][j]);
}
}
}
SYSTEM.OUT.PRINTLN ("Backpack Maximum value:" +max[num_items][capacity]);
}
}

Dynamic planning 0-1 knapsack problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.