Code for testing 0-1 knapsack problems

Source: Internet
Author: User
Package WW; public class test {void initial (INT [] weight, int [] value, int N, int capacity) {int [] [] maxvalue = new int [N] [capacity + 1]; int [] Trace = new int [N]; maxvalue [0] [0] = 0; for (Int J = 1; j <= capacity; j ++) {If (weight [0] <= J) maxvalue [0] [J] = value [0]; elsemaxvalue [0] [J] = 0;} zeroonepack (trace, maxvalue, weight, value, n-1, capacity); print (weight, value, maxvalue, Trace, n-1, capacity); system. out. println ("Maximum Weight:" + m Axvalue [n-1] [capacity]); system. out. println ("corresponding to the selected item: 0 indicates that this item is not selected, 1 indicates that this item is selected"); For (int K = 0; k <= n-1; k ++) system. out. println (trace [k]);} int zeroonepack (INT [] Trace, int [] [] maxvalue, int [] weight, int [] value, int I, Int J) {int X1 = 0, X2 = 0; if (I> = 1 & J> = 0) {x1 = zeroonepack (trace, maxvalue, weight, value, I-1, j); x2 = (J-weight [I]) <0? 0 :( zeroonepack (trace, maxvalue, weight, value, I-1, J-weight [I]) + value [I]); If (x1> x2) return (maxvalue [I] [J] = x1); elsereturn (maxvalue [I] [J] = x2 );} else if (I = 0 & J> = 0) return maxvalue [0] [J]; return 0;} void print (INT [] weight, int [] value, int [] [] maxvalue, int [] Trace, int I, Int J) {// ordered output. If the result is 0, this item is not selected. If (I = 0 & J> = 0 & J-weight [I]> = 0) trace [0] = 1; else if (I> = 1 & J> = 0) {If (maxvalue [I-1] [J] <maxvalue [I-1] [J-weight [I] + value [I]) trace [I] = 1; print (weight, value, maxvalue, Trace, I-1, J-weight [I]);} public static void main (string ARGs []) {int [] value = {6, 10, 12 }; int [] Weight = {1, 2, 3}; int n = value. length; Test A = new test ();. initial (weight, value, N, 5); system. out. println ();}}

The item number starts from 0.

The item number starts from 0. It is relatively easy to obtain the maximum value, but it is a little difficult to output the final maximum value which items are selected. No matter whether the item number is from 0 or from 1, it is the same for this problem. The Recursion Formula for the 0-1 knapsack problem is as follows:

In M [I] [J], I indicates the I-th item, and J indicates the maximum weight that can be accommodated, the same number of items in the Max in the above formula indicates that the I-th item is not put into the package with a capacity of J, and the latter indicates that the item is to be put. When the selected item corresponding to the maximum value is output at the end, the method used is the same as the method used to solve the maximum value, which is also recursive. The entire problem is solved recursively from top to bottom.

The dynamic planning solution for the 0-1 knapsack problem is similar to the assembly line tuning problem in the introduction to algorithms. For an online equipment, the similarities between the two are for one point (assembly point and item) select either or not. That is to say, the final result is similar to that of 000111101010011. This is different from the dynamic planning of Matrix Multiplication. The dynamic planning of matrix multiplication is continuous, that is, there is no such disconnection, it means that all I is equal to J to stop. Another feature is that they all need to apply recursion. The parent problem always corresponds to several possible sub-problems, and then finds an optimal problem from these sub-problems, instead of dividing the problem into two subproblems through different Split points K like matrix multiplication, then finding the k that maximizes the solution as the optimal solution of the parent problem. From these two points, we can first determine a single position for the dynamic planning problem based on the solution of the problem, which will help us to find the recursive formula.

In addition, when the optimal solution corresponds to an item, that is, the output path result, I think there are two ways, one is to record when solving the maximum value, then, recursively output the Path results. One is to use the target values in the process of solving the maximum value in recursive output without recording. For example, M [I] [J] Here to help solve the results. Of course, I think it may be better to deal with this backpack problem in the latter way. I have not yet thought about how to record it in the computation, and then use recursion to output the path result.

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.