01 Backpack Java Implementation (beginner to proficient)

Source: Internet
Author: User

One, what is 01 backpack

  01 The backpack is a few pieces of m items in the space W backpack, each item volume of W1,W2 to WN, corresponding to the value of P1,P2 to PN. 01 Backpack is the simplest problem in knapsack problem. 01 Backpack Constraints are given a few items, each item has and only one , and has the right value and volume of two attributes. in the 01 knapsack problem, because there is only one for each item, you only need to consider selecting and not selecting two cases for each item. If you do not choose to put it in your backpack, you do not need to process it. If you choose to put it in your backpack, because it's unclear how much space you've placed before, you need to enumerate all the things that might occupy your backpack space after putting it in your backpack.

Two, 01 backpack formula

01 backpack State transition equation  f[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 represents the value of article I items. decision: To maximize the total value of the items in the backpack, should I put the items in the backpack? We only need to initialize the first row and the first column of data, we can calculate all the results according to the state transition equation! We only need to initialize the first row and the first column of data, we can calculate all the results according to the state transition equation! We only need to initialize the first row and the first column of data, we can calculate all the results according to the state transition equation!
three, illustrated 01 backpack


With this picture and the formula summarized above, we can clearly understand the 01 knapsack algorithm (reverse)!!!!!!!

    • E2 Cell: When there is only one item E, the capacity of the package is 2 o'clock and cannot be loaded, so the maximum value is 0
    • A8 cell: Items include a, B, C, D, E, the capacity is 8 o'clock, f[i-1,j]=f[b,8]=9,f[i-1,j-wi]+pi=f[b,6]+6=9+6=15, the maximum value of two cases, so the maximum value here is 15
iv. Java Code Implementation 01 backpack ( Positive-order calculation!!!!!!! )First look at the result diagram:

 

Code section:
1  Packagecom.niu.test;2 3 /**4 * Created by Administrator on 2017/10/3.5  */6  Public classZeroonepag {7 8     /**9 * W: Represents the weight of the itemTen * P: Representing the value of the item One * Pag: Represents the size of the backpack A      * @paramargs -      */ -      Public Static voidMain (string[] args) { the         int[] w={3,5,2,6,4}; -         int[] p={4,4,3,5,3}; -         intPag=12; - table (pag,w,p); +  -     } +  A     Static voidTableintPagint[] W,int[] p) { at         intn=w.length; -         //Initialize the f[0][1~pag]=0;f[1-n][o]=0 so that other values can be calculated according to the formula based on the initialization!  -         int[] f=New int[N+1] [Pag+1]; -         //01 knapsack algorithm F[i][j]=max{f[i-1][j-w[i]]+p[i] (J>=w[i],f[i-1][j])} -         //the core of the algorithm: calculation based on the formula!!!!!  -          for(inti=1;i<=n;i++){ in              for(intj=1;j<=pag;j++){ -                 if(j>=w[i-1]){ toF[i][j]=math.max (f[i-1][j-w[i-1]]+p[i-1],f[i-1][j]); +}Else{ -F[i][j]=f[i-1][j]; the                 } *             } $         }Panax Notoginseng         //output Table -          for(inti=0;i<=n;i++){ the              for(intj=0;j<=pag;j++){ +System.out.print (f[i][j]+ "\ T"); A             } theSystem.out.print ("\ n"); +         } -         //Maximum output value $System.out.println ("The maximum value that can be installed is:" +F[n][pag]); $         //output-Loaded items -System.out.print ("Loaded Item Number:"); -         intx=pag; the          for(inti=n;i>0;i--){ -             if(f[i][x]>f[i-1][x]) {WuyiSystem.out.print (i+ "\ T"); theX-=w[i-1]; -             } Wu         } -     } About}

01 Backpack Java Implementation (beginner to proficient)

Related Article

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.