subset-sum

Partitioning a List in Prolog

一个人想着一个人 提交于 2019-12-02 01:59:08
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 this error message: ERROR: is/2: Arithmetic: `[]/0' is not a function. I would like to get this piece

Finding maximum valued subset in which PartitionProblem algorithm returns true

ⅰ亾dé卋堺 提交于 2019-12-01 12:16:37
I´ve got the following assignment. You have a multiset S with of 1<=N<=22 elements. Each element has a positive value of up to 10000000. Assmuming that there are two subsets s1 and s2 of S in which the sum of the values of all the elements of one is equal to the sum of the value of all the elements of the other and it is the highest possible value. I have to return which elements of S would not be included in either of the two subsets. Its probably been solved before, I think its some variant of the Partition problem but I can´t find it. If anyone could point me in the right direction that´d

Finding maximum valued subset in which PartitionProblem algorithm returns true

给你一囗甜甜゛ 提交于 2019-12-01 09:29:04
问题 I´ve got the following assignment. You have a multiset S with of 1<=N<=22 elements. Each element has a positive value of up to 10000000. Assmuming that there are two subsets s1 and s2 of S in which the sum of the values of all the elements of one is equal to the sum of the value of all the elements of the other and it is the highest possible value. I have to return which elements of S would not be included in either of the two subsets. Its probably been solved before, I think its some variant

equal k subsets algorithm

匆匆过客 提交于 2019-12-01 01:05:45
does anyone know a good and efficient algorithm for equal k subsets algorithm ? preferably c or c++ which could handle a 100 element vector maybe with a complexity and time estimation ex. 9 element vector x = {2,4,5,6,8,9,11,13,14} i need to generate all k=3 disjoint subsets with sum = 24 the algorithm should check if there are k disjoint subsets each with sum of elements 24, and list them in ascending order(in subset and between subsets) or to see if the solution doesn't exists Solutions solution 1: {2 8 14} {4 9 11} {5 6 13} solution 2: {2 9 13} {4 6 14} {5 8 11} Thanks Unfortunately the

equal k subsets algorithm

橙三吉。 提交于 2019-11-30 19:01:50
问题 does anyone know a good and efficient algorithm for equal k subsets algorithm ? preferably c or c++ which could handle a 100 element vector maybe with a complexity and time estimation ex. 9 element vector x = {2,4,5,6,8,9,11,13,14} i need to generate all k=3 disjoint subsets with sum = 24 the algorithm should check if there are k disjoint subsets each with sum of elements 24, and list them in ascending order(in subset and between subsets) or to see if the solution doesn't exists Solutions

given a set of n integers, return all subsets of k elements that sum to 0

雨燕双飞 提交于 2019-11-30 14:38:38
given a unsorted set of n integers, return all subsets of size k (i.e. each set has k unique elements) that sum to 0. So I gave the interviewer the following solution ( which I studied on GeekViewpoint ). No extra space used, everything is done in place, etc. But of course the cost is a high time complexity of O(n^k) where k=tuple in the solution. public void zeroSumTripplets(int[] A, int tuple, int sum) { int[] index = new int[tuple]; for (int i = 0; i < tuple; i++) index[i] = i; int total = combinationSize(A.length, tuple); for (int i = 0; i < total; i++) { if (0 != i) nextCombination(index,

Equal sum subsets hybrid

穿精又带淫゛_ 提交于 2019-11-30 08:37:11
问题 The problem is the following: You are given a set of positive integers { a1 , a2 , a3 , ... , an } in which there are no same numbers ( a1 exists only once ,a2 exists only once,...) eg A = {12 , 5 ,7 ,91 }. Question: Are there two disjoint subsets of A , A1 = { b1,b2,...,bm } and A2 = { c1,c2,...,ck} so that b1+b2+...+bm = c1+c2+...+ck ? Note the following: It is not obligatory for A1 and A2 to cover A, so the problem isn't automatically reduced to the subset sum problem. eg A = {2,5,3,4,8,12

given a set of n integers, return all subsets of k elements that sum to 0

雨燕双飞 提交于 2019-11-29 21:44:58
问题 given a unsorted set of n integers, return all subsets of size k (i.e. each set has k unique elements) that sum to 0. So I gave the interviewer the following solution ( which I studied on GeekViewpoint). No extra space used, everything is done in place, etc. But of course the cost is a high time complexity of O(n^k) where k=tuple in the solution. public void zeroSumTripplets(int[] A, int tuple, int sum) { int[] index = new int[tuple]; for (int i = 0; i < tuple; i++) index[i] = i; int total =

Divide array into k contiguos partitions such that sum of maximum partition is minimum

☆樱花仙子☆ 提交于 2019-11-29 08:42:51
Here maximum sum subset is one of k subsets that give maximum sum e.g: arr = [10,5,3,7] and k = 2 possible ways to divide arr in k subsets is {10,[5,3,7]},{[10,5],[3,7},{[10,5,3],7} and {[10,5],[3,7} is the optimal one. Edit: it is equivalent of https://www.codechef.com/DI15R080/problems/MINMAXTF Assume you know the answer is x which means sum of the maximum subset is equal to x . You can verify this assumption by a greedy algorithm O(n) . (Traverse the array from left to right and pick items until the sum of that subset is lower than x ). Now you can binary search on x and find the minimum

Dynamic programming sum

丶灬走出姿态 提交于 2019-11-29 07:42:30
How would you use dynamic programming to find the list of positive integers in an array whose sum is closest to but not equal to some positive integer K? I'm a little stuck thinking about this. The usual phrasing for this is that you're looking for the value closest to, but not exceeding K. If you mean "less than K", it just means that your value of K is one greater than the usual. If you truly mean just "not equal to K", then you'd basically run through the algorithm twice, once finding the largest sum less than K, then again finding the smallest sum greater than K, then picking the one of