"Algorithmic Learning Notes" 30. Dynamic planning 01 The relationship between backpack and complete backpack

Source: Internet
Author: User

First explain the difference between the 01 backpack and the complete knapsack problem.

01 Backpack: There are N items and a backpack with a capacity of V. The cost of placing the article I is Ci, the value is Wi. The sum of the values is maximized by solving which items are loaded into the backpack. (Can not be filled)

Full backpack: There are N items and a backpack with a capacity of V, each item has unlimited pieces available. The cost of placing the item I is Ci, the value is Wi. Solution: Which items will be loaded into the backpack, so that the cost of these items are not more than the total amount of backpack capacity, and the value of the largest sum.

First, the conclusion:

The optimal solution of both problems is to be solved by DP, the process of implementation is very similar to just in the inner loop of the complete backpack is the order, 01 is reverse.

The reason is that the 01 backpack to ensure that every time before the calculation of the first I items, the analysis of article I items release vessel is not put, according to the value of the former i-1 items, because I can not enter the backpack two times.

The complete backpack must be based on the first I items to consider whether the item I will be added to the current capacity of the W effect.

01 knapsack Problem:

The state transition equation: F[i,v] represents the maximum value that can be achieved in a backpack with a capacity of V, by selecting several of the first I objects.

F[I,V] = Max{f[i−1,v],f[i−1,v−ci] + Wi}

Visible calculation F[i,v], is transferred from the i-1 phase of the two states, one is if the first object is not placed, then the first I-1 object in the backpack for the maximum value of V, one is if I object, in the backpack for v-c[i] The maximum value plus wi. The two bigger one is the result.

This is represented by a two-dimensional array, it is very easy to understand, but the space complex through the large, but also not easy to unify, if you use a one-dimensional array to represent, at each stage when the f[v] if the left value is the current stage, that is, f[i,v], if the right value is divided into two cases, If it has been updated (re-assigned) The current stage is indicated, if it has not been overwritten, it represents the previous stage, that is, f[i-1,v].

If you use a one-dimensional array to represent, F[v] in the left value is expressed in this stage, to ensure that the right side of the f[v] is the previous stage, and F[v-ci] is also the previous stage, so ~ Reverse calculation!!

Then carefully observe this transfer equation, because when calculating f[i,v], the use of f[i-1,v-ci] in order to use a one-dimensional array f[v-ci] to take this value, it must be guaranteed that it has not been updated, that is, it is not yet calculated, and V-ci < v so to reverse the calculation!

This picture can illustrate the problem. Note that each stage (each row) is calculated from c[10] to c[0].

Pseudo code:

F [0..V]←0

For I←1 to N

For V←v to Ci

F[V]←MAX{F[V],F[V−CI]+WI}

Note the order and bounds of the inner loop. Because in stage I, if V is less than the size of the article I, I must not be put in, so it must not be updated, do not have to detect.

Complete Backpack problem:

The biggest difference between the 01 and the backpack is that each infinite repetition can be repeated.

So its state transfer equation can be written as

F[I,V] = Max{f[i−1,v],f[i,v−ci] + Wi}

Indicates that if you do not place the item I the previous stage of the f[v], if it is used, it will be included in the phase of the v-ci to accumulate.

If you use a one-dimensional array to express, F[v] in the left value is expressed in this stage, to ensure that the right side of the f[v] is the previous stage, and F[v-ci] is this stage, so ~ Order calculation!!

Pseudo code:

F [0..V]←0

For I←1 to N

For V←ci to V

F[v]←max (F[V],F[V−CI]+WI)

In addition, if you reverse the order of the two layers of loops, we can also get an understanding:

F [0..V]←0

For V←ci to V

TMP = 0

For I←1 to N

Tmp←max (TMP,F[V−CI]+WI)

F[v]=tmp

In this case, we can assume that for each V, F[v] has a maximum value for n items at capacity V, and when we calculate each V, we loop through each object, find the object that makes F[v the largest, and put it in.

So at this point the state transfer equation should be written

F[V] = max (i:1~n) {F[v-ci]+wi} that is, each state is transferred from its first n states, which is v-ci so that an item can be placed more than once.

-----------

But for the convenience of memory can be 01 backpack and complete backpack all with I-V double-cycle, complete backpack sequence, 01 backpack reverse order, you can.

"Algorithmic Learning Notes" 30. Dynamic planning 01 The relationship between backpack and complete 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.