Skip to main content

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.

Filter by
Sorted by
Tagged with
3 votes
1 answer
1k views

I'm trying to solve the following problem: As you know Bob likes to destroy the buildings. There are N buildings in a city, ith building has ai floors. Bob can do the following operation. Choose a ...
driver's user avatar
  • 222
1 vote
1 answer
209 views

The assignment is to be implemented on the following instructions: You are to write a Knapsack class and the main() to support and demonstrate the functionality required here. A function generate(int)...
user avatar
6 votes
1 answer
381 views

The need (context). Several times a day, we need to migrate a set of digital assets from one system to another. The process uses memory (the assets are held in memory for a short time) and we need to ...
Konchog's user avatar
  • 355
1 vote
1 answer
190 views

Background The continuous knapsack problem is the following linear program: $$ \begin{align} \text{maximize} \quad & f(x) = \sum_{i=1}^n u_i x_i \\ \text{subject to} \quad & \...
Max's user avatar
  • 207
1 vote
0 answers
773 views

I was watching Computerphile's video on using genetic algorithms to solve the Knapsack problem, and I decided to give it a whack. For anyone running the code, the ...
Jose Fernando Lopez Fernandez's user avatar
2 votes
2 answers
249 views

I'm solving a knapsack problem here. It works, but gives time limit exceeds on a certain test case. Problem statement There are N items, numbered 1,2,…,N. For each i (1≤i≤N), Item i has a weight of ...
Youssef13's user avatar
  • 167
3 votes
1 answer
337 views

I have implemented a solution for the knapsack problem while breaking an item is allowed. I've used the dictionary data structure where I used weight as a key and value per weight as a value. After ...
Mahmudul Hasan's user avatar
9 votes
2 answers
784 views

The following code uses dynamic approach to solve 0/1 knapsack problem. (I know the maximum profit function I have used is not as good as the one I have defined ...
cpplover's user avatar
  • 354
3 votes
0 answers
84 views

The following is a solution to the knapsack problem and supposed to be my entry to this weeks perl challenge. I wonder if this is a good use of dynamic variables or is it too confusing? I use them ...
Holli's user avatar
  • 221
1 vote
1 answer
625 views

My goal is to code the knapsack problem algorithm. The problem is that a knapsack has a given weight limit, W. I have a collection of items which have a given ...
DY92's user avatar
  • 151
4 votes
1 answer
121 views

This question was given in my college assignment to be done in C. After doing that, I rewrote the program again in rust. I come from a C/Python background and this is my first rust program. Please ...
Kushagra Gupta's user avatar
21 votes
3 answers
4k views

I solved the following problem using backtracking: We are given a list of military units, their weight and their (numerical) strength. We have a number of ships with a limited carrying capacity. ...
Jack's user avatar
  • 313
3 votes
1 answer
2k views

Here is what a knapsack/rucksack problem means (taken from Wikipedia): Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that ...
Justin's user avatar
  • 2,619
4 votes
1 answer
748 views

Problem description: Molly wants to purchase laptops for her school. Find out how many laptops she can purchase by comparing the vendors available. Each vendor sells the laptops in batches, with a ...
fpezzini's user avatar
  • 346
4 votes
3 answers
8k views

This post is based on the 0-1 Knapsack problem. I came across this problem in Assignment #4 of Professor Tim Roughgarden's course Greedy Algorithms, Minimum Spanning Trees, and Dynamic Programming on ...
user avatar
1 vote
2 answers
231 views

This is a simple implementation of a knapsack written in Ruby. I've included all the relevant code here, but my question is concerning the contents setter ...
kingsfoil's user avatar
  • 179
4 votes
1 answer
153 views

I'm using some old USA Computing Olympiad (USACO) problems to help teach me programming. This is the second one I've posted, I hope that's okay -- let me know if this is considered abusing the system. ...
silverware's user avatar
4 votes
0 answers
389 views

Here is an implementation of Knapsack in Rust. How could it be improved? Some things I have already spotted: What would be the best way to extract result? Is using an option preferred unwrapping the ...
epsilon8's user avatar
  • 141
3 votes
2 answers
198 views

Is this code written in a decent style? Appreciate some feedback : ) ...
reviewee's user avatar
2 votes
0 answers
62 views

Problem Statement: I am given a set of prices for \$n\$ items I have to find out what is the maximum value I can get with \$M\$ money by selecting exactly \$C\$ items without exceeding the amount of ...
ng.newbie's user avatar
  • 171
1 vote
1 answer
750 views

...
navindren's user avatar
  • 183
4 votes
2 answers
1k views

This function is checking whether it has enough money in a list called self.deviceMoney to make change. It works exactly the way I want, but only in case where not ...
vbvvv20's user avatar
  • 59
3 votes
1 answer
1k views

I solved the knapsack problem where duplicates elements are allowed: Given weights and values related to n items and the maximum capacity allowed for these items. What is the maximum value we can ...
Harsha's user avatar
  • 1,311
3 votes
0 answers
697 views

Following is my solution to Knapsack problem using all combinations of the sent list of items (in Racket programming language). Although it is very short, I am sure it can be improved in many ways. <...
rnso's user avatar
  • 283
5 votes
1 answer
870 views

Write a function that meets the specifications below: ...
Tao Xu's user avatar
  • 111
6 votes
2 answers
8k views

I implemented the well-known knapsack problem and now I would like to improve it using list comprehension or lambda. I don't want to use NumPy. Could you help me? ...
Michael's user avatar
  • 295
6 votes
3 answers
2k views

Given a dictionary of cows and their weight: ...
Andrew Church's user avatar
2 votes
1 answer
188 views

Given a set of items with a weight and a value, this problem gives the subset of items which maximize value so that their combined weights is less or equal than a given maximum weight. This solution ...
MAG's user avatar
  • 2,974
3 votes
2 answers
4k views

Here is a recursive implementation of the Subset sum problem: ...
Ziezi's user avatar
  • 1,194
4 votes
1 answer
584 views

I recently came across this problem: It is Lavanya's birthday and several families have been invited for the birthday party. As is customary, all of them have brought gifts for Lavanya as well ...
hellozee's user avatar
  • 439
3 votes
3 answers
391 views

Description: Given the Knapsack capacity and the weight and value of some items, find a way to maximize the value in the given Knapsack. Code: ...
CodeYogi's user avatar
  • 5,209
5 votes
2 answers
2k views

I am a new Pythoner. I have solved a OJ project with Python. Can you tell me how can I solve it more Pythonically? Problem Description: There are n coin denominations, each with an unlimited supply ...
Cherlex's user avatar
  • 145
5 votes
1 answer
733 views

Background My mother has a hobby of buying and reselling books via online trading sites. After a price is agreed on, the books have to be put into an envelope for mailing. On this envelope, stamps ...
Pimgd's user avatar
  • 22.6k
2 votes
0 answers
453 views

My solution correctly solves the problem, but I would like to optimize its performance. For a capacity of 7 and bars = [ 4, 1, 1, 2, 1], the output is 7. ...
mgabz's user avatar
  • 121
8 votes
2 answers
4k views

My version of Knapsack works only when the weights or values of items are whole numbers. Restrictions You are given an array of objects each of which contains a weight and value. You are also given a ...
nicezebra's user avatar
2 votes
1 answer
693 views

This is my solution to an assignment on the fractional Knapsack problem. I take as problem input the following pieces of information: The number of item types The total weight limit For each item type,...
user3759417's user avatar
6 votes
1 answer
751 views

I have a problem were the time in minutes of N songs are given and I have to write the maximum number of time in K CDs. Input description The first input line consists of two positive integers N e K (...
Felipe's user avatar
  • 607
6 votes
1 answer
2k views

A tourist wants to make a good trip at the weekend with his friends. They will go to the mountains to see the wonders of nature, so he needs to pack well for the trip. He has a good knapsack for ...
Caridorc's user avatar
  • 28.2k
1 vote
1 answer
1k views

Solution to bounded knapsack 01 problem. Once again comprehensive description is difficult in this space, refer here. Looking for code review. optimizations and best practices. ...
JavaDeveloper's user avatar
4 votes
1 answer
3k views

I've got the imperative styled solution working perfectly. What I'm wondering is how to make a recursive branch and bound. This is my code below. Evaluate function returns the optimistic estimate, ...
Looft's user avatar
  • 143
58 votes
3 answers
69k views

I wrote a solution to the Knapsack problem in Python, using a bottom-up dynamic programming algorithm. It correctly computes the optimal value, given a list of items with values and weights, and a ...
voithos's user avatar
  • 862