0-1 Backpack

Source: Internet
Author: User

01 Backpack (Zeroonepack): There are n items and a backpack with a capacity of V, each of which has only one item . The cost of article I is c[i], the value is w[i]. The sum of the values is maximized by solving which items are loaded into the backpack.

Full Backpack (completepack): There are n items and a backpack with a capacity of V, each item has unlimited pieces available. The cost of item I is c[i] and the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.

multiple Backpacks (multiplepack): There are n items and a backpack with a capacity of V, the first item I have n[i] pieces are available. The cost per piece is c[i] and the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.

Compare three topics, you will find that the difference is the number of each backpack, 01 backpack is only one piece of each, complete backpack is each unlimited pieces, and multi-pack is a limited piece of each.

First to analyze the backpack :

01 Backpack (Zeroonepack): There are n items and a backpack with a capacity of V, each of which has only one item. The cost of article I is c[i], the value is w[i]. The sum of the values is maximized by solving which items are loaded into the backpack.

This is the most basic knapsack problem, characterized by: only one piece of each item, you can choose to put or not put.

Define the state with sub-question: F[i][v] Indicates the maximum value that the first I item can get in a backpack with a capacity of V. The state transfer equation is:

F[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}

to understand this process :

When the first I item is placed in the backpack with a capacity V, it has two different situations :

Situation one: I do not put in, then the value is: F[i-1][v] (that is, the same backpack space, do not put the first piece)

Case two: I put in, then the value is: F[i-1][v-c[i]]+w[i] (that is, the backpack put down the first piece, see how much space left, and then to see if you can put the 1~i-1 in the first few things)

(What does the second mean?) is if I put in, then in the capacity V-c[i] will be placed in the top I-1 items)

Finally compare the first and the second kind of the value of the size, which is relatively large, f[i][v] the value of which is. (Here is the point, understand!) )

Example:

Article number I

1

2

3

4

5

6

Volume C

2

3

1

4

6

5

Value W

5

6

5

1

19

7

If the backpack can be loaded with a maximum volume of 10, the package can be loaded in the largest value?

Solution:

5

5

5

5

5

TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= "> /tr>

1

2

3

4

5

6

7

8

9

10

1

0

5

5

5

5

5

5

5

5

2

0

6

6

11

11

11

11

11

11

3

5

10

11

11

16

16

16

16

16

4

5

10

11

11

16

16

16

16

17

5

5

10

11

11

19

24

24

29

30

6

5

5

10

11

11

19

24

24

29

30

The first line indicates: The current selection is the first I piece

The first column indicates: The space of the current backpack is V

Step: The table is drawn in the order that the first row from left to right, the second row from left to right ... (because a two-dimensional array is used, this is done from right to left; If you use a one-dimensional array, you want to go from left to right, and the next line overwrites the previous line)

Example: Taking f[5][10] as an example

This is how it gets, f[5][10] = max{f[4][10],f[4][10-c[i]]+w[i]} = max{f[4][10],f[4][10-6]+19} = max{f[4][10],f[4][4]+19}   = max{17,11+19}=30. The red lattice in the 5th row of the table is drawn from the 4th row of two red squares.

This is stored in a two-dimensional array that optimizes the space and stores it in a one-dimensional array.

In F[0..V], F[v] represents the value of putting the first I item into a backpack with a capacity of V. After I have cycled from 1~n (n pieces), the last F[v] represents the maximum value to be asked.


Here F[v] is the equivalent of a two-dimensional array of f[i][v]. So, how to get f[i-1][v] and F[i-1][v-c[i]]+w[i]? Focus Thinking

The first thing to know is that we are using the I from 1 to n loops to indicate the status of the first I item deposited.

That is: for I=1..N

Now think about how it can be f[v] that the current state is the value of a backpack with a capacity of V, while the f[v] and F[v-c[i]]+w[i] label the value of the previous state?

Reverse

That's the point!

1  for i=12for        v=v: 0 3  f[v]=max{f[v],f[v-c[i]]+w[i]};

Why reverse order?

Example: F[3][5] How did it come about?

① by the matrix to save, it depends on the f[2][5] and f[2][4], this line when I is 2 has been obtained, can be used directly.

② but by a one-dimensional array, when I is 3 o'clock, a one-dimensional array is computed after the total value is calculated from v=1~10, so v=1~4 in a one-dimensional array is updated when f[3][5] is counted, and 5~10 is not updated. Looking back at the ①, the required f[2][5] is still not updated in one-dimensional array, and f[2][4]=6 is updated to 11, although f[3][5] The final result is the same. But if you pick something else, it doesn't have to be the same, like f[5][10].

0-1 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.