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
10
votes
3 answers

Designing a different kind of tag cloud

Rather than having a bunch of links that are all different sizes, I want all of my tags to be the same size. However, my goal is to minimize the amount of space required to make the cloud, aka minimizing the number of lines used. Take this…
animuson
  • 53,861
  • 28
  • 137
  • 147
10
votes
4 answers

Knapsack: how to add item type to existing solution

I've been working with this variation of dynamic programming to solve a knapsack problem: KnapsackItem = Struct.new(:name, :cost, :value) KnapsackProblem = Struct.new(:items, :max_cost) def dynamic_programming_knapsack(problem) num_items =…
randombits
  • 47,058
  • 76
  • 251
  • 433
9
votes
7 answers

Knapsack with continuous (non distinct) constraint

I watched Dynamic Programming - Kapsack Problem (YouTube). However, I am solving a slightly different problem where the constraint is the budget, price, in double, not integer. So I am wondering how can I modify that? Double is "continuous" unlike…
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
9
votes
2 answers

Get the most efficient combination of a large List of objects based on a field

I'm looking to maximize the number of stars given a certain budget and max limit on the combination. Example question: With a budget of 500 euro, visiting only the maximum allowed restaurants or less, dine and collect the most stars possible. I'm…
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
9
votes
1 answer

Minimization of the Unbounded Knapsack with Dynamic Programming

I am curious if it is possible to modify (or use) a DP algorithm of the Unbounded Knapsack Problem to minimize the total value of items in the knapsack while making the total weight at least some minimum constraint C. A bottom-up DP algorithm for…
danrpts
  • 93
  • 1
  • 6
9
votes
1 answer

Anything prevents optimizing tail-recursion?

I'm solving a knapsack problem in Haskell using Dynamic Programing. My first attempt was to build a two-dimensional table. But the memory easily gets blown up when the input is large(e.g. a 100 * 3190802 table). Knowing that any given row i only…
cfchou
  • 1,239
  • 1
  • 11
  • 25
8
votes
3 answers

Haskell Knapsack

I've written an answer to the bounded knapsack problem with one of each item in Scala, and tried transposing it to Haskell with the following result: knapsack :: [ ( Int, Int ) ] -> [ ( Int, Int ) ] -> Int -> [ ( Int, Int ) ] knapsack xs [] _ =…
Sean Kelleher
  • 1,952
  • 1
  • 23
  • 34
8
votes
1 answer

0-1 Knapsack w/ partitioning constraints

I have a problem that on the surface looks like 0-1 knapsack. I have a set of possible "candidates" that can be choosen (or not), each candidate has a "weight" (cost) and a potential "value". Were this the entire problem, I'd use a DP approach and…
8
votes
6 answers

0-1 Knapsack algorithm

Is the following 0-1 Knapsack problem solvable: 'float' positive values and 'float' weights (can be positive or negative) 'float' capacity of the knapsack > 0 I have on average < 10 items, so I'm thinking of using a brute force implementation.…
alhazen
  • 1,907
  • 3
  • 22
  • 43
8
votes
5 answers

Hockey pool Algorithm

This is a little fun project that I have started to try and maximize my chances of winning our office hockey pool. I'm trying to find the best way to select 20 players that will give me the most points within a maximum salary cap. For example,…
user949777
  • 101
  • 5
8
votes
3 answers

Knapsack with min weight

This variation of the knapsack problem requires a minimum weight. The goal is to minimize the cost while achieving at least the minimum weight. For example, we have 6 items with weights {1, 1, 1, 5, 13, 3} and costs {1, 1, 1, 5, 10, 12}. Assume a…
Nimbus
  • 117
  • 2
  • 5
8
votes
0 answers

Python implementation of Multiple-Choice Knapsack

I have been searching for python implementation of multiple choice knapsack problem. So far I have found a java implementation in github: https://github.com/tmarinkovic/multiple-choice-knapsack-problem And a psedu-code:…
t0m9er
  • 143
  • 2
  • 11
8
votes
1 answer

Dynamic Programming Algorithm similar to Knapsack Java Code

A mission-critical production system has n stages that have to be performed sequentially; stage i is performed by machine M_i. Each machine M_i has a probability r_i of functioning reliably and a probability 1-r_i of failing (and the failures are…
user5983771
8
votes
2 answers

Algorithm for solving this distributing beads puzzle?

Lets say you have a circle (like below) with N spots, and you have N beads distributed in the slots. Here's an example: Each bead can be moved clockwise for X slots, which costs X^2 dollars. Your goal is to end up with one bead in each slot. What…
8
votes
4 answers

C++ implementation of knapsack branch and bound

I am trying to a C++ implementation of this knapsack problem using branch and bounding. There is a Java version on this website here: Implementing branch and bound for knapsack I'm trying to make my C++ version print out the 90 that it should,…
user1527877
  • 83
  • 1
  • 1
  • 5
1 2
3
73 74