subset-sum

Subset sum for exactly k integers?

纵饮孤独 提交于 2019-12-07 15:26:10
问题 Following from these question Subset sum problem and Sum-subset with a fixed subset size I was wondering what the general algorithm for solving a subset sum problem, where we are forced to use EXACTLY k integers, k <= n. Evgeny Kluev mentioned that he would go for using optimal for k = 4 and after that use brute force approach for k- 4 and optimal for the rest. Anyone could enlight what he means by a brute force approach here combined with optimal k=4 algo? Perhaps someone knows a better,

Haskell generate subsets

◇◆丶佛笑我妖孽 提交于 2019-12-07 04:46:46
问题 I have a function 'subsets' which generate all the subsets of a given set: subsets :: [Int] -> [[Int]] subsets [] = [[]] subsets (x:xs) = subsets xs ++ map (x:) (subsets xs) How can I combine map, foldl and filter in another function to return me all the subsets with elements that sum up to 0? **Example: ** set = [1,-1,5,2,-2,3] result = [[1,-1],[2,-2],[-1,-2,3]] 回答1: You have subsets already. So we need a function filterSubs :: [[Int]] -> [[Int]] filterSubs = --remove all subsets which don't

Subset sum problem where each number can be added or subtracted

跟風遠走 提交于 2019-12-06 15:52:03
问题 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) 回答1: You can solve it by using Boolean Integer Programming. There are several algorithms (e.g. Gomory or branch and bound) and

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

≯℡__Kan透↙ 提交于 2019-12-06 15:36:28
问题 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

Find smallest subset sum matching another subset sum

﹥>﹥吖頭↗ 提交于 2019-12-06 09:54:48
I have a real-world problem (not homework!) that requires finding the sum of a subset of set A that equals the sum of a subset of some other set B. A very similar question with a helpful answer is here . Consider this example: @a = qw(200 2000 2000 2000 4000); @b = qw(528 565 800 1435 2000 2000 2872); Using the code provided in the accepted answer to that question, I get the following output: sum(200 2000 4000) = sum(528 800 2000 2872) sum(200 4000 4000) = sum(528 800 2000 2000 2872) sum(200 4000) = sum(528 800 2872) sum(200 2000 2000 2000 4000) = sum(528 800 2000 2000 2000 2872) For my

Subset-Sum in Linear Time

浪尽此生 提交于 2019-12-06 02:44:36
问题 This was a question on our Algorithms final exam. It's verbatim because the prof let us take a copy of the exam home. (20 points) Let I = {r1,r2,...,rn} be a set of n arbitrary positive integers and the values in I are distinct . I is not given in any sorted order. Suppose we want to find a subset I' of I such that the total sum of all elements in I' is exactly 100*ceil(n^.5) (each element of I can appear at most once in I' ). Present an O( n ) time algorithm for solving this problem. As far

Subset sum for exactly k integers?

笑着哭i 提交于 2019-12-05 18:31:17
Following from these question Subset sum problem and Sum-subset with a fixed subset size I was wondering what the general algorithm for solving a subset sum problem, where we are forced to use EXACTLY k integers, k <= n. Evgeny Kluev mentioned that he would go for using optimal for k = 4 and after that use brute force approach for k- 4 and optimal for the rest. Anyone could enlight what he means by a brute force approach here combined with optimal k=4 algo? Perhaps someone knows a better, general solution? The original dynamic programming algorithm applies, with a slight extension - in

Haskell generate subsets

ぐ巨炮叔叔 提交于 2019-12-05 10:43:10
I have a function 'subsets' which generate all the subsets of a given set: subsets :: [Int] -> [[Int]] subsets [] = [[]] subsets (x:xs) = subsets xs ++ map (x:) (subsets xs) How can I combine map, foldl and filter in another function to return me all the subsets with elements that sum up to 0? **Example: ** set = [1,-1,5,2,-2,3] result = [[1,-1],[2,-2],[-1,-2,3]] You have subsets already. So we need a function filterSubs :: [[Int]] -> [[Int]] filterSubs = --remove all subsets which don't sum to 0 So next we'd need a predicate sumZero :: [Int] -> Bool sumZero xs = sum xs == 0 Now, using this

Find sum of subset with multiplication

人走茶凉 提交于 2019-12-05 01:15:33
问题 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 回答1: It is easier to calculate sum of multiplied triplets

Number of distinct sums from non-empty groupings of (possibly very large) lists

穿精又带淫゛_ 提交于 2019-12-04 22:37:08
Assume that you are given a set of coin types (maximum 20 distinct types) and from each type you have maximum 10^5 instances, such that the total number of all the coins in your list is maximum 10^6. What is the number of distinct sums you can make from non-empty groupings of these coins. for example, you are given the following lists: coins=[10, 50, 100] quantity=[1, 2, 1] which means you have 1 coin of 10, and 2 coins of 50, and 1 coin of 100. Now the output should be possibleSums(coins, quantity) = 9. Here are all the possible sums: 50 = 50; 10 + 50 = 60; 50 + 100 = 150; 10 + 50 + 100 = 160