0-1 backpack dynamic Programming algorithm, partial knapsack greedy algorithm and DP algorithm------algorithm INTRODUCTION

Source: Internet
Author: User

First, the problem description

0-1 knapsack problem, part knapsack problem. The DP algorithm of 0-1 knapsack, the greedy algorithm of partial knapsack and DP algorithm are implemented respectively.

Second, the principle of the algorithm

(1) 0-1-pack DP algorithm

0-1 knapsack problem: There are n items and a backpack with a capacity of W. The weight of article I is w[i], the value is v[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. Each of these items has only one item, which you can choose to put or not put.

Optimal substructure properties: for 0-1 questions, consider the most valuable package of weights up to W. If you remove one of the items J, the rest must be n-1 items other than J, which can take away the most valuable bag of W-WJ.

Define state with sub-problem: c[i,w] Indicates the maximum value that the first I item fits into a backpack with a capacity of W. The state transfer equation is:

In the first I items into a backpack in the capacity W this sub-problem, if only consider the strategy of article I items, if you choose not to put the article I item, then the problem is converted to "the first I-1 items into the backpack capacity W", the value of c[i-1,w]; If you choose to place item I, then the problem is converted to " The first I-1 items are placed in the remaining W-wi backpack ", the maximum value that can be obtained at this time is C [I-1,W-WI] plus the value obtained by placing the article I item VI. According to this idea of recursion, the final maximum value that can be obtained is c[n, W].

(2) greedy algorithm for some backpacks

Part of the backpack problem is similar to the 0-1 knapsack problem, the difference is that part of the knapsack problem can be selected as part of the item, rather than as a 0-1 backpack can only be a two-point choice.

Some knapsack problems also have the properties of the optimal substructure. Consider that if the weight of the item J is removed from the best cargo, the remainder of the goods must be n-1 from the wj-w of the original item and article J, which can be taken away from the maximum value of w-w.

For some knapsack problem, you can use greedy strategy to solve. First of all to calculate the unit value of each item, that is, VI/WI, and then according to the greedy strategy, in each choice, the priority to select items of high unit value. In other words, select the item with the highest value of the current unit, if you have finished the item, and still can select some other items, then select the current unit value of the item, continue until it can not be taken.

(3) DP algorithm for some backpacks

Some knapsack problems can also be solved by DP algorithm. As the title has been described in all items weight and value are integers, using this feature, you can skillfully turn some knapsack problem into 0-1 knapsack problem, and then call 0-1 knapsack problem DP algorithm to solve.

The conversion method is: The first item is split into a weight of 1,2,4...2^ (k-1), wi-2^k+1 items, the corresponding value is the unit value multiplied by the split weight results. In a split sequence, K is the largest integer that satisfies wi-2^k+1>0. For example, if WI is 14, the item is divided into four items with a factor of 1,2,4,7. This is the idea of binary, and this division can always represent all the weight values that the item can choose. A new weight sequence and a value sequence are obtained by this division, and the new weight sequence and value sequence are called as input to solve some knapsack problems by calling the 0-1 knapsack algorithm. (The detailed thought can refer to the contents of the backpack Nine talk)

Third, the experimental data

(1) The experimental data input of three algorithms are:

A) Number of items n

b) The value of each item v1,v2......vn

c) Weight of each item W1,W2......WN

d) Maximum weight of backpack W

(2) The output is:

The maximum value that can be gained from the currently selected scenario

In this experiment, for three algorithms, the backpack maximum capacity w is 100, the number of items N is 5, the value sequence of all items {v} is {60,100,120,80,90}, and the weight sequence {W} is {10,20,30,40,5}. This data is then entered into each algorithm in turn for testing.

Iv. Source Code

Too much code, with download link: http://download.csdn.net/detail/zhh1992/8359275

0-1 backpack dynamic Programming algorithm, partial knapsack greedy algorithm and DP algorithm------algorithm INTRODUCTION

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.