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

Coin change with limited number of coins

I have written a program for generating subset sum which might be used in this problem which states: Suppose, you have 3 $1-coins, 2 $2-coins, 3 $5-coins, 1 $10-coin, there are 4 ways to obtain $10 from those coins. If there are n1 $X1 …
5
votes
1 answer

How to fill a knapsack table when using recursive dynamic programming

* NOT HOMEWORK * I have implemented the knapsack in python and am successfully getting the best value however I would like to expand the problem to fill a table with all appropriate values for a knapsack table of all weights and items. I've…
5
votes
3 answers

What kind of algorithm? (Knapsack, Bin Packing!?)

The Problem is the following: You have n trip lengths in km which should be divided among m number of days such that the maximum sum of lengths per day is minimized. E.g. The Trip lengths [1,5,2,6,8,3,2] divided among 3 days result in [1,5,2] [6]…
Igl3
  • 4,900
  • 5
  • 35
  • 69
5
votes
2 answers

knapsack algorithm with two weights

I have questions which is the following: Solve the knapsack 0-1 problem(not fractional) Assuming that every object have weight w1 or w2 (there only two weights). Capacity=W, the algorithm must run on O(nlogn). I tried to solve, the greedy…
5
votes
3 answers

Pick m numbers from array of n numbers so their total differences are minimum

There are n towers with variable number of floors in an ancient city. We have to build floors on them so that at least m out of n towers (n>=m) are of equal height. Write a function to calculate the minimum number of floors that need to be built for…
crazyim5
  • 706
  • 2
  • 9
  • 26
5
votes
6 answers

canonical problems list

Does anyone known of a a good reference for canonical CS problems? I'm thinking of things like "the sorting problem", "the bin packing problem", "the travailing salesman problem" and what not. edit: websites preferred
BCS
  • 75,627
  • 68
  • 187
  • 294
5
votes
1 answer

Dynamic Programming Knapsack K-exact items

I found this very handy example code which implements a DP solution to the knapsack problem (kudos to the person who posted it). https://codereview.stackexchange.com/questions/20569/dynamic-programming-solution-to-knapsack-problem I am trying to…
elligottmc
  • 65
  • 7
5
votes
1 answer

knapsack with requirement to select one item each from many sets

I have an understanding of the basic 0-1 knapsack problem and its solution. I am trying to reason through a variant of the 0-1 problem where instead of being able to select any combination of items from a single list, you must select exactly one…
user139014
  • 1,445
  • 2
  • 19
  • 33
5
votes
2 answers

Calculate a rough estimate for shipping box size

I'm trying to find the best way to calculate the box size needed for shipping. I have 3 shipping containers with different sizes. I have the product's width, length, depth, and mass defined in the database. I would like to know how to find the…
Wil
  • 540
  • 1
  • 4
  • 9
5
votes
2 answers

Calculating items included in branch and bound knapsack

Using a branch and bound algorithm I have evaluated the optimal profit from a given set of items, but now I wish to find out which items are included in this optimal solution. I'm evaluating the profit value of the optimal knapsack as follows…
Alex
  • 243
  • 1
  • 7
  • 18
5
votes
3 answers

Dynamic programming sum

How would you use dynamic programming to find the list of positive integers in an array whose sum is closest to but not equal to some positive integer K? I'm a little stuck thinking about this.
CyberShot
  • 2,265
  • 6
  • 28
  • 36
5
votes
2 answers

knapsack variation algorithm

As a homework I have the following program to make in java: In a bookcase we have a stack of N books which have to be copied by hand by K writers. Each book has Ui pages where Ai is the book. We need to give each writer continuous books from the…
Jason Kur
  • 73
  • 1
  • 6
4
votes
1 answer

Is the runtime complexity of the Unbounded Knapsack problem really O(n×W)?

Given a knapsack weight W and a set of n items each with certain value value_i and weight w_i, we need to calculate the maximum value we can get from the items with total weight ≤ W. This is different from classical Knapsack problem, here we are…
4
votes
2 answers

C# 0-1 Knapsack Problem with known sum and number of zeros in set

I have a 5x5 table of values from 0 to 3 inclusive with all values unknown. I know both the sum of the values and the number of zeros for each row and column. How would I go about solving this 0-1 knapsack problem using C# and retrieving the…
hydroiodic
  • 476
  • 1
  • 3
  • 10
4
votes
3 answers

Allocation optimization problem using python

I have a list of sectors each with certain capacity. For instance: Sectors = { "1A": 80, "1B": 20, "2A": 10, "3A": 50, "3B": 20, "3C": 110 } I have also a list of teams each with certain amount of…