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
4
votes
1 answer

How to display all included numbers in KnapSack problem?

I have a problem with displaying used numbers. I'm using KnapSack algorithm and I want to display all numbers that I used to get highest value. So there is my code: static int max(int a, int b) { int c = (a > b) ? a : b; …
AweX
  • 43
  • 3
4
votes
2 answers

0/1 Knapsack with Minimum Cost

The famous 0/1 knapsack problem focuses on getting the maximum cost/value in the given Weight (W). The code for the above is this :: n = cost_array / weight_array size INIT :: fill 0th col and 0th row with value 0 for (int i=1; i<=n; i++) { …
arqam
  • 3,582
  • 5
  • 34
  • 69
4
votes
1 answer

Why is there a 1-D array constructed in case of unbounded knapsack and 2-D array constructed in case of 0/1 knapsack?

I saw that a 1-D array is constructed in case of unbounded knapsack and a 2-D array is constructed in case of 0/1 knapsack? Why does this happen? This question is asked in reference to dynamic programming.
user11919180
4
votes
1 answer

0-1 Knapsack 2 rows how to find elements

Following the lesson here I've implemented a working 0-1 Knapsack algorithm only using two rows. Which outputs the correct final value. Names contains the id of each element that is being used. def KnapSack(val, wt, n, W): names = n n =…
4
votes
1 answer

Divide to n bins with minimum cost

Consider 2*k tuples (a0, b0), (a1, b1), ... and 2 bins A and B. placing the i-th tuple in bin A will cost you ai dollar, in bin B will cost you bi dollar. What the minimum cost to place k elements in bin A and k elements in bin B. I came up with…
4
votes
1 answer

Knapsack solution for input of strings

I had an online coding test where the problem was described as below. I could not finish the question but did leave some comments on how to come up with a potential solution. I wanted to know what have been the best to solve this problem. The main…
user6248190
  • 1,233
  • 2
  • 16
  • 31
4
votes
1 answer

Algorithm to get every possible subset of a list, in order of their product, without building and sorting the entire list (i.e Generators)

Practically, I've got a set of objects with probabilities, and I want to look at each possible group of them, in order of how likely it is that they're all true assuming they're independent -- i.e. in descending order of the product of the elements…
Tim Perry
  • 11,766
  • 1
  • 57
  • 85
4
votes
1 answer

Knapsack C# implementation task

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…
Ekli
  • 45
  • 1
  • 6
4
votes
1 answer

Knapsack with multiple contstraits

Given problem: n items, each having a value val, a weight w, and an volume vol. Basically the same as knapsack 0/1 problem however the task is to find the maximum value V you can get in the knapsack but the weight can't be more than W_max and also…
G.M
  • 530
  • 4
  • 20
4
votes
2 answers

Knapsack algorithm with proper ratio constraint and weight constraint

Problem: You're a juice maker looking for the best suppliers for your business. The suppliers are selling concentrates that have 3 ingredients: X:Y:Z in different proportions. Your juice needs to have these proportions as 1:1:1, otherwise it won't…
4
votes
1 answer

KnapSack dynamic programming in R with recursive function

I created this simple code in R to solve the Knapsack program with a recursive funtion n <- c(0,1,2,3,4) v <- c(10,40,30,50) w <- c(5,4,6,3) k <- 10 myfunction <- function(n,k){ if (n==0 | k==0){ output <- 0 } else if (w[i] > k) { …
4
votes
1 answer

Getting list of selected items in Knapsack DP matrix

I have tried implementing stack Overflow Answered Solution. But it is not working. TEST CASE : int val[] = {10,40,30,50}; int wt[] = {5,4,6,3}; W = 10; OUTPUT KNAPSACK DP MATRIX: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 5 5 5 5 5 0 0 0 0 4…
Mohit Kumar
  • 500
  • 6
  • 24
4
votes
1 answer

Algorithm for distributing beads puzzle (2)?

Let's say you have a circle (shown below) with N slots. Your goal is to end up with a specified number of beads in each slot, and you have an array of size N containing the amount of beads you need in each slot. For example, if the array was {1, 5,…
4
votes
1 answer

Algorithm that is a combination of the continuous knapsack problem and the variable size bin packing problem

I'm trying to solve a problem (in php, but programming language doesn't matter). I'm having a n number of persons that have paid money, and I have got a m number of persons that are going to pay the same amount as the sum of what the n number of…
jonepatr
  • 7,769
  • 7
  • 30
  • 53
4
votes
1 answer

Constrained Knapsack without weight

I just came across the following problem(it reminds me of the knapsack-problem, but there a some differences): You are given a number n of items which you have to put inside your knapsack with a maximum profit. Each item has a specific profit value…