knapsack-problem

Can not understand knapsack solutions

邮差的信 提交于 2019-12-02 05:53: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 - 1). I could not find anywhere logic to consider that perhaps the max value comes from a smaller

What's the fastest way to solve knapsack prob with two properties

泄露秘密 提交于 2019-12-01 22:16:41
Let's say we've got an input: 10 // saying 1st property should be 10(in total) 10 // saying 2d property should be 10 (in total) 5 // saying theres 5 records below // (1st property) (2nd property) (cost) 2 5 8 7 3 10 4 2 9 4 3 5 8 5 15 In this case, the output would look like that: 22 // lowest possible cost 1 3 4 // indexes of records, we've been using (indexing starts with 1) 2 5 8 4 2 9 4 3 5 +--------- 10 10 22 If there wasn't possible way to achieve those properties to be 10 and 10, program would output -1; I do know how to solve knapsack problem, however I've got no clue how to solve this

What is a good algorithm for compacting records in a blocked file?

陌路散爱 提交于 2019-12-01 22:11:51
Suppose you have a large file made up of a bunch of fixed size blocks. Each of these blocks contains some number of variable sized records. Each record must fit completely within a single block and then such records by definition are never larger than a full block. Over time, records are added to and deleted from these blocks as records come and go from this "database". At some point, especially after perhaps many records are added to the database and several are removed - many of the blocks may end up only partially filled. What is a good algorithm to shuffle the records around in this

What's the fastest way to solve knapsack prob with two properties

不羁岁月 提交于 2019-12-01 21:59:07
问题 Let's say we've got an input: 10 // saying 1st property should be 10(in total) 10 // saying 2d property should be 10 (in total) 5 // saying theres 5 records below // (1st property) (2nd property) (cost) 2 5 8 7 3 10 4 2 9 4 3 5 8 5 15 In this case, the output would look like that: 22 // lowest possible cost 1 3 4 // indexes of records, we've been using (indexing starts with 1) 2 5 8 4 2 9 4 3 5 +--------- 10 10 22 If there wasn't possible way to achieve those properties to be 10 and 10,

linear programming relaxed for MKP

为君一笑 提交于 2019-12-01 13:07:41
问题 how to calculate to find this relaxation. What should i know to find it. Suppose i have n items and m knapsack . So i wanted to know the number of m relaxation. Does anybody can give me some idea at least. I have been searching it for while. There is some article on internet but not much clear. Please at least someone say to me you should read this thing , you should know this thing e.i so i will very appreciate Thank you 回答1: I think your real question is "what is the exact definition of a

Knapsack algorithm for two bags

落爺英雄遲暮 提交于 2019-12-01 08:33:38
问题 I've found thread which provides pseudo-code for knapsack algorithm with 2 knapsacks. I've tried implement it in C++, but it doesn't work as suppose. Here's code: #include <cstdio> #define MAX_W1 501 #define MAX_W2 501 int maximum(int a, int b, int c) { int max = a>b?a:b; return c>max?c:max; } int knapsack[MAX_W1][MAX_W2] = {0}; int main() { int n, s1, s2, gain, weight; // items, sack1, sack2, gain, cost scanf("%d %d %d", &n, &s1, &s2); // filing knapsack for (int i = 0; i < n; i++) { scanf("

0-1 Multidimensional Knapsack

为君一笑 提交于 2019-12-01 06:43:44
So I'm trying to generate an algorithm that will find the best combination of n items (in my case 4) that can only be placed in the knapsack once (0-1) with a maximum weight capacity. Summarized probably more effectively, I want to place no more than four unique items in my knapsack so that the their weights are less than some value W while maximizing their total value. My first attempt and assumption was to put a volume limit of 4 with all item volumes as 1 for a multidimensional knapsack problem. But I ran into the problem of it not being 0-1 (meaning either in the bag or not). Then I tried

knapsack 01 with twist

早过忘川 提交于 2019-12-01 04:36:17
I'm doing a Knapsack in Java where we only use weights no value. The weightlimit is 1000. We get 5 weights scanned from keyboard which we use. The twist is that you can actually go over 1000 aslong as its the closets to 1000. So in one scenario we have 2 possible weights 990 and 1010 and the program is suposed to pick the higher one. The scanned numbers can never be higher then 1000. package kapsackidone; import java.util.Scanner; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.*; public class Kapsack { public static void main(String[] args) throws Exception {

Solving task scheduling or bin-packing optimizations in R

余生长醉 提交于 2019-12-01 04:22:20
I have an optimisation issue. It's about a product that contains 20 parts (the order of producing doesn't matter). I've got 3 similar machine that can produce all 20 parts. I've got the 20 parts represented in minutes (ie. it takes 3min to produce the first part and 75min to produce the second part, etc) ItemTime<-c(3,75,55,12,45,55,11,8,21,16,65,28,84,3,58,46,5,84,8,48) So to produce 1 product it takes 730 min. sum(ItemTime) The aim is to minimise the production of one product by allocating the good item to the three machines. sum(ItemTime/3) So actually I need to be as close as 243.333 min

Generating the power set of a list

◇◆丶佛笑我妖孽 提交于 2019-11-30 23:32:45
I have to write a brute-force implementation of the knapsack problem. Here's the pseudocode: computeMaxProfit(weight_capacity) max_profit = 0 S = {} // Each element of S is a weight-profit pair. while true if the sum of the weights in S <= weight_capacity if the sum of the profits in S > max_profit update max_profit if S contains all items // Then there is no next subset to generate return max generate the next subset S While the algorithm is fairly easy to implement, I haven't the slightest idea how to generate the power set of S, and to feed the subsets of the power set into each iteration