subset-sum

Find all combinations of a given set of integers summing up to a given sum

送分小仙女□ 提交于 2019-12-04 20:48:44
I am looking for an answer to the following problem. Given a set of integers (no duplicates) and a sum, find all possible combinations of the set's elements summing up to the sum. Solutions order does not matter (solutions {2, 2, 3} and {3, 2 ,2} are equal). Please note that the final combination does not need to be a set, as it can contain duplicates. Example: Set {2,3,5} Sum 10 Result: {2, 2, 2, 2, 2}, {2, 2, 3, 3}, {2, 3, 5}, {5, 5} I've looked at Subset Sum problem as well as Coin Change problem, but couldn't adapt them to suit my needs. I am not really familiar with dynamic programming,

Subset sum problem where each number can be added or subtracted

不问归期 提交于 2019-12-04 20:42:51
Given a set A containing n positive integers, how can I find the smallest integer >= 0 that can be obtained using all the elements in the set. Each element can be can be either added or subtracted to the total. Few examples to make this clear. A = [ 2, 1, 3] Result = 0 (2 + 1 - 3) A = [1, 2, 0] Result = 1 (-1 + 2 + 0) A = [1, 2, 1, 7, 6] Result = 1 (1 + 2 - 1 - 7 + 6) You can solve it by using Boolean Integer Programming. There are several algorithms (e.g. Gomory or branch and bound) and free libraries (e.g. LP-Solve) available. Calculate the sum of the list and call it s. Double the numbers

Subset-sum problem in PHP with MySQL

血红的双手。 提交于 2019-12-04 16:03:56
following Problem: I have a MySQL database with songs in it. The database has the following structure: id INT(11)(PRIMARY) title VARCHAR(255) album VARCHAR(255) track INT(11) duration INT(11) The user should be able to enter a specific time into a php form and the php function should give him a list of all possible combinations of songs which add up to the given time ±X min. So if the user wants to listen to 1 hour of music ±5 minutes he would enter 60 minutes and 5 minutes of threshold into the form and would recieve all possible song sets which add up to a total of 55 to 65 minutes. It

Subset sum with special conditions

有些话、适合烂在心里 提交于 2019-12-04 14:44:39
(Before you reply with a link to another SO question or close this as a duplicate please read the question carefully. This is different than the standard variant of this problem and I've searched for a long time so I'm pretty sure there isn't a question like this here) I need to find if the smallest possible S that is the sum of some subset of X[i] that is >= T (some target value, smaller than the sum of the full set). The set isn't very large (about 40 elements), but still too big for exponential backtracking solution. The numbers and the sum are huge (about 10^15), so dynamic programming won

Subsets with equal sum

别来无恙 提交于 2019-12-03 22:58:11
I want to calculate how many pairs of disjoint subsets S1 and S2 (S1 U S2 may not be S) of a set S exists for which sum of elements in S1 = sum of elements in S2. Say i have calculated all the subset sums for all the possible 2^n subsets. How do i find how many disjoint subsets have equal sum. For a sum value A, can we use the count of subsets having sum A/2 to solve this ? As an example : S ={1,2,3,4} Various S1 and S2 sets possible are: S1 = {1,2} and S2 = {3} S1 = {1,3} and S2 = {4} S1 = {1,4} nd S2 = {2,3} Here is the link to the problem : http://www.usaco.org/index.php?page=viewproblem2

Find sum of subset with multiplication

佐手、 提交于 2019-12-03 20:07:29
Let's say we have got a set {a_1, a_2, a_3, ..., a_n} The goal is to find a sum that we create in the following way: We find all subsets whose length is 3, then multiply each subset's elements (for the subset {b_1, b_2, b_3} the result will be b_1*b_2*b_3 ). At the end we sum up all these products. I am looking for a shortest time-execution algorithm. Example SET: {3, 2, 1, 2} Let S be our sum. S = 3*2*1 + 3*2*2 + 2*1*2 + 3*1*2 = 28 It is easier to calculate sum of multiplied triplets when repetitions are allowed (like a_1*a_1*a_1). This sum is just (sum^3) . Since repetitions are not allowed,

Find the minimum number of elements required so that their sum equals or exceeds S

。_饼干妹妹 提交于 2019-12-03 11:29:39
问题 I know this can be done by sorting the array and taking the larger numbers until the required condition is met. That would take at least nlog(n) sorting time. Is there any improvement over nlog(n) . We can assume all numbers are positive. 回答1: Here is an algorithm that is O(n + size(smallest subset) * log(n)) . If the smallest subset is much smaller than the array, this will be O(n) . Read http://en.wikipedia.org/wiki/Heap_%28data_structure%29 if my description of the algorithm is unclear (it

find a solution to subset sum using dynamic programming

家住魔仙堡 提交于 2019-12-03 03:45:48
What I want to do I want to find a subset of an array that sums to a target T . I also want to use to a dynamic programming approach (and a bottom-up solution at that) to do this. What I currently have Currently I only found a way to see if amongst all subsets of size N , whether or not there is at least one subset that has the desired sum. See code below. public boolean solve(int[] numbers, int target) { //Safeguard against invalid parameters if ((target < 0) || (sum(numbers) < target)){ return false; } boolean [][] table = new boolean [target + 1] [numbers.length + 1] ; for (int i = 0; i <=

Find the minimum number of elements required so that their sum equals or exceeds S

此生再无相见时 提交于 2019-12-03 01:56:40
I know this can be done by sorting the array and taking the larger numbers until the required condition is met. That would take at least nlog(n) sorting time. Is there any improvement over nlog(n) . We can assume all numbers are positive. Here is an algorithm that is O(n + size(smallest subset) * log(n)) . If the smallest subset is much smaller than the array, this will be O(n) . Read http://en.wikipedia.org/wiki/Heap_%28data_structure%29 if my description of the algorithm is unclear (it is light on details, but the details are all there). Turn the array into a heap arranged such that the

Partitioning a List in Prolog

↘锁芯ラ 提交于 2019-12-02 03:44:17
问题 I am trying to create a Prolog predicate where, given a list, it is seen whether or not the list can be split into two lists that sum to the same amount. I have a working list sum predicate, so I am using that within my partitioning predicate. I first tried to code the predicate to see if the first element of the list equals the sum of the rest of the list ([2,1,1]). This is what I have for that situation. partitionable([X|Y]) :- sum([X],SUM), sum([Y],SUM2), SUM = SUM2. However, I am getting