The knapsack problem of dynamic planning

Source: Internet
Author: User

Knapsack problem is a classical algorithm problem, can be solved by dynamic programming, greedy method, branch boundary method and so on. Description of the problem: There are n items, numbered "x", "N", of which the item I is of the weight of the WI value Vi, has a capacity of W backpack. In terms of capacity, how to select items, you can get the maximum value. (For the sake of simplicity, assume that the weight of the item Wi and value VI are positive)

Today, the main point is 0, 1 knapsack problem, the solution is dynamic planning. Of course, there are two other kinds of questions to be introduced.

Problem Analysis:

Using dynamic programming to solve the problem first to effectively find the sub-problem, you can get the solution of the original problem through this sub-problem, usually the essence of the sub-problem is the same as the original problem, but the scale of the reduction, that is, the sub-problem and the original problem can have the same representation, The problem can be solved by shrinking the scale (which will usually have a limit) to find the solution to the sub-problem.

The question to solve is the maximum value of the items that can be taken with the backpack. Define m[i,W] as: The maximum value of the backpack with the 1th,, 2, 3, and I items loaded with quality <=w.

m Analysis of the value of [i,W]:

1), the quality of the backpack is W, there is no item, so its value is 0;

2), the backpack quality is 0, so there is no way to install anything, no matter what the front i is, the total value of 0;

For any of the article I items, there are two cases, put in a backpack or not put. Do not first item if then:

3) because the weight of item I is greater than the capacity of the backpack, it cannot be placed.

If. So

4)

For item I, there are two options: if put in a backpack, then m[i,w]=m[i-1,w-wi]+vi. That is, the maximum value of the previous one of I plus my own value. If I do not have the article I, then: M[i,w]=m[i-1,w]. That is, the maximum value of the previous one for I. Since the knapsack problem is ultimately worth the most, the most valuable of these two cases is chosen.

In this problem, the definition sub-problem: m[i,W] for each sub-problem, can be obtained through the above analysis. Through 3), 4) It can be found that each time a child problem, the size of the problem is reduced. Either decrease on w, or decrease on I. The size of the final question will be reduced to m[i,0] and m[0,w]. Both values are 0, as long as the inverse thinking back, you can gradually get the solution of the problem.

Algorithm description

Algorithm implementation Java version
 Public classknapsack {Static inttotalweight=Ten;//the capacity of the backpack    Static int[] w=New int[]{0,2,2,6,5,4};//The total weight of the item, where the No. No. 0 bit is not used    Static int[] v=New int[]{0,6,3,5,4,6}; Static int[] x=New int[W.length]; Static int[] matri=New int[W.length] [totalweight+1];  Public Static voidpackage0_1 () { for(intj=0; j<=totalweight;j++) {matri[0][j]=0;//The first line is initially 0        }         for(intj=1; j<=v.length-1; j + +) {matri[j][0]=0;//The first column is initially 0        }                 for(intI=1; I <=w.length-1; i++) {             for(intj =1; j<=totalweight; J + +) {                if(j<W[i]) {Matri[i][j]=matri[i-1][j]; }Else{Matri[i][j]=math.max (matri[i-1][j],matri[i-1][j-w[i]]+V[i]); }            }        }    }         Public Static voidAnswer () {intj=Totalweight; inti;  for(i=w.length-1; i>=1; i--) {            if(matri[i][j]==matri[i-1][j]) {X[i]=0; }Else{X[i]=1; J-=W[i]; }} x[0]=0; }     Public Static voidMain (string[] args) {package0_1 ();        Answer ();  for(inti =0; i < x.length; i++) {System. out. println (X[i]); }    }}

The knapsack problem of dynamic planning

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.