knapsack-problem

Why is the knapsack problem pseudo-polynomial?

走远了吗. 提交于 2019-11-28 02:52:35
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 ? marcog 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 it pseudo -polynomial. Consider W = 1,000,000,000,000. It only takes 40 bits to represent this

knapsack optimization with dynamic variables

有些话、适合烂在心里 提交于 2019-11-28 02:25:06
I am trying to solve an optimization problem, that it's very similar to the knapsack problem but it can not be solved using the dynamic programming. The problem I want to solve is very similar to this problem: Alex Fleischer indeed you may solve this with CPLEX. Let me show you that in OPL. The model (.mod) {string} categories=...; {string} groups[categories]=...; {string} allGroups=union (c in categories) groups[c]; {string} products[allGroups]=...; {string} allProducts=union (g in allGroups) products[g]; float prices[allProducts]=...; int Uc[categories]=...; float Ug[allGroups]=...; float

Dynamic programming sum

不羁的心 提交于 2019-11-28 01:03:09
问题 How would you use dynamic programming to find the list of positive integers in an array whose sum is closest to but not equal to some positive integer K? I'm a little stuck thinking about this. 回答1: The usual phrasing for this is that you're looking for the value closest to, but not exceeding K. If you mean "less than K", it just means that your value of K is one greater than the usual. If you truly mean just "not equal to K", then you'd basically run through the algorithm twice, once finding

OpenMP - Nested for-loop becomes faster when having parallel before outer loop. Why?

孤街浪徒 提交于 2019-11-27 16:41:39
问题 I'm currently implementing an dynamic programming algorithm for solving knapsack problems. Therefore my code has two for-loops, an outer and an inner loop. From the logical point of view I can parallelize the inner for loop since the calculations there are independant from each other. The outer for loop can not be parallelized because of dependancies. So this was my first approach : for(int i=1; i < itemRows; i++){ int itemsIndex = i-1; int itemWeight = integerItems[itemsIndex].weight; int

Knapsack with multiple bags and items having only weight

浪尽此生 提交于 2019-11-27 13:28:31
问题 I am trying to solve this problem and I wanted to know if there are known existing algorithms / solutions to solve this. Problem: I have n bags and n items (which are either equal or different weights) to fill into these bag. Each of these bags have a certain weight limit and the n items needs to be put into these bags in such a way that I can use the maximum space in each of these bags. The bags are of equal size. Will also like to know how to solve with bags of unequal size too. Most of the

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

為{幸葍}努か 提交于 2019-11-27 11:08:44
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.append(val[0]) t2.append(val[1]) print min(sum(t1),sum(t2)), max(sum(t1),sum(t2)), "\n" Question is

Variation on knapsack - minimum total value exceeding 'W'

有些话、适合烂在心里 提交于 2019-11-27 04:08:23
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! let TOT = w1 + w2 + ... + wn . In this answer I will describe a second bag. I'll denote the original as 'bag' and to the additional as 'knapsack' Fill the bag with all elements, and start excluding

Optimal way of filling 2 knapsacks?

一笑奈何 提交于 2019-11-27 01:09:12
问题 The dynamic programming algorithm to optimally fill a knapsack works well in the case of one knapsack. But is there an efficient known algorithm that will optimally fill 2 knapsacks (capacities can be unequal)? I have tried the following two approaches and neither of them is correct. First fill the first knapsack using the original DP algorithm to fill one knapsack and then fill the other knapsack. First fill a knapsack of size W1 + W2 and then split the solution into two solutions (where W1

Multiple Constraint Knapsack Problem

吃可爱长大的小学妹 提交于 2019-11-27 00:57:18
问题 If there is more than one constraint (for example, both a volume limit and a weight limit, where the volume and weight of each item are not related), we get the multiply-constrained knapsack problem, multi-dimensional knapsack problem, or m-dimensional knapsack problem. How do I code this in the most optimized fashion? Well, one can develop a brute force recursive solution. May be branch and bound.. but essentially its exponential most of the time until you do some sort of memoization or use

knapsack optimization with dynamic variables

半世苍凉 提交于 2019-11-26 23:43:14
问题 I am trying to solve an optimization problem, that it's very similar to the knapsack problem but it can not be solved using the dynamic programming. The problem I want to solve is very similar to this problem: 回答1: indeed you may solve this with CPLEX. Let me show you that in OPL. The model (.mod) {string} categories=...; {string} groups[categories]=...; {string} allGroups=union (c in categories) groups[c]; {string} products[allGroups]=...; {string} allProducts=union (g in allGroups) products