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

C++ partition algorithm

My task is: You have N items that weight is s1, s2... sN. Program has to divide items into two groups so that item weight would be as much similar as possible. I found a great explanation how to solve this problem(Author: Abhiraj Smit): // A…
MCTG
  • 65
  • 11
-1
votes
1 answer

Knapsack Algorithm Code

I am trying to code the algorithm for the knapsack problem in C++. I can't get this to work. I am getting max sum equals 0 for any input. I think the problem relies in the nested for-loop. Can someone point out what went wrong and how to fix the…
patzi
  • 353
  • 4
  • 13
-1
votes
2 answers

Knapsack using dynamic programming

There is a common algorithm for solving the knapsack problem using dynamic programming. But it's not work for W=750000000, because there is an error of bad alloc. Any ideas how to solve this problem for my value of W? int…
Kayrosik
  • 151
  • 1
  • 5
-1
votes
1 answer

Dynamic Programming Knapsack select only Unique Items in R

I have this code that selects multiple items to put in a knapsack from a dataframe. I wanted that it only selects an item from the dataframe only once:- knapsack_volume<-function(Data, W, Volume, full_K){ Data = Data # Data must have the colums…
Abdul Basit Khan
  • 646
  • 1
  • 6
  • 19
-1
votes
1 answer

Knapsack 0-1 in R

I have been trying to formulate a simple knapsack problem, but I cannot see why it is not working. i <- c(1,2,3,4) v <- c(100,80,10,120) w <- c(10,5,10,4) k <- 15 F <- function(i,k){ if (i==0 | k==0){ output <- 0 } else if (k
-1
votes
1 answer

0-1 knapsack implementation using dynamic programming

It is working fine for the given value of weight, value, max_weight and total_items but it is giving segmentation fault when I change the weight, value and other variables. I checked that when i am changing the variables then in knapsack function…
-1
votes
1 answer

Understanding a pseudocode of Knapsack in recrusive

int knapsack(i, k){ if(i=N){ val= 0; //end of recrusive return val; } if (w[i]>k) // no space anymore val= knapsack(i+1, k); else { a = knapsack(i+1, k) // i don't take with b =…
-1
votes
1 answer

Variation on the subset sum prob. (multiple constraints)

Let me give you an example of a variation on the normal subset-sum problem that I am trying to solve: Given is a set S = {1,2,3,4,5,6,7,8,9} with the maximal capacity c0 = 40. Furthermore we have 3 additional constraints on 3 different subsets of…
-1
votes
1 answer

Rectangle packing in Java

I'm trying to implement a rectangle packing algorithm. I have some rectanges on my input Each rectangle have a heigth and a width(and an index number, so i will be able to identify them when i log them) I have a matrix (an Integer[][]), and i want…
-1
votes
1 answer

Greedy algorithm for knapsack in java

I am trying to write a very simple greedy algorithm for the knapsack problem. My inputs are two parallel arrays. One array contains the value of the item and the other array contains the weights. The greedy algorithm that I’m trying to write would…
Tezen
  • 47
  • 1
  • 8
-1
votes
1 answer

Is there a solution instead of move for knapsack backtracking algorithm in C++

I used the solution of Backtracking of Knapsack solution with C++. I had the solution again from here. Knapsack solution with Backtraking in c++ My compiler gives error at line move. Is there solution without using move? how can I modify the code…
xxxx
  • 27
  • 2
  • 8
-1
votes
1 answer

Java Combinatorial optimization

i have a combinatorial optimization problem. I have X bids containing a price and n rows. The price is the total price for these n rows. n can be between 1 and 12. All rows have a number attached (1-12). This means all in all there are 12 different…
Daniel
  • 43
  • 1
  • 1
  • 4
-1
votes
1 answer

Most efficient method of getting possible sum from list in python

Similar to knapsack problem. If I have list = [5,7,9,12,6] targetValue = 15 Since 6+9 = 15, what is the most efficient way of getting [6,9] Does python have a built in method for such a problem?
Nicky Feller
  • 3,539
  • 9
  • 37
  • 54
-1
votes
1 answer

Recursive Knapsack (Divide and Conquer)

I have been trying to figure this for over 3 days now. It is part of my fortnightly assignment for my uni. However, I have hit a road block. Any help will be must appreciated. Specifics of the assignment, I have figured out so far: I must use an…
-1
votes
1 answer

Knapsack with one additional constraint

This is the old and famous knapsack problem : Knapsack Problem Here I have Knapsack problem with one constraint. I have Knapsack with size W = 100000000 and N = 100 items I wrote the dynamic solution for it the Complexity of my algorithm is …
Daniel.V
  • 2,322
  • 7
  • 28
  • 58