[Dynamic planning] 01 backpack and full backpack

Source: Internet
Author: User
01 backpack (the status of each item is "select" or "do not select". You can only select one package at most ):

1. In the traditional two-dimensional array, the weight of the I-th item is w [I], and the value is v [I].

Dp [I] [j] saves the first I items (the status of each item is selected or not). When the backpack capacity is j, maximum value

Two cases:
I. current backpack capacity j <I weight, I backpack is certainly not optional, can not put down, there are dp [I] [j] = dp [i-1] [j]
II. when the size of the current backpack is j> = the weight of the I-part, the I-part backpack can be selected and can be placed. However, because we need to consider the maximum value, there are two options at this time, optional or not. We need to take the maximum value. The state transition equation is:

Dp [I] [j] = max (dp [i-1] [j], dp [I-w [I] [j]). dp [i-1] [j] is the I backpack is not selected, dp [i-1] [j-w [I] is the I backpack choose, note that dp [i-1] [j-w [I] has been calculated before, can be used directly, and the saved inside must be the optimal value.

Key code:
For (int I = 1; I <= N; I ++)
For (int j = 0; j <= V; j ++) // Remember that the weight starts from 0, not 1
   {  
If (w [I] <= j)
Dp [I] [j] = max (dp [i-1] [j], dp [i-1] [j-w [I] + value [I]);
Else
Dp [I] [j] = dp [i-1] [j];
    }  


Summary: dp [I] [j] is the first I item selected currently (the status of each item is selected or not ), when the backpack capacity is j, the maximum value is obtained, that is, the local optimal solution. In the process of loop, the previously calculated value must be used, this reflects the idea of dynamic planning.


2. The optimized one-dimensional array is used. The weight of the I-th item is w [I], and the value is v [I].
Dp [j] stores the greatest value when the current capacity of the backpack is j.


Let's take a look at the key code:
For (int I = 1; I <= N; I ++)
For (int j = V; j> = w [I]; j --)
Dp [j] = max (dp [j], dp [j-w [I] + v [I]);
The outer loop is the first item. When the inner loop is j, dp [j] stores the maximum value obtained by selecting the first one. However, it does not rely solely on an item (because the one-dimensional array does not contain [I] in the two-dimensional array, it is in all the previous states, dp [j] is the optimal one. Note that the value of j in the inner loop ranges from large to small. Why? From small to large, assuming that j is small to large when I item is selected, the calculated dp [w [I], dp [w [I] + 1], dp [w [I] + 2]... The subscript is also small to large, but pay attention to the state transition equation dp [j] = max (dp [j], dp [j-w [I] + v [I]); dp [j-w [I] subscript must be smaller than d [j, that is to say, in an internal loop, dp [j-w [I] must be first calculated, assume that the value is obtained when the status of the I-th item is selected. When the value is calculated to dp [j], there is a situation, it is dp [j] <dp [j-w [I] + v [I]. This means that the I-th item has to be selected and has the largest value requirement, however, the dp [j-w [I] I-th item has been selected, if you select another one, this is not the 01 backpack (01 the backpack requires only one item for each item). Therefore, the value of the inner loop j cannot be small to large, from large to small, this ensures that when the outer loop is fixed to I, during the calculation of the inner loop, dp [j-w [I] is calculated after dp [j, here dp [j-w [I] is the value obtained from the Outer Loop 1 to the I-1, that is, the optimum value obtained from the previous I-1 item. This ensures that the I-th item can only be selected 0 times or once in an in-layer loop and cannot be selected multiple times.


Full backpack (you can choose an infinite number of items for each item ):


As explained in the second scenario of the above 01 backpack, we naturally have to write the key code:


For (int I = 1; I <= N; I ++)
For (int j = w [I]; j <= V; j ++)
Dp [j] = max (dp [j], dp [j-w [I] + v [I]);

[Dynamic planning] 01 backpack and full backpack

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.