knapsack-problem

Reducing time complexity of Knapsack 0~1 while using DP algorithm

不羁岁月 提交于 2019-12-11 08:05:46
问题 I'm using DP algorithm, i.e. storing sub-problem values in 2D array where one axis means n items and other - w values from 0 to W where W is the maximum capacity of knapsack. Therefore T[n-1][W] value is the optimum I need to calculate. I've read in other sources that time complexity of this algorithm is O(nW) . My quesiton would be: is it possible to reduce this time complexity even more? I found other answer which talks about pretty much same thing but I can't understant it without example:

Knapsack with items to consider constraint

心已入冬 提交于 2019-12-11 07:07:15
问题 I have items I1, I2, I3, I4 with weights W1...W4 and Values V1...V4. I want to maximize values with minimum weights. This is a traditional Knapsack. However there is small constraint some items cannot go together. So lets say I2 and I3 cannot go together. Can anyone provide a dynamic programming solution or any other solution for the same. 回答1: With this constraint, the problem becomes strongly (as opposed to discrete knapsack, which is only weakly NP-hard) NP-hard. Suppose all your items

How to find best solution from all brute force combinations?

耗尽温柔 提交于 2019-12-11 05:17:43
问题 I want to find the best solution from all brute force combinations of a dictionary. For the context of the problem, I need to find out the minimum number of trips needed to transport all the cows given a weight limit. The combinations are already given to me with the helper functions get_partitions . The function returns a nested list, with each inner list represents a trip and names of cows on that trip. Helper functions: def partitions(set_): if not set_: yield [] return for i in range(2*

How to implement the Sum of Subsets problem in Java

自闭症网瘾萝莉.ら 提交于 2019-12-11 02:57:56
问题 Does anyone know how to implement the Sum-of-Subsets problem in Java from this pseudo code? w = an array of positive integers sorted in non-decreasing order. W = the target sum value include = an array or arraylist of the solutions who's weight adds up to W. After the print statement, this array can be deleted so that it can store the next solution. weight = weight of elements in the include array. total = weight of the remaining elements not in the include array. public static void sum_of

Knapsack C# implementation task

▼魔方 西西 提交于 2019-12-11 00:57:18
问题 I'm trying to write knapsack c# algorithm with given conditions but there's always two problems i encountering. I'm getting "Index was outside the bounds of the array" error or my result is just 0. I found couple code examples of Knapsack implementation and just can't figure out what I'm doing wrong. Code examples: https://www.programmingalgorithms.com/algorithm/knapsack-problem http://www.csharpstar.com/csharp-knapsack-problem/ My code: static int Knapsack(int n, int w, int[] s, int[] v) {

find items in knapsack bag

半世苍凉 提交于 2019-12-11 00:12:43
问题 I want to solve the knapsack problem recursively in C#. This is my code: public int f(int n, int remain) { if (n < 0) return 0; if (w[n] > remain) { // Thread.VolatileWrite(ref check[n], 0); check[n] = 0; return f(n - 1, remain); } else { int a = f(n - 1, remain); int b = p[n] + f(n - 1, remain - w[n]); if (a >= b) { // Thread.VolatileWrite(ref check[n], 0); check[n] = 0; return a; } else { // Thread.VolatileWrite(ref check[n], 1); check[n] = 1; return b; } } } w is an array that holds

2 knapsacks with same capacity - Why can't we just find the max-value twice

我的未来我决定 提交于 2019-12-10 16:39:06
问题 If you are given one set of items with values and weight: [(w1,v2),(w2,v2),...(wn,vn)], and two knapsacks Knap1 and Knap2 of equal capacity C, the goal is to determine what are the optimal subsets of items S1 and S2 that can go into Knap1 and Knap2 respectively and maximize the knapsacks' values and capacity. An incorrect way to solve this would be to first fill Knap1 with a DP programming algorithm using all of the items as candidates, and then fill Knap2 by using the leftover items from

Knapsack problem with all profits equal to 1

人盡茶涼 提交于 2019-12-10 15:58:19
问题 There is a variation of knapsack problem when all profits are equal to 1. It seems it can be solved much faster than classical discrete (0-1) knapsack problem, but how? Will just greedy algorithm work (on each iteration put an object with minimum weight to the knapsack)? 回答1: I should think so. Intuitively, given that all profits equal one, on the profit side you're indifferent to which items you select, you just want as many as you can. The greedy algorithm will give you exactly that. 来源:

Recursive Knapsack Java — Errors

扶醉桌前 提交于 2019-12-10 11:56:15
问题 yet another knapsack question... I understand the concept of the knapsack problem but I am having troubles with parts of my code.. I would like to believe that I am headed into the right direction but maybe not? Let's say I have a list of 1,2,3,6,7 and my goal weight is 4: The items that will equal this weight are 1 and 3. My first iteration will go like this: Item: 1 Goal Weight: 3 (I did weight - the item) Items: 1,2 Goal Weight: 1 Items: 1,2,3 Goal Weight: -2 -- Once the weight reaches < 0

Knapsack problem with repeating components for lowest value, to fill (or slightly over) in Python

廉价感情. 提交于 2019-12-10 11:24:28
问题 I'm actually trying to find the lowest cost to supply custom medication doses from vials - however while researching, I found the knapsack problem is the best way to describe the issue, in way that is currently understood. Breakdown of problem *A dose is prescribed based on clinical need. *The vials commercially available to make up the dose (and their respective dose) are then considered. The same size vial can be used more than once. *The vial / size combination may provide excess (i.e.