knapsack-problem

Dynamic T-SQL approach for combinatorics/knapsack

本秂侑毒 提交于 2019-12-03 07:42:37
I guess my question has to do with a variant of the knapsack problem, but I can't really come up with a solution for this: Let's say you are in a hardware store and need to buy 21 screws. They only offer them in bags: Bag X - 16 Screws - 1.56$ per screw - 25$ Total Bag Y - 8 Screws - 2.25$ per screw - 18$ Total Bag Z - 4 Screws - 1.75$ per screw - 7$ Total Now you have to figure out which Bags you should buy to get your 21 screws (or more!) for the lowest possible price. So what I got is a table with all the bags and a variable to define the required amount. What I need as a result should be a

Knapsack with continuous (non distinct) constraint

浪子不回头ぞ 提交于 2019-12-03 07:42:02
问题 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 integer where I can have 1,2,3 .... I don't suppose I do 0.0, 0.1, 0.2 ...? UPDATE 1 I thought of converting double to int by multiply by 100. Money is only 2 decimal places. But that will mean the range of values will be very large? UPDATE 2 The

Algorithm for solving this distributing beads puzzle?

你说的曾经没有我的故事 提交于 2019-12-03 06:56:38
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 is the minimum amount of money you have to spend to achieve this task? More interesting variation of this problem: Algorithm for distributing beads puzzle (2)? In this answer I assume beads can only be moved once. Otherwise it would be evident that beads should only move one square at a time, which makes this problem much less interesting: the sum of

Knapsack algorithm with an additional property

笑着哭i 提交于 2019-12-03 02:24:27
When there's 1 property, I do understand what's going on in there. I'm having a problem with understanding knapsack problem when there's more than 1 property. I have to write a program that uses knapsack algorithm with a 2 properties. Teacher told us, It has to be done in a 3d array. I can't imagine how would such array look like. Let's say here's my input: 4 3 4 // number of records below, 1st property of backpack, 2nd property of backpack 1 1 1 // 1st property, 2nd property, cost 1 2 2 // 1st property, 2nd property, cost 2 3 3 // 1st property, 2nd property, cost 3 4 5 // 1st property, 2nd

How can I solve it? KnapSack - values all the same but each other object has three weights

寵の児 提交于 2019-12-02 23:25:57
问题 I have a problem with solve my exercise. I read about dynamic programming and algorithms and I think my exercise is "specific knapsack problem". I solved it with brute force method but I can't solve it with dynamic programming. I have a ship (knapsack) which has 300 tons of weight. There are crystals which have in themselves 3 substances (X, Y, Z) - each other substance has weight and all crystals have the same value. I need to pack to ship as many as possible crystals but the proportions of

Knapsack with continuous (non distinct) constraint

做~自己de王妃 提交于 2019-12-02 21:09:18
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 integer where I can have 1,2,3 .... I don't suppose I do 0.0, 0.1, 0.2 ...? UPDATE 1 I thought of converting double to int by multiply by 100. Money is only 2 decimal places. But that will mean the range of values will be very large? UPDATE 2 The problem I need to solve is: Items have a price (double) & satisfaction (integer) value. I have a budget

Efficient table for Dynamic Programming in Haskell

≡放荡痞女 提交于 2019-12-02 17:46:46
I've coded up the 0-1 Knapsack problem in Haskell. I'm fairly proud about the laziness and level of generality achieved so far. I start by providing functions for creating and dealing with a lazy 2d matrix. mkList f = map f [0..] mkTable f = mkList (\i -> mkList (\j -> f i j)) tableIndex table i j = table !! i !! j I then make a specific table for a given knapsack problem knapsackTable = mkTable f where f 0 _ = 0 f _ 0 = 0 f i j | ws!!i > j = leaveI | otherwise = max takeI leaveI where takeI = tableIndex knapsackTable (i-1) (j-(ws!!i)) + vs!!i leaveI = tableIndex knapsackTable (i-1) j --

Can not understand knapsack solutions

时光总嘲笑我的痴心妄想 提交于 2019-12-02 13:38:30
问题 In wikipedia the algorithm for Knapsack is as follows: for i from 1 to n do for j from 0 to W do if j >= w[i] then T[i, j] := max(T[i-1, j], T[i-1, j-w[i]] + v[i]) [18] else T[i, j] := T[i-1, j] end if end for end for And it is the same structures on all examples I found online. What I can not understand is how does this code take into account the fact that perhaps the max value comes from a smaller knapsack? E.g. if the knapsack capacity is 8 then perhaps max value comes from capacity 7 (8 -

How can I solve it? KnapSack - values all the same but each other object has three weights

时光怂恿深爱的人放手 提交于 2019-12-02 13:30:54
I have a problem with solve my exercise. I read about dynamic programming and algorithms and I think my exercise is "specific knapsack problem". I solved it with brute force method but I can't solve it with dynamic programming. I have a ship (knapsack) which has 300 tons of weight. There are crystals which have in themselves 3 substances (X, Y, Z) - each other substance has weight and all crystals have the same value. I need to pack to ship as many as possible crystals but the proportions of substances of all packed crystals must be 1:1:1. But for example if I have three crystals with

Knapsack solution with Backtraking in c++

坚强是说给别人听的谎言 提交于 2019-12-02 10:07:28
Im having troubles trying to resolve the Knapsack problem using backtraking. For example, for the following values, the Knapsack function will return 14 as the solution, but the correct result should be 7. int n = 3, weights[] = {2, 3, 1}, values[] = {6, 15, 7}; int solution = 0, max = 2; void Knapsack (int i, int max, int value, int *solution) { int k; for (k = i; k < n; k++) { if (weights[k] <= max) { Knapsack (k, max - weights[k], value + values[k], solution); if (value+ values[k] > *solution) *solution= value + values[k]; } } } What is the problem here? #include<iostream> #include<vector>