knapsack-problem

Algorithm to get every possible subset of a list, in order of their product, without building and sorting the entire list (i.e Generators)

主宰稳场 提交于 2020-01-01 06:17:31
问题 Practically, I've got a set of objects with probabilities, and I want to look at each possible group of them, in order of how likely it is that they're all true assuming they're independent -- i.e. in descending order of the product of the elements of the subsets -- or in order of length if the probabilities are the same (so that (1, 0.5) comes after (0.5)). Example: If I have [ 1, 0.5, 0.1 ] I want [ (), (1), (0.5), (1, 0.5), (0.1), (1, 0.1), (0.5, 0.1), (1, 0.5, 0.1) ] In essence, this

Algorithm to get every possible subset of a list, in order of their product, without building and sorting the entire list (i.e Generators)

纵饮孤独 提交于 2020-01-01 06:17:12
问题 Practically, I've got a set of objects with probabilities, and I want to look at each possible group of them, in order of how likely it is that they're all true assuming they're independent -- i.e. in descending order of the product of the elements of the subsets -- or in order of length if the probabilities are the same (so that (1, 0.5) comes after (0.5)). Example: If I have [ 1, 0.5, 0.1 ] I want [ (), (1), (0.5), (1, 0.5), (0.1), (1, 0.1), (0.5, 0.1), (1, 0.5, 0.1) ] In essence, this

knapsack 01 with twist

痴心易碎 提交于 2019-12-30 08:53:53
问题 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

Coin change with limited number of coins

老子叫甜甜 提交于 2019-12-29 01:37:31
问题 I have written a program for generating subset sum which might be used in this problem which states: Suppose, you have 3 $1-coins, 2 $2-coins, 3 $5-coins, 1 $10-coin, there are 4 ways to obtain $10 from those coins. If there are n1 $X1 coins, n2 $X2 coins.... nm $Xm coins, how many ways can we obtain $X from these limited number of coins? If we create a set of { X1, X1..... X1, X2, X2.......... X2, ..., ..., ............, Xm, Xm... Xm}, and then run Subset summing on it, surely we can get a

Is there something wrong with my id array?

江枫思渺然 提交于 2019-12-25 09:03:15
问题 This program pulls two columns from the input.txt file where the first column indicates the value of the object, and the second column represents the weight. The values are imported and placed into two arrays: the value array and the weight array. The knapsack calculations are then made. There are 23 objects in total represented by the rows of the arrays. My code correctly calculates the total value that is being held in the knapsack, and will print out the correct IDs if the weight capacity

algorithm for the 0-1 Knapsack with 2 sacks?

血红的双手。 提交于 2019-12-25 04:26:53
问题 formally, say, we have 2 sacks with capacities c1 and c2. There are N items with profits pi and weights wi. As in 0-1 Knapsack problem, we need to fill in c1 and c2 with these items in such a way the overall profit is maximized. Assume pi and wi are positive integers! For the 2 knapsack problem does below recurrence relation hold good? DP[I][J][K] is maximum profit we could achieve from the first i items such that the weight of exactly j was used in knapsack #1 and a weight of exactly k was

C/C++ implementation of an algorithm similar to subset sum

非 Y 不嫁゛ 提交于 2019-12-25 03:22:28
问题 The problem is simpler than knapsack (or a type of it, without values and only positive weights). The problem consists of checking whether a number can be a combination of others. The function should return true or false . For example, 112 and a list with { 17, 100, 101 } should return false , 469 with the same list should return true , 35 should return false , 119 should return true , etc... Edit: subset sum problem would be more accurate for this than knapsack. 回答1: An observation that will

how to understand about reducing time complexity on 0~1 knapsack

这一生的挚爱 提交于 2019-12-25 00:17:03
问题 As for 0~1 knapsack problem, f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]} c[i] means the cost of ith goods, w[i] means the value of ith goods. And I read one doc,which said the time complexity can be optimization,especially when V is larger.as below i=1...N v=V...0 can be changed to i=1...n bound=max{V-sum{w[i..n]},c[i]} v=V...bound what does it mean?How can V(the maximum of bag) minus sum of w[i](the value of goods)? Really confuse,or something wrong on this doc? 回答1: You didn't say whose

Sum of a subset of numbers

╄→гoц情女王★ 提交于 2019-12-24 08:58:20
问题 Say I have one number 'n' and a table of numbers. I want to choose up to four of the numbers in the table, and the sum of those four will be the closest possible match to n. Given length 'L' of the table, the number of combinations it has to go through is (6*L + 11*L^2 + 6*L^3 + L^4)/24. ex. Say I have the variable n = 100 and the set of numbers t = {86, 23, 19, 8, 42, 12, 49} Given this list, the closest combination of four to n is 49 + 23 + 19 + 8 = 99. What is the optimal way of doing this

Two knapsack, dynamic programming

雨燕双飞 提交于 2019-12-23 20:28:55
问题 Edit: Codes are fixed. I'm trying to solve the problem below with dynamic programming but it gives 15 instead of 18. I'm looking for an error for about an hour but I couldn't figure it out. Problem: There is two knapsack with capacities c1, c2 and an n-element set. Each element has a w1 (its weight when put in the first knapsack), a w2 (its weight when put in the second knapsack) and a value. I need to find two disjoint subsets of the n-element set that fits in the corresponding knapsacks and