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

function for solving 0/1 knapsack problem using Brute-force recursive solution

I am trying this code for solving 0/1 knapsack problem using Brute-force recursive solution, but it keeps running with no output at all when I make the size of the problem(profit and weight arrays) 100. if any one can tell me why? and how to solve…
hanan
  • 11
  • 2
-1
votes
1 answer

Multiple optimal solutions for 0/1 Knapsack problem

A 0/1 knapsack problem with a maximum capacity constraint can have more than one optimal solution yielding the same profit but with/without the same capacity. How can we generate the set of all optimal soultions from the dp matrix. For…
-1
votes
1 answer

How to get the list of selected items in naive 0-1 knapsack?

I have a code of the naive solution of the Knapsack problem, i want to get the list of of selected items, currently it is returning the total sum of values of the selected items. Any help will be appreciated. PYTHON CODE: def knapSack(W, wt,…
Buddy
  • 13
  • 3
-1
votes
1 answer

Function to find "best" combination based on input?

I am trying to make an algorythm to solve the best combination of "upgrades" in a game based on input. These are the variables: 'imp_total': '30', # This says how many in total the other variables can be combined 'imp_coalpower': '0',…
-1
votes
1 answer

Dynamic Programming Knapsack

I'm trying to solve this problem however several testcases fail. I'm using a memo table to called arr to optimize recursion. What may I have done wrong? s=list(map(int,input().split())) n=s[0] W=s[1] value=[-1] weight=[-1] for i in range(n): …
-1
votes
2 answers

Runtime Error - can only concatenate list (not "int") to list

I've been doing 0-1 Knapsack problem using recursion+memoization. My Code: def knapSack(W, wt, val, n): ''' :param W: capacity of knapsack :param wt: list containing weights :param val: list containing corresponding values …
-1
votes
1 answer

0-1 Knapsack C++ not returning proper value

#include using namespace std; int knapsackTab(int val[],int wt[], int W, int n) { int dp[n+1][W+1]; for(int i=0;i
-1
votes
2 answers

Knapsack problem where there are three arrays: weight of an item array, value of an item array and amount of an item array

What would be the solution to this problem? I think it would be rather tricky because you would have to juggle three characteristics. Here is my attempt: p-value, s-weight, k-amount, max-maximum weight, m-number of items to take static int…
-1
votes
2 answers

Knapstack implementation in c++ using memoization

What is the difference between doing int t[102][1002] = {-1}; vs running a for loop and doing for(int i = 0; i < 102; i++) for(int j = 0; j < 1002; j++) t[i][j] = -1; The code below does not work when the former method is used.…
-1
votes
1 answer

Is this an instance of the knapsack problem?

I have what seems like a simple problem, but I'm struggling to solve it. Turning to Google it appears this may be a variation of the Knapsack problem, but I'm having trouble mapping those solutions to this particular problem. Let's say I have two…
ricklane
  • 173
  • 1
  • 11
-1
votes
4 answers

How to find what values of a list sum to a specified value

I am programming a knapsack encryption algorithm. I am new to Python programming. I have a list and an integer value I have determined. I want to find what elements in my list will sum up to my integer value. I can get it running for two elements or…
-1
votes
1 answer

Dynamic Programming Problem "Total rating must be highest after buying players with input price"

Players Input Table The problem is making total rating maximum , while buying one player from each position with some price. For example 29000€. You can use 27000€ but you can't use 29001€. Note: You can only buy one player from each positions so if…
Omer Adem
  • 9
  • 3
-1
votes
1 answer

This knapsack code is showing a float[float] invalid type error. what may be the reason for this?

This following piece of knapsack code is showing a float[float] error at line 32,33. why is this happening? I am not able to find out the reason why. Any help would be greatful. //maximum value of loot #include #include
RAJU C
  • 21
  • 1
  • 6
-1
votes
1 answer

Brute Force algorithm for multidimensional knapsack

I'm looking for a brute force algorithm for the multidimensional knapsack problem with 4 knapsacks. I have done some research on here and other websites but couldn't find anything helpful so far. Would be great if someone could point me in the…
-1
votes
1 answer

What is wrong with my dp programming code to find max profit given price limit?Variation of knapsack?

Hi I'm new to Python and I have this list of list: product_list=[[0, 'Cool Blue Marella Jug', 33, 15], [1, 'Weight Loss Pack', 55, 16], [2, 'Weight Loss Pack', 10, 16]] and the first number is basically the index, the second number(33,55,10) is…
Sook Lim
  • 541
  • 6
  • 28