knapsack-problem

Why is the knapsack problem pseudo-polynomial?

吃可爱长大的小学妹 提交于 2019-11-26 18:48:00
问题 I know that Knapsack is NP-complete while it can be solved by DP. They say that the DP solution is pseudo-polynomial , since it is exponential in the "length of input" (i.e. the numbers of bits required to encode the input). Unfortunately I did not get it. Can anybody explain that pseudo-polynomial thing to me slowly ? 回答1: The running time is O(NW) for an unbounded knapsack problem with N items and knapsack of size W. W is not polynomial in the length of the input though, which is what makes

How do I solve the 'classic' knapsack algorithm recursively?

巧了我就是萌 提交于 2019-11-26 17:38:44
This is my task The Knapsack Problem is a classic in computer science. In its simplest form it involves trying to fit items of different weights into a knapsack so that the knapsack ends up with a specified total weight. You don't need to fit in all the items. For example, suppose you want your knapsack to weigh exactly 20 pounds, and you have five items, with weights of 11, 8, 7, 6, and 5 pounds. For small numbers of items, humans are pretty good at solving this problem by inspection. So you can probably figure out that only the 8, 7, and 5 combination of items adds up to 20. I really don't

Algorithm to Divide a list of numbers into 2 equal sum lists

雨燕双飞 提交于 2019-11-26 15:26:21
问题 There is a list of numbers. The list is to be divided into 2 equal sized lists, with a minimal difference in sum. The sums have to be printed. #Example: >>>que = [2,3,10,5,8,9,7,3,5,2] >>>make_teams(que) 27 27 Is there an error in the following code algorithm for some case? How do I optimize and/or pythonize this? def make_teams(que): que.sort() if len(que)%2: que.insert(0,0) t1,t2 = [],[] while que: val = (que.pop(), que.pop()) if sum(t1)>sum(t2): t2.append(val[0]) t1.append(val[1]) else: t1

Variation on knapsack - minimum total value exceeding 'W'

余生颓废 提交于 2019-11-26 12:42:54
问题 Given the usual n sets of items (each unlimited, say), with weights and values: w1, v1 w2, v2 ... wn, vn and a target weight W , I need to choose items such that the total weight is at least W and the total value is minimized . This looks to me like a variation (or in some sense converse) of the integer/unbounded knapsack problem. Any help in formulating the DP algorithm would be much appreciated! 回答1: let TOT = w1 + w2 + ... + wn . In this answer I will describe a second bag. I'll denote the

How do I solve the 'classic' knapsack algorithm recursively?

≯℡__Kan透↙ 提交于 2019-11-26 05:32:00
问题 This is my task The Knapsack Problem is a classic in computer science. In its simplest form it involves trying to fit items of different weights into a knapsack so that the knapsack ends up with a specified total weight. You don\'t need to fit in all the items. For example, suppose you want your knapsack to weigh exactly 20 pounds, and you have five items, with weights of 11, 8, 7, 6, and 5 pounds. For small numbers of items, humans are pretty good at solving this problem by inspection. So

How to find which elements are in the bag, using Knapsack Algorithm [and not only the bag's value]?

纵饮孤独 提交于 2019-11-26 03:34:34
问题 There I have a code, which calculates the optimal value by knapsack algorithm (bin packing NP-hard problem): int Knapsack::knapsack(std::vector<Item>& items, int W) { size_t n = items.size(); std::vector<std::vector<int> > dp(W + 1, std::vector<int>(n + 1, 0)); for (size_t j = 1; j <= n; j++) { for ( int w = 1; w <= W; w++) { if (items[j-1].getWeight() <= w) { dp[w][j] = std::max(dp[w][j-1], dp[w - items[j-1].getWeight()][j-1] + items[j-1].getWeight()); } else { dp[w][j] = dp[w][j - 1]; } } }