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
6
votes
2 answers

Knapsack with "at least X value" constraint

How would you go about solving this variation of Knapsack? You have n objects (x1,...xn), each with cost ci and value vi (1<=i<=n) and an additional constraint X, which is a lower bound on the value of the items selected. Find a subset of x1,...,xn…
Luca Giorgi
  • 910
  • 3
  • 14
  • 28
6
votes
4 answers

knapsack 01 with twist

I'm doing a Knapsack in Java where we only use weights no value. The weightlimit is 1000. We get 5 weights scanned from keyboard which we use. The twist is that you can actually go over 1000 aslong as its the closets to 1000. So in one scenario we…
6
votes
2 answers

Knapsack with selection from distinct groups

I have a variation on the Knapsack Problem that I'm struggling to find an efficient solution for. Let's say you have multiple groups of items. Each group can have an arbitrary number of items, each with a value and weight. The problem is to find…
Dov Rosenberg
  • 673
  • 6
  • 20
6
votes
2 answers

Dynamic T-SQL approach for combinatorics/knapsack

I guess my question has to do with a variant of the knapsack problem, but I can't really come up with a solution for this: Let's say you are in a hardware store and need to buy 21 screws. They only offer them in bags: Bag X - 16 Screws - 1.56$ per…
user3464235
6
votes
2 answers

Printing out result in 0/1 KnapSack (Recursive Brute Force)

public static int KnapSack(int capacity, Item[] items, int numItems) { if (numItems == 0 || capacity == 0) return 0; if (items[numItems-1].weight > capacity) return KnapSack(capacity, items, numItems-1); else { …
Kevin Le
  • 846
  • 8
  • 17
6
votes
2 answers

Bounded knapsack special case - small individual item weight is small compared to the number of items

For the bounded knapsack problem, assuming the value of each item is the same as its weight and all weights are positive integers, I am wondering if there is an optimisation for the case where individual item weight is small compared to the number…
Jason L
  • 510
  • 5
  • 16
6
votes
2 answers

Solving knapsack prob in F#: performance

I found an article: Solving the 0-1 knapsack problem using continuation-passing style with memoization in F# about knapsack problem implemented in F#. As I'm learning this language, I found this really interesting and tried to investigate this a…
LA.27
  • 1,888
  • 19
  • 35
6
votes
2 answers

Knapsack algorithm with 2 properties. How to implement that in a 3d array?

I'm having a problem with understanding knapsack problem when there's more than 1 property. When there's 1 property, I have to write a program that uses knapsack algorithm with a 2 properties. Teacher told us, It has to be done in a 3d array. Wrong…
Paulina
  • 483
  • 3
  • 13
6
votes
3 answers

Logic Issue - how many / which small boxes in a big box - PHP/MySQL

I've got a problem, and I'll try and describe it in as simplest terms as possible. Using a combination of PHP and MySQL I need to fulfil the following logic problem, this is a simplified version of what is required, but in a nutshell, the logic is…
Big-G
  • 219
  • 1
  • 11
5
votes
4 answers

Java Optimization Algorithm

I'm new in programming, currently I'm facing an issue to build an algorithm for optimisation, I can't figure out a good and efficient solution to achieve what I'm expected for my current facing scenario. This is the case: Assume now James have 900…
kw88
  • 626
  • 7
  • 11
5
votes
2 answers

Solving a variation of 0/1 Knapsack (multiple source for items, each item can be selected from one of the sources)

So for a practice question, we are supposed to design a Dynamic Programming algorithm which is a variation of 0/1 knapsack problem... Basically each item comes from 4 different source, and that item can be taken from only one of the…
Tolga E
  • 12,188
  • 15
  • 49
  • 61
5
votes
2 answers

Recursive backtracking

I am having a problem with my backtracking function it loops with certain data I can't write here the whole program code but can put here my function. bool shareMoney(int quantity, int *values, int index, int moneyA, int half, bool *ifChosen) { …
Paul
  • 163
  • 7
5
votes
2 answers

Knapsack but exact weight

Is there a algorithm to determine a knapsack which has an exact weight W? I.e. it's like the normal 0/1 knapsack problem with n items each having weight w_i and value v_i. Maximise the value of all the items, however the total weight of the items in…
ztv
  • 134
  • 1
  • 9
5
votes
1 answer

subset which gives the least sum greater than or equal to a given number

I have a (multi)set of positive numbers, eg. {71.28, 82.62, 148.77, 85.05, 50.76, 103.41}. I want to find the subset which gives the least sum greater than or equal to a given number. Eg. if the minimum was 270, then the result would be {148.77,…
boofaz
  • 301
  • 1
  • 8
5
votes
1 answer

Knapsack problem with all profits equal to 1

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…
Yuri Stuken
  • 12,820
  • 1
  • 26
  • 23