Some understanding and application of knapsack problem _c language

Source: Internet
Author: User
Tags pack

1. Knapsack Problem Introduction

Knapsack problem is not only a simple algorithm problem, it essentially represents a large class of problems, this kind of problem is actually 01 linear programming problem, its constraint condition and objective function are as follows:

Since Dd_engi launched in 2007, "backpack problem nine-talk", the main essence of knapsack problem has been done. This article does not attempt to expand or dig into the nature of the knapsack problem, but only from the limited understanding (here refers to the "backpack question nine" understanding) embarks, helps the reader to learn "knapsack question nine" the various knapsack problem main algorithm thought, and through the example explained the corresponding algorithm, At the same time, the classic application of several knapsack problems is given.

2. Knapsack problem and its application

Dd_engi in the "backpack problem nine" in the main mention of four kinds of knapsack problems, respectively: 01 knapsack problem, full knapsack problem, multiple knapsack problem, two-dimensional cost knapsack problem. This section summarizes these knapsack problems and gives a typical application to help readers understand the nature of these problems.

2.101 knapsack problem

(1) Problem description

There are n items and a backpack with a capacity of V. The cost of item I is c[i], value is w[i]. Solving which items are loaded into the backpack can make the sum of the greatest value.

(2) state transition equation

Of these, F (i,v) represents the maximum value that the previous I items had selected to be loaded into a backpack with a capacity of V. When V=0, F (i,v) initialized to 0, indicating that the problem does not require the backpack must be full, and F (i,v) =inf/-inf (positive infinity or negative infinity) that the problem requires that the backpack must be just filled. The following kinds of backpacks are similar and don't repeat them later.

(3) Pseudo-code

It can be seen from the transfer equation that the optimal solution of the first I object depends only on the optimal solution of the first i-1 item, and the former i-2,i-3,... The best of the items are not directly related, you can use this feature to optimize the storage space, that is, only a one-dimensional array can be applied, the algorithm time complexity (O (VN)) is:

For I=1..N for
v=v.. 0
F[v]=max{f[v],f[v-c[i]]+w[i]};

Note the traversal order of V!!!

The following kinds of backpacks are used for similar optimizations and will not be added later.

(4) Examples

v=10,n=3,c[]={3,4,5}, w={4,5,6}

(1) The backpack is not necessarily filled

The order of calculation is: from right to left, from top to bottom:

(2) Pack just full

The order of calculation is: from right to left, from top to bottom. Note the initial value, where-inf represents negative infinity

(5) Classic Questions

[1] You have a bunch of stones of quality, respectively, for W1,W2,W3 ... WN. (W<=100000,n <30) now requires you to combine the stones into two piles to minimize the difference in the mass of the two heaps.

[2] give a set of integers, divide it into two sets, two sets of numbers and the closest

[3] There is a box volume for V (positive integer, 0≤v≤20000), which has at the same time n goods (0 less than n≤30), each item has a volume (positive integer). Requirements from the n items, take a number of loaded into the box, so that the remaining space of the box is the smallest.

2.2 Full knapsack problem

(1) Problem description

There are n items and a backpack with a capacity of V, each of which has an unlimited number of items available. The cost of item I is c[i], value is w[i]. To find out what items to pack into the backpack can make the sum of these items does not exceed the backpack capacity, and the total value of the largest.

(2) state transition equation

Or:

(3) Pseudo-code

For I=1..N for
v=0..v
F[v]=max{f[v],f[v-c[i]]+w[i]};

Note the traversal order of V!!!

Note that the time complexity is still: O (VN).

(4) Examples

v=10,n=3,c[]={3,4,5}, w={4,5,6}

(1) The backpack is not necessarily filled

The order of calculation is: From left to right, from top to bottom:

(2) Pack just full

The order of calculation is: From left to right, from top to bottom. Note the initial value, where-inf represents negative infinity

(5) Classic Questions

[1] Change the question: There are n denominations of coins, each coin infinitely more, at least how many coins to represent the given value of M?

More than 2.3 heavy knapsack problem

(1) Problem description

There are n kinds of goods and a backpack with a capacity of V. The items I have at most have n[i] pieces available, each cost is c[i], value is w[i]. To find out what items to pack into the backpack can make the sum of these items does not exceed the backpack capacity, and the total value of the largest.

(2) state transition equation

(3) Solving ideas

Use the following methods to convert to a normal 01 knapsack problem: Divide the article I into several items, each of which has a factor, and the cost and value of this item are multiplied by the original cost and value. The coefficients are 1,2,4,..., 2^ (k-1), n[i]-2^k+1, and K is the largest integer that satisfies the n[i]-2^k+1>0. For example, if N[i] is 13, the 1,2,4,6 is divided into four items of the coefficient respectively. This method can be guaranteed for 0. Each integer of N[i] can be represented by a number of coefficients. It is easy to prove that the proof process uses the following theorem: Any integer n can be represented as: N=a0*2^0+a1*2^1+...+ak*2^k, where ak=0 or 1 (actually the binary decomposition of N),

Theorem: A positive integer n can be decomposed into 1,2,4,..., 2^ (k-1), n-2^k+1 (k is the largest integer that satisfies n-2^k+1>0), and all integers within 1~n can be uniquely represented as 1,2,4,..., 2^ (k-1), n-2^k+ 1 The form of the and of a few numbers.

The proof of the theorem is as follows:

(1) Sequence 1,2,4,..., 2^ (k-1), n-2^k+1 all elements of the and is N, so the number of elements and the range is: [1, N];

(2) If the positive integer t<= 2^k–1, then T must be able to use the 1,2,4,..., 2^ (k-1) of a number of the and representation, this is easy to prove: We write the binary representation of T, it is obvious that T can be represented as a n=a0*2^0+a1*2^1+...+ak*2^ (k-1), wherein ak=0 or 1 indicates that the first AK bit binary number of T is 0 or 1.

(3) If t>=2^k, set s=n-2^k+1, then t-s<=2^k-1, so that T-S can be represented as 1,2,4,..., 2^ (k-1) in the form of several numbers, which can be represented as 1,2,4,..., 2^ (k-1), The form of a number in S and (Addends must contain s).

(The Certificate of completion!) )

The time complexity of the algorithm is: O (V*sum (logn[i)).

(4) Classic Questions

[1] To find the change problem: There are n denominations of coins, respectively, a[0], a[1],..., a[n-1], the number of each coin is b[0], b[1],..., b[n-1], at least how many coins to represent the given value m?

2.4 Two-dimensional cost backpack

(1) Problem description

Two-dimensional cost of the knapsack problem is: For each item, there are two different costs, choose this item must pay both of these costs, for each price has a maximum value (backpack capacity). Ask how to choose items to get the most value. These two costs are cost 1 and 2 respectively, and the two costs required for item I are a[i] and b[i]. The maximum value that can be paid at two costs (two knapsack capacity) is V and U respectively. The value of the item is w[i].

(2) state transition equation

(3) algorithm idea

To solve the problem by using the same dimension

(4) Classic Questions

There are 2n integers, the average is divided into two groups, each group of n number, so that the two sets of the number and the closest.

3. Backpack Summary

Knapsack problem in fact represents a linear programming problem, generally consider the following factors have been decided to choose what algorithm:

(1) constraints , there is one or two or more, if it is one, may be 01 backpacks, full backpack or multiple knapsack problem, if there are two constraints, it may be a two-dimensional knapsack problem.

(2) optimize the target , ask the maximum, or the minimum, or the total (just change max to sum)

(3) the number of each item limit , if each item only one, just a simple 01 knapsack problem, if the number of unlimited, it is full knapsack problem, if the number of each item is limited, it is a multiple knapsack problem.

(4) whether the backpack requires just filling, attention is not filled and stuffed in two cases the initial value is different.

4. Reference materials

Dd_engi: "Backpack problem nine-story"

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.