Algorithm Learning notes (ii) retrospective solution of--01 knapsack problem __ Algorithm

Source: Internet
Author: User
Backpack problem, I believe you reader must have heard.         The author simply describes the knapsack problem: given a backpack and n items, the capacity of the backpack for C, the weight of the article I is w[i], the value of V[i] (1<=i<=n); Ask for those items, can make the most value. Analysis: Obviously, each item is simply two choices: loading and not loading the backpack. If the loading is indicated by state 1 and is not loaded with state 0, then a binary tree can be formed. There are n layers, so you can find the optimal solution by traversing the search from the first layer, and the total weight of the loaded items is not greater than C. The idea is quite simple, other than to say, directly on the code:

public class Demo1 {private static int c = 10;//backpack capacity private static int[] w= {0,2,2,6,5,5};//weight of each object: 5 objects private Stati C Int[] v = {0,6,3,5,4,6}; The value of each object is private static int[] x= new int[6]; The selection of each object is stored private static int bestcp = 0; The current optimal solution private static int[] Bestcparr = new Int[6]; private static int count = 0; private static int n = 5; Number of objects public static void main (string[] args) {backtrack (1, 0, 0); for (int i =0 i < bestcparr.length; i++) {System. Out.print (Bestcparr[i]); } System.out.println (""); System.out.println (BESTCP); System.out.println ("The tree has" + count + "traversal mode"); /** * * @param i: Find the first I goods (I starting from 1 index) * @param CV: value in current package * @param CW: Current package weight/private static void backtrack (int i, int c W, int cv) {count++; if (i > N) {if (CV > bestcp) {bestcp = CV; for (int j = 0; J < 6; j) {bestcparr[j]= x[ J]; }}else{//Start traversal, if k=0, means not to install this item, k=1, means to install this item for (int k = 0; k <= 1; k++) {x[i] = k; if (CW + w[i] <= c) {CV = V[i] * k; CW +=w[i] * k; Backtracki + 1, CW, CV); CV-= v[i] * k; CW-=w[i] * k; } } } } }we know that the backtracking method is not very efficient. Because backtracking, by its very nature, is still a poor one. But it provides a poor idea: backtracking. Indeed, the example in the above code consists of only 5 objects, in other words, the two-forked tree is only 5 layers. But when the items have 10, 20, or even 100 words, the composition of the two fork tree is how huge. (I think unimaginable depth is 100 of the two Fork tree). Therefore, on the basis of the above code, it is necessary to do an optimization of the algorithm. After watching someone else's blog on the Internet, I have a general idea: that is, in the process of constantly traversing the Zuozi (that is, constantly loading the items), if there is no way to load the next item, then you need to go through the right subtree. But if,If the remaining capacity is filled with excess capacity (if greater than 0 and less than the weight of the item), in the case of the value of the unit weight multiplied by the residual capacity, the total value obtained is smaller than the current optimal solution, then the right subtree is completely unnecessary to traverse, and needs to be cut off directly. This achieves the goal of optimization. Nonsense not much to say, directly on the code:

/**

* The upper bound function of this algorithm is defined in this way. First, the bubble method is used to sort, according to the unit value right high to low start order.

* In this case, you can guarantee that the first load is cost-effective (their invention, do not spray) the optimal. And in the upper bound function, so is the remainder

* The maximum value that the capacity can hold in the right subtree (where the backpack capacity is allowed), once it is less than the current optimal solution, then

* No need to continue traversing the right subtree

*

*/

Public class Demo2 {private static int n;//items number private static double c;//backpack capacity private static double[] v = new DOUBL e[100];//value of each item private static double

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.