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

Solving the Integer Knapsack

I a new to dynamic programing and have tried the integer knapsack problem here at SPOJ (http://www.spoj.pl/problems/KNAPSACK/) . However, for the given test cases my solution is not giving the correct output. I'd be thankful to you if you could…
hytriutucx
  • 1,614
  • 6
  • 26
  • 37
7
votes
3 answers

Problems with dynamic programming

I've got difficulties with understanding dynamic programming, so I decided to solve some problems. I know basic dynamic algorithms like longest common subsequence, knapsack problem, but I know them because I read them, but I can't come up with…
xan
  • 1,053
  • 1
  • 10
  • 29
7
votes
1 answer

Are these 2 knapsack algorithms the same? (Do they always output the same thing)

In my code, assuming C is the capacity, N is the amount of items, w[j] is the weight of item j, and v[j] is the value of item j, does it do the same thing as the 0-1 knapsack algorithm? I've been trying my code on some data sets, and it seems to be…
Richie Li
  • 1,257
  • 2
  • 13
  • 20
7
votes
2 answers

Why does this DP solution to the 0/1 Knapsack Problem not give the correct output with GCC?

#include int max(int a,int b) { if(a>b) return a; else return b; } void knapsack(int m,int n,int w[],int p[]) { int v[10][10],x[10],i,j; for(i=0;i<=n;i++) { for(j=0;j<=m;j++) { …
Chaitanya Nettem
  • 1,209
  • 2
  • 23
  • 45
7
votes
2 answers

Cutting Stock Problem

I'm trying to nest material with the least drop or waste. Table A Qty Type Description Length 2 W 16x19 16' 3 W 16x19 12' 5 W 16x19 5' 2 W 5x9 3' Table B Type Description StockLength W 16X19 …
Arik Lewis
  • 71
  • 1
  • 4
7
votes
3 answers

Recursive solution of unbounded knapsack using logic of 0/1 knapsack

Out of all the DP solutions I have checked out for 0/1 knapsack and unbounded knapsack, solution approaches are always defined like this : 0/1 knapsack : Maximise total value by either taking n-th item, or excluding n-th item. For example, 0/1…
user270386
  • 323
  • 1
  • 4
  • 14
7
votes
3 answers

Linear programming with conditional constraints in R

I have a linear programming problem where I'm trying to select from a number of binary resources to optimize value, basically a knapsack problem. The issue I'm having is that the different resources have characteristics in common and I want to…
7
votes
3 answers

Reconstructing the list of items from a space optimized 0/1 knapsack implementation

A space optimization for the 0/1 knapsack dynamic programming algorithm is to use a 1-d array (say, A) of size equal to the knapsack capacity, and simply overwrite A[w] (if required) at each iteration i, where A[w] denotes the total value if the…
kabir
  • 145
  • 1
  • 6
7
votes
1 answer

Optimal MLB lineup using Knapsack variant

I am writing a program to find the best possible MLB lineup using a knapsack solution. For this I pass in player data which has a players calculated value and salary. The salary will be my "weight" in terms of being a knapsack problem. My problem…
urnotsam
  • 770
  • 7
  • 24
7
votes
4 answers

Which is the best method between Genetic Algorithm and Dynamic Programming to solve classic 0-1 knapsack?

Let's say I have problem like this: Knapsack capacity = 20 million Number of item = 500 Weight of each item is random number between 100 to 20 million Profit of each item is random number between 1 to 10 So which is the best method for my problem?…
hanx
  • 83
  • 1
  • 3
7
votes
3 answers

Solving task scheduling or bin-packing optimizations in R

I have an optimisation issue. It's about a product that contains 20 parts (the order of producing doesn't matter). I've got 3 similar machine that can produce all 20 parts. I've got the 20 parts represented in minutes (ie. it takes 3min to produce…
S12000
  • 3,345
  • 12
  • 35
  • 51
6
votes
2 answers

Forming Dynamic Programming algorithm for a variation of Knapsack Problem

I was thinking, I wanted to do a variation on the Knapsack Problem. Imagine the original problem, with items with various weights/value. My version will, along with having the normal weights/values, contain a "group" value. eg. Item1[5kg, $600,…
user730882
6
votes
3 answers

Given an elevator with max weight and n people with x_i weights, find out minimum number of rides needed

input: max_weight = 550 n = 4 x_i = [120, 175, 250, 150] output: 2 // [[250, 175, 120], [150]] My initial impression is that this looks very similar to a dynamic programming coin change/knapsack problem, however it is not coin change (which…
6
votes
2 answers

Multidimensional Knapsack with minimum and maximum

I have a problem which is similar to the Knapsack problem, more specifically the multidimensional variation. I have a bunch of objects which all have a cost, a value, and a category. I need to the Knapsack optimisation for value under a maximum…
Kaito Kid
  • 983
  • 4
  • 15
  • 34
6
votes
2 answers

PHP: Finding a set of numbers in a database that sums up to a particular number

Firstly, i am a php newbie... so i still code and understand php procedurally. that said, i have a collection of numbers (amount) stored in a database. Question: Using PHP and mySQL, Whats the best way to spool this info out of a database such…