knapsack-problem

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

家住魔仙堡 提交于 2020-02-03 02:25:19
问题 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 less but I can not run it for more than two elements. Assume: privkey = [2,3,6,13,27,52] cipher = 9 My current function can run the scenario above: searchList = [] for i, number in enumerate(privkey[:-1]): complementary = cipher - number if

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

十年热恋 提交于 2020-01-24 19:55:46
问题 i have a code of the naive solution of the Knapsack problem, i want to get the list of index of selected items, currently it is returning the total sum of values of the selected items. Any help will be appreciated. JAVA CODE: /* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ /* A Naive recursive implementation of 0-1 Knapsack problem */ class Knapsack { // A

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

旧巷老猫 提交于 2020-01-11 09:38:29
问题 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

Dynamic programming with large inputs

扶醉桌前 提交于 2020-01-05 04:40:28
问题 I am trying to solve a classic Knapsack problem with huge capacity of 30.000.000 and it works well up until 20.000.000 but then it runs out of memory: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space I have tried to divide all values and capacity by 1.000.000 but that generates floats and I don't think that is the correct approach. I have also tried to make the arrays and matrix of type long but that does not help. Perhaps another data-structure? Any pointers welcome...

0/1 Knapsack - A few clarification on Wiki's pseudocode

大兔子大兔子 提交于 2020-01-05 02:44:30
问题 Here's the code on en.wikipedia's article on the Knapsack problem: // Input: // Values (stored in array v) // Weights (stored in array w) // Number of distinct items (n) // Knapsack capacity (W) for w from 0 to W do m[0, w] := 0 end for for i from 1 to n do for j from 0 to W do if j >= w[i] then m[i, j] := max(m[i-1, j], m[i-1, j-w[i]] + v[i]) else m[i, j] := m[i-1, j] end if end for end for I've got two points which my tired brain cannot work out. They're minor, I'm sure, but I could really

How to get the optimized choice from arrays

。_饼干妹妹 提交于 2020-01-04 14:14:50
问题 I have several arrays of hashes (let's say I have three) as follows: a = [{ cost: 10, value: 20}, { cost: 9, value: 20}, { cost: 10, value: 22}, { cost: 2, value: 10} ] b = [{ cost: 4, value: 20}, { cost: 9, value: 20}, { cost: 15, value: 22}, { cost: 12, value: 10} ] c = [{ cost: 10, value: 21}, { cost: 9, value: 20}, { cost: 10, value: 22}, { cost: 3, value: 10} ] I need to find out which choice of hashes, one from each array, gives me the maximum sum of :value keeping the sum of :cost

Knapsack with selection from distinct groups

情到浓时终转凉″ 提交于 2020-01-04 05:17:33
问题 I have a variation on the Knapsack Problem that I'm struggling to find an efficient solution for. Let's say you have multiple groups of items. Each group can have an arbitrary number of items, each with a value and weight. The problem is to find the set of items with maximum total value, weight < some limit, and (the tricky part) only sets than include one item from EVERY group are valid. That is, imagine you have hundreds of items to pick from, but you must take one sandwich, one beverage,

Maximum Value taken by thief

血红的双手。 提交于 2020-01-03 05:13:30
问题 Consider we have a sacks of gold and thief wants to get the maximum gold. Thief can take the gold to get maximum by, 1) Taking the Gold from contiguous sacks. 2) Thief should take the same amount of gold from all sacks. N Sacks 1 <= N <= 1000 M quantity of Gold 0 <= M <= 100 Sample Input1: 3 0 5 4 4 4 Output: 16 Explanation: 4 is the minimum amount he can take from the sacks 3 to 6 to get the maximum value of 16. Sample Input2: 2 4 3 2 1 Output: 8 Explanation: 2 is the minimum amount he can

Need T-SQL Query find all possible ways

£可爱£侵袭症+ 提交于 2020-01-02 08:06:07
问题 create table #sample ( product varchar(100), Price float ) insert into #sample values ('Pen',10) insert into #sample values ('DVD',29) insert into #sample values ('Pendrive',45) insert into #sample values ('Mouse',12.5) insert into #sample values ('TV',49) select * from #sample Consider this situation ... I have 1000$, I want to buy something listed above. I want to spend the entire amount So I need a query which gives how much units in all products will cost 1000$ Any help ? 回答1: This is

NP problems and need some detail?

随声附和 提交于 2020-01-01 19:40:29
问题 I see one solved ex on Algorithms. Which of the following is in NP? a) Decision Version of TSP b) Array is Sorted? c) Finding the maximum flow network d) Decision version of 0/1 knapsack? My Note, says all of these is in NP, anyone could add a bit detail for each one why? and I confusing about 0/1 knapsack, is it NP? NP-HARD? or NP-Complete? Thanks. 回答1: They all are in NP, because: It is verifiable in polynomial time. Given some route, we can easily check whether its length does not exceed