Questions tagged [knapsack-problem]

The knapsack problem is a problem in combinatorial optimization: Given a set of items with associated weights and values, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and it maximizes the total value. It is an NP-complete problem, but several common simplifications are solved efficiently with dynamic programming.

There are several variants of the knapsack problem, such as 0-1 knapsack problem, bounded and unbounded knapsack problem. The unbounded and 0-1 knapsack are solved efficiently by using divide-and-conquer techniques (dynamic programming).

Other variants, where one can take a non-integer amount of an item, the weights are real numbers (and instances where other constraints become real instead of integer) are NP-complete.

A multiple knapsack problem is often referred to as the bin-packing problem.

1098 questions
-2
votes
1 answer

Helping to solve program knapsack the dynamic programming method

Friends who can program "knapsack the dynamic programming method" and "C ++" Will share with me? Algorithm to solve knapsack with dynamic programming: Thank You all Code to the method of "weight / value" : #include #include…
MONJE
  • 85
  • 11
-2
votes
1 answer

KNapsack variant. Ideas?

I have the following knapsack problem variant: I want to buy X units of a product at min cost, and there are m farmers that offer: - and I can choose at most one option from each farmer. Formally, I want to Could you please let me know if this…
STiGMa
  • 906
  • 3
  • 11
  • 24
-2
votes
1 answer

Function that return set (vector) of integers that are sum of other integer

I am looking for a Matlab function, that help me to set a vector of integers, that multiplied by other known vector will be a sum of the other known integer. For example: V = [1, 2, 3, 4] X = 10 results = [10, 0, 0, 0] [8, 1, 0, 0] ..... [0, 2, 0,…
user3448282
  • 2,629
  • 3
  • 25
  • 47
-2
votes
3 answers

How to return words from the input string as close to certain numbers of characters in Python?

On input we have a long list of words. And I should return an arbitrary string composed of words present in the input list and only of those. The overall length of the resulting string should be as close to 15 characters(ignoring spaces) from the…
-3
votes
1 answer

Algorithm to find the set of values which are repeatable to fit upto a maximum Total value

I have a set of values. (length of it can be dynamic. and some of the values can be undefined). Each of these values is an attack type. Ex: attacks=[960,1200,1800,2120,undefined,undefined]; And a total value, Ex: totalEnergy=18680; Now I want a…
Vignesh S
  • 332
  • 1
  • 2
  • 14
-3
votes
1 answer

Fast Knapsack Solver For big problems

I want to approximately solve the knapsack problem for big data sets using Python. Right now, I am using this implementation, which works well for small examples like: import knapsack weight = np.random.randint(10, size = 10) value =…
HolyMonk
  • 432
  • 6
  • 17
-3
votes
1 answer

Recursive 0-1 knapsack

Here I have two functions for the recursive knapsack problem while knapsack() gives the correct output (i.e 220) but knapsack1() gives wrong value (i.e 60) .Can anyone please explain why? #include #include using namespace std; …
Natsu
  • 91
  • 5
-3
votes
1 answer

Simple 3D cube in java

How do we code a simple 3d cube in java or multiple 3d cubes made into a container? The cubes will be used as parts of a 3d knapsack problem to build the container and 3 types of parcels. A 3d visual representation for these is needed.
-3
votes
1 answer

Wrong answer in Codechef MasterChef

I am facing issues in the solution for the problem http://www.codechef.com/problems/MCHEF , here is my solution http://ideone.com/SsbABr I have solved the problem using knapsack and set but i am getting wrong answer, can't seem to figure out why! I…
shekhark
  • 1
  • 4
-3
votes
1 answer

Wrong Answer : Scuba diver SPOJ

I am trying this problem on SPOJ. Its basically a 0/1 knapsack. Problem Statement: We are given N≤1000 choice of air tanks having oxygen capacity, nitrogen capacity, and weight O[i], N[i], and W[i] respectively O≤21,N≤79,W≤800. We need enough tanks…
newuser
  • 31
  • 5
-3
votes
1 answer

how to apply knapsack algorithm on three column

I have a problem like following to be implemented in java. item Profit Availability Quality 1 20 6 55 2 18 8 67 3 16 7 70 4 13 …
-3
votes
1 answer

InputMismatchException reading decimal fractions

In the main() function of http://penguin.ewu.edu/cscd501/Wint-2011/BranchAndBound/Knap01BnB.txt inp = new Scanner ( new File(lineIn) ); maxWeight = inp.nextInt(); n = inp.nextInt(); System.out.printf ("Reading data from file %s, with %d…
-4
votes
2 answers

Hi, I got java.lang.OutOfMemoryError: Java heap space while trying to solve this Algorithm problem, what can be done?

The problem is similar to subset-sum in which we determine whether or not a list of number has a subset which produces a given sum, but in this case, we are allowed to create a subset from elements of list p only if it is equal or greater than the…
theUturn
  • 1,101
  • 2
  • 12
  • 25
-4
votes
1 answer

How to solve this question asked in premium coding contest?

Could someone guide me on how to solve this programming question? It seems something like DP problem which I couldn't find answer for. Question: There are ‘n’ ads. Each ad has an effectiveness value associated with it which is given in an array…
-4
votes
1 answer

why Python script if statement not working correctly?

I was writing knapsack w = [0 for x in xrange(item_count+1)] v = [0 for x in xrange(item_count+1)] assigned value and weight (v, w) respectively. created c = [[0 for x in xrange(capacity+1)] for x in xrange(item_count+1)] c, w and v is…
ashmish2
  • 2,885
  • 8
  • 40
  • 54
1 2 3
73
74