# [Algorithm review 3] algorithm design skills and Optimization-Summary of various knapsack problems

Source: Internet
Author: User

I. algorithm Policy Application

By profit

• Profit-independent knapsack problems

• Profit-related knapsack problems

How many objects are loaded into a backpack?

• Some backpack Problems

• 0-1 backpack Problems

2) solutions to the knapsack problem, based on different needs

• Greedy, backtracking, branch restriction, and dynamic planning

• Recursive or non-recursive Solution

Ii. Demonstration of knapsack problems

1) backpack problem 1

Problem description: For N kinds of items, select m items from them to minimize the absolute value of the sum of weights and the difference between T.

For example, n = 9, M = 3, T = 500 grams.

• Problem Analysis 1: triplicate and enumeration are used.

Data change process:

(1, 2, 3) (1, 2, 4 )... (1, 2, 9)

(1, 3, 4) (1, 3, 5 )... (1, 3, 9 )... (1, 8, 9)

(2, 3, 4) (2, 3, 5 )... (2, 3, 9 )... (7, 8, 9)

• Algorithm 1 analysis time complexity is O (N3)

Source code:

# Include <iostream> <br/> using namespace STD; <br/> int main () <br/> {<br/> int A [9] = {1, 2, 3, 4, 5, 6, 7, 8, 9 }; <br/> int M = 3; <br/> int T = 5; <br/> int diff; <br/> int mindiff = 999; <br/> int tempi, tempj, tempk; <br/> for (INT I = 0; I <9; ++ I) <br/> {<br/> for (Int J = I + 1; j <9; ++ J) <br/> {<br/> for (int K = J + 1; k <9; ++ K) <br/> {<br/> diff = (a [I] + A [J] + A [k]-T> 0? A [I] + A [J] + A [k]-T: T-(A [I] + A [J] + A [k]); <br/> If (mindiff> diff) <br/>{< br/> mindiff = diff; <br/> tempi = I; <br/> tempj = J; <br/> tempk = K; <br/>}</P> <p> cout <"select number is: "<A [tempi] <" "<A [tempj] <" "<A [tempk] <Endl; <br/> cout <"sunm:" <A [tempi] + A [tempj] + A [tempk] <Endl; <br/> cout <"mindiff:" <mindiff <Endl; </P> <p>}

2) backpack problem 2

Problem description: N items and a weight are given as M backpacks. The Weight of item I is WI. Ask how to choose the items to be loaded into the backpack so that the total weight of the items in the backpack is the heaviest. Find all solutions to the problem. For example, when M = 10, each item is {5, 2, 3.5, 1.7, 1, 5.1 }.

• Problem Analysis 2: multiple loops and enumeration are used.

• Source code:

# Include <iostream> <br/> using namespace STD; <br/> int main () <br/> {<br/> double A [6] = {5, 2, 3.5, 1.7, 1, 5.1 }; <br/> double M = 10; <br/> double max = 0; <br/> double temp; <br/> for (INT I1 = 0; I1 <= 1; ++ I1) <br/> {<br/> for (INT I2 = 0; i2 <= 1; ++ I2) <br/> {<br/> for (INT I3 = 0; I3 <= 1; ++ I3) <br/> {<br/> for (INT I4 = 0; I4 <= 1; ++ I4) <br/> {<br/> for (INT I5 = 0; I5 <= 1; ++ I5) <br/> {<br/> for (INT I6 = 0; I6 <= 1; ++ I6) <br/> {<br/> temp = A [0] * I1 + A [1] * I2 + A [2] * I3 + A [3] * I4 + [4] * I5 + A [5] * I6; <br/> If (temp <= M & max <temp) <br/> max = temp; <br/>}</P> <p> cout <max <Endl; <br/>}

Improvement: Convert decimal to binary. For example, "000001" indicates that only one is selected, and "000011" indicates that two are selected. The number of cycles is 2 '6

3) backpack problem 3

Problem description: N items and a weight are given as M backpacks. The Weight of item I is WI. Ask how to choose the items to be loaded into the backpack, so that the total weight of the items in the backpack is exactly M, to find out all the solutions to the problem. For example, if M = 10 and each item is {,}, four groups of solutions (,) (, 5) (, 5, 2)

• Problem Analysis

The backtracking method is used to put items into the backpack one by one. If the weight of the backpack is exceeded, discard the selected items and select the next one. The items are repeated until all or no solutions are found.

Assume that the order of listing items {, 3,} is 1, 2, 3, 4, 5, or 6. In this case, the order of listing items is the same as that of loading items, stack can be used to describe the solution.

• Source code

# Include <iostream> <br/> # include <stack> <br/> using namespace STD; <br/> int W [6] = }; <br/> int M = 10; </P> <p> void stacktraverse (stack <int> S) <br/>{ <br/> cout <"the result of this group is:" <Endl; <br/> while (! S. empty () <br/>{< br/> cout <W [S. top ()] <Endl; <br/> S. pop (); <br/>}< br/> void knapsack (int w [], int M, int N) <br/> {<br/> stack <int> S; <br/> int K = 0; // evaluate the knowledge of 1st items <br/> while (! S. empty () | K <n) <br/> {<br/> while (M> 0 & K <n) <br/>{< br/> If (m-W [k]> = 0) <br/>{< br/> S. push (k); <br/> M-= W [k]; <br/>}< br/> K ++; <br/>}< br/> If (M = 0) <br/> stacktraverse (s); // output a group of solutions, continue to find the next group of solutions </P> <p> K = S. top () + 1; // continue next <br/> M + = W [S. top ()]; // restore m value <br/> S. pop (); // The top, backtracing <br/>}< br/> int main () <br/>{< br/> knapsack (W, M, 6); </P> <p> return 0; <br/>}< br/>

4) Knapsack Problem 4-Greedy Algorithm for profit knapsack problems

Problem description: N items and a backpack are given. The Weight of item I is wi, the value is pi, and the size of the backpack is M. Q: How should I select the items to be loaded into the backpack to maximize the total value of the items in the backpack?

• Greedy Analysis

Each time you choose the object with the highest unit price from the item set, if its weight is smaller than the remaining weight of the backpack, you can put it into the backpack. Then we were faced with an optimal sub-problem-it was also a backpack problem, but the total capacity M was reduced and the item set was reduced. Therefore, the knapsack problem is obviously of the optimal sub-structure nature. We can use the greedy algorithm to solve the problem.

• Basic steps to solve the knapsack problem with greedy method 4

(1) Calculate the unit weight value PI/wi of each item first;

(2) If the total weight of N kinds of items is less than the total weight of M, you only need to put all the items into the backpack.

(3) Otherwise, carry a backpack from high to low based on the unit weight value of the item.

(4) If the total weight of a backpack does not exceed M, follow this rule until the backpack is full.

(5) The last part may be part of the item.

5) Knapsack Problem 5-0-1 Dynamic Programming Method for Knapsack Problem

Problem description: N items and a backpack are given. The Weight of item I is wi, the value is pi, and the size of the backpack is M. Q: How should I select the items to be loaded into the backpack to maximize the total value of the items in the backpack? (Items not allowed to be split) See blog http://blog.csdn.net/tianshuai11/article/details/7533317

6) knapsack problem 6--0-1 branch restriction method of Knapsack Problem

• Problem description: N items and a backpack are given. The Weight of item I is wi, the value is pi, and the size of the backpack is M. Q: How should I select the items to be loaded into the backpack to maximize the total value of the items in the backpack? (Items cannot be split), and the queue-type branch restriction method is provided for the solution of the loaded items.

• Algorithm analysis

The calculation time upper bound of the algorithm is O (2' N)

• For a 0-1 backpack, the greedy choice cannot obtain the optimal solution because it cannot guarantee that the backpack will eventually be filled up, and some idle space will reduce the value of each kilogram of space.

• In fact, when considering the 0-1 backpack problem, we should compare the Final Solution caused by selecting this item and not selecting this item, and then make the best choice. As a result, we can export many subproblems that overlap each other. This is another important feature of the problem that can be solved using a dynamic planning algorithm.

Related Keywords:

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.