C Language algorithm

Source: Internet
Author: User

For a 0/1 knapsack problem, load a backpack with a capacity of C. Select items from n items to be packed in backpacks, the weight of each item I is WI, and the value is pi. For a workable backpack load, the total weight of the item in the backpack cannot exceed the capacity of the backpack, and the best load is the highest value of the loaded item, that is, the maximum value of N. I=1pi XI. Constraints are N. I=1wi xi≤c and Xi? [0, 1] (1≤i≤n).

In this expression, a XT value is required. Xi=1 said the goods I loaded into the backpack, Xi=0 said the goods I do not pack into the backpack. 0/1 knapsack problem is a generalized problem of loading of containers, i.e. the value of each container is different. The problem of loading the container into the knapsack problem is as follows: The ship as a backpack, the container as a cargo that can be loaded into the backpack. Example 1-8 you get the first prize in a grocery game, and the trophy is a free grocery. There are n different kinds of goods in the shop. The rules stipulate that from each kind of goods can take only one, the car's capacity is C, the item I need to occupy WI space, the value is pi. Your goal is to maximize the value of the items that are loaded in your car. Of course, the goods can not exceed the capacity of the car, and the same item must not take many pieces. This problem can be modeled on the 0/1 knapsack problem, in which the car corresponds to the backpack and the goods correspond to the goods.

0/1 knapsack problem has several greedy tactics, each greedy strategy adopts multi-step process to complete the backpack loading. Use greedy guidelines to select an item to pack in each step of the process. A greedy rule is: from the rest of the goods, choose the most valuable items can be loaded into the backpack, using this regulation, the most valuable items are first loaded (assuming sufficient capacity), then the next most valuable items, so continue. This strategy does not guarantee the optimal solution. For example, consider n=2, w=[100,10,10], p=[20,15,15], C=1 0 5. When using the value greedy criterion, the solution obtained is x=[1, 0, 0], the total value of this scheme is 2 0. The optimal solution is [0, 1, 1], and its total value is 3 0.

Another option is the weight-greedy rule: Choose from the rest of the items that can be loaded with the smallest weight of the backpack. Although this rule can produce an optimal solution for the preceding example, it is not always possible to get the optimal solution in general. Consider n=2, w=[10,20], p=[5,100], c=2 5. When using the weight greedy strategy, the obtained solution is x=[1,0], which is worse than the optimal solution [0, 1].

Alternatively, a value density pi/wi greedy algorithm can be used, which is: Select from the remaining items to

This strategy does not guarantee an optimal solution by loading a package with the PI/WI value of the item. Using this strategy, we try to solve the optimal solution of n=3, w=[20,15,15], p=[40,25,25] and c=30.

We do not have to be frustrated by the number of greedy algorithms that are being investigated, and the 0/1 knapsack problem is an n-p complex problem. For this type of problem, it may not be possible to find an algorithm with polynomial time at all. Although it is not guaranteed that the optimal solution can be obtained according to the Order of PI/WI (increasing) reduction, it is an intuitively approximate solution. We want it to be a good heuristic, and most of the time it's close to the final algorithm. In 6 0 0 randomly generated knapsack problems, this heuristic greedy algorithm is used to solve 2 3 9 entitled Optimal Solutions. There are 5 8 3 examples with the optimal solution of 1 0, all 6 0 0 and the difference of the optimal solution is all within 2 5. This algorithm can achieve such good performance in O (NL o gn) time. We may ask whether there is an X (x<1 0 0), so that the result of the greedy heuristics differs from the optimal value within X percent. The answer is in the negative. To illustrate this point, consider examples of n=2, w=[1, y], p=[1 0, 9y], and c=y. Greedy algorithm result is x=[1,0], the value of this scheme is 1 0. For y≥1 0/9, the value of the optimal solution is 9 y. Therefore, the ratio of the value of the greedy algorithm to the optimal solution is ((9y-1 0)/9y* 1 0 0)%, and for large y, this value tends to be 1 0 0. However, greedy heuristic method can be established to provide the solution, so that the difference between the result of the solution and the value of the optimal solution is within X percent (X<100) of the optimal value. First of all k items into the backpack, if the K item weight greater than C, then give it up. Otherwise, the remaining capacity is used to consider loading the remaining items in descending order of PI/WI. The optimal solution is obtained by considering all possible subsets of k items in the solution produced by heuristic method.

Example 13-9 considers N=4, w=[2,4,6,7], p=[6,10,12,13], c=11. When k=0, the backpack according to the value density of goods in a non descending order, first put the item 1 into the backpack, then the goods 2, backpack the remaining capacity of 5 units, the rest of the goods do not have a suitable, so the solution for x=[1, 1, 0, 0]. The value obtained by this solution is 1 6.

Now consider the greedy heuristics when k=1. The initial subset is {1}, {2}, {3}, {4}. subset {1}, {2} produces the same result as k=0, considering subset {3} and placing X3 as 1. At this point, there are 5 units of capacity, based on the value density is not incremental order to consider how to use the capacity of these 5 units. First consider the item 1, it is suitable, so take X1 for 1, then only 3 unit capacity, and the rest of the goods can not be added to the backpack items. The results obtained from the subset {3} are x=[1, 0, 1, 0], with a value of 1 8. Starting with the subset {4}, the resulting solution is x=[1, 0, 0, 1], and the value obtained is 1 9. The optimal solution for the subset size 0 and 1 o'clock is given [1, 0, 0, 1]. This solution is obtained by k=1 greedy heuristic algorithm.

If k=2, in addition to considering a subset of k< 2, you must also consider subsets {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, and {3, 4}. Start with the last subset, it is not feasible, so discard it, the remaining subsets are solved with the following results: [1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1], [0, 1, 1, 0] and [0, 1, 0]. , 1], the last value of these results is 2 3, and its value is higher than the solution obtained by k=0 and k=1, this answer is the result of heuristic method. This modified greedy heuristic method is called K-order optimization (K-o p t i m a l). That is, if you remove K from the answer and put in another k, the result will not be better than the original, and the value obtained in this way is within the optimal value (1 0 0/(k + 1))%. When K=1, the final result is guaranteed to be within 5 0 of the optimal value, while the k=2 is 3 3. 3 3% and so on, the execution time of this heuristic method increases with the increase of k, the subset number that needs to be tested is O (NK), the time required for each subset is O (n), so the total time cost for K >0 is O (nk+1). The actual observed performance is much better.

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.