01 backpack Problems

Source: Internet
Author: User

The basic idea of dynamic planning:

A subproblem is decomposed into recursive solutions, and intermediate results are saved to avoid repeated computation. It is often used to find the optimal solution, and the local part of the optimal solution is also optimal. The solution process generates multiple decision sequences. The next step is always based on the results of the previous step and the bottom-up solution.

The dynamic planning algorithm can be divided into four steps:

1. Describe the structure of an optimal solution, find Sub-problems, and divide the problems.

2. Define the status. A group of values of variables related to sub-problems is often defined as a state. The value of a State is the solution of this subproblem (if there are K variables, K-dimensional arrays are generally used to store solutions in various States, and print the solution process according to the array records .).

3. Find the state transition equation. Generally, the value changes from one status to another.

4. Calculate the value of the optimal solution in the "bottom-up" mode.

5. Build the optimal path from the calculated information. (The optimal solution is a group of solutions for reaching the optimal solution)

Steps 1 ~ 4 is the basis of dynamic programming for solving the problem. If the question only requires the value of the optimal solution, step 5 can be omitted.

Backpack Problems

01 backpack: There are n items and a backpack weighing M. (Each item only has one.) The weight of the I-th item is W [I], and the value is P [I]. Solving which items are loaded into a backpack can maximize the total value.

Full backpack: There are n kinds of items and a backpack with a weight of M, each of which has unlimited pieces available. The weight of type I is W [I], and the value is P [I]. Solving which items are loaded into a backpack can make the total cost of these items not exceed the weight of the backpack, and the total value is the largest.

Multiple backpacks: There are n items and a backpack weighing M. A maximum of N [I] items are available for Type I. The weight of each item is W [I] and the value is P [I]. Solving which items are loaded into a backpack can make the total cost of these items not exceed the weight of the backpack, and the total value is the largest.

01 backpack problems:

This is the most basic problem with a backpack. It is characterized by the ability to choose whether to put or not each item.

Define the state with a sub-question: C [I] [v] indicates the maximum value that a backpack with a weight of M can obtain for the first I item. The state transition equation is:

C [I] [m] = max {C [I-1] [m], C [I-1] [M-W [I] + P [I]}

This equation is very important. Basically all the equations related to the backpack are derived from it. So it is necessary to explain it: "Put the first I items into a backpack weighing m, if you only consider the I-item Policy (put or not put), it can be converted into a problem that only involves the previous I-1 items. Assuming that I items are not placed, then the problem is converted to "pre-i-1 items into a V backpack", the value is C [I-1] [m]; assuming that I items are placed, the problem is converted to "the previous I-1 items are placed in the backpack where the remaining weight is m-W [I ", the greatest value that can be obtained at this time is C [I-1] [M-W [I] plus the value p [I] obtained by placing the I item.

Token Test Data:
10, 3
3, 4
4, 5
5, 6



The C [I] [J] array stores the maximum value of items 1, 2, and 3 selected in sequence.

How can we get the greatest value? Starting from a backpack with a capacity of 0, items on the first day of the first day cannot be put. therefore, set 0 and put 4 in 3 for the backpack capacity. in this way, the size of this backpack is 4, 5, 6 ,.... when 10, the best solution is to put 4. assume that item 1 is placed in a backpack. then let's look at item 2 again. when the size of the backpack is 3, the best solution is the best price solution in the last row. C is 4. when the backpack capacity is 5, the best solution is 5 of its weight. when the backpack capacity is 7, it is obviously 5 plus a value. Who should I add ?? Obviously, when 7-4 = 3, the optimal solution for the previous C3 row is 4. So. In general, the best solution is 5 + 4 for 9. In this way, a row is pushed down. The rightmost decentralized data is the greatest value. (Note that when the size of the 3rd-row backpack is 7, the best solution is not its own 6. but 9 in the last row. this indicates that item 3 is not selected at this time. item 1 and 2 are selected. so 9 .)

Public class pack01 {public int [] [] Pack (int m, int N, int W [], int P []) {// C [I] [v] indicates the maximum value that an I-item can obtain when placed in a backpack with a weight of M. Int C [] [] = new int [n + 1] [M + 1]; for (INT I = 0; I <n + 1; I ++) C [I] [0] = 0; For (Int J = 0; j <m + 1; j ++) C [0] [J] = 0; // For (INT I = 1; I <n + 1; I ++) {for (Int J = 1; j <m + 1; j ++) {// when the weight of an item is J, assume that the weight of Part I (W [I-1]) is smaller than the weight J, C [I] [J] is one of the two conditions: // (1) item I is not in the backpack, So C [I] [J] is C [I-1] [J] value // (2) item I is in the backpack, then the remaining weight of the backpack is J-W [I-1], so C [I] [J] is C [I-1] [J-W [I-1] value plus the value of the current item I if (W [I-1] <= J) {If (C [I-1] [J] <(C [I-1] [J-W [I-1] + P [I-1]) c [I] [J] = C [I-1] [J-W [I-1] + P [I-1]; elsec [I] [J] = C [I-1] [J];} elsec [I] [J] = C [I-1] [J] ;}} return C ;} /*** obtain the optimal solution using the reverse push Method * @ Param C * @ Param w * @ Param M * @ Param N * @ return */Public int [] printpack (int c [] [], int W [], int M, int N) {int X [] = new int [N]; // start with C [N] [m] For (INT I = N; I> 0; I --) {// assume that C [I] [m] is greater than C [I-1] [m], description C [I] [m] This optimal value includes W [I-1] (note here is the I-1, because the C array length is n + 1) if (C [I] [m]> C [I-1] [m]) {x [I-1] = 1; m-= W [I-1];} for (Int J = 0; j <n; j ++) system. out. println (X [J]); Return X;} public static void main (string ARGs []) {int M = 10; int n = 3; int W [] = {3, 4, 5}; int P [] = {4, 5, 6}; pack01 pack = new pack01 (); int C [] [] = pack. pack (m, n, W, P); pack. printpack (C, W, m, n );}}



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.