I have a simple algorithmic problem.
I have a set of positive integers S and a positive maximum integer i.
Let's say the sum of S (or a subset of S) is the sum of its elements.
I need to find a subset s of S whose sum does not exceed i and is "maximally summing" - meaning no other subset of S has a greater sum than s without exceeding i.
The trivial solution I came up with is to go over each set of the power set of S and sum the integers, keeping track of the set with the properties I seek, but this algorithm is obviously exponential.
There must be a well-known name for this problem, as I don't think I am the first to come across this need. Could someone help me out?
Solve subset sum problem for your set using dynamic programming.
Then scan filled table from i-th entry to smaller values until you find non-zero entry (i.e. such sum exists). This is the largest sum of subsets that not exceeding given value.
Related
I have an array of integers. Lets denote it by A. There is another array W containing weights associated with each entry of A.
In other words, associated with each entry A[i] is a certain weight entry W[i]. (Note that the weights are out of a limited set S_w = {w1,w2,w3,w4} so only few possible values)
The problem statement is as follows: Pick a random number of entries out of A such that when summed together, give you the highest value (SUM_A) under the constraint that the sum of their respective weights (SUM_W) doesn't exceed a threshold, W_threshold.
One possibility is brute force: Compute all permutations of A and for each permutation, select first n entries such that their sum weight SUM_W doesn't exceed W_threshold. Finally, the permutation that gives the maximum SUM_A shall be selected. But the complexity of this scheme is very high due to permutation computation step since the length of A is not constrained.
One other (sub-optimal) possibility is to sort A in descending order and then select first n entries such that their SUM_W doesn't exceed W_threshold. This has lower complexity but the result would be suboptimal.
Could someone give me tips if their already exists an algorithm to resolve the above stated problem? Or if anyone has ideas better than the ones I described above. Many thanks
From a set A of N positive numbers, a set of Sum of all possible subsets of the set A is formed and given. We have to find the set A.
My approach is to sort first and then keep sbtracting the Nth largest number from the largest number to find the elements of the set. What is wrong with that approach?
Consider the set of elements to be {a,b,c,d}, in such a case the possible subset sums of the set would be (1){a}, (2){b+c}, (3){b+c+d}, (4){a+b+c+d} and more. However the largest subset sum would be (4) and as visible, the subtraction of (4) - (2) will yield {a+d} which is just another subset sum of the set and not the actual element.
A possible way to solve the problem is to sort the array, and start picking up elements from the smallest in a sack. Every time we pick a new element, we compute all the possible subset sums possible which always includes this element and other elements from our maintained sack, and then remove these computed subset sums from the given subset sum list. We then proceed to pickup the next smallest element from the given subset list which hasn't been removed yet.
EDIT: Added possible solution to the given question.
I'm trying to solve the following:
The knapsack problem is as follows: given a set of integers S={s1,s2,…,sn}, and a given target number T, find a subset of S that adds up exactly to T. For example, within S={1,2,5,9,10} there is a subset that adds up to T=22 but not T=23. Give a correct programming algorithm for knapsack that runs in O(nT) time.
but the only algorithm I could come up with is generating all the 1 to N combinations and try the sum out (exponential time).
I can't devise a dynamic programming solution since the fact that I can't reuse an object makes this problem different from a coin rest exchange problem and from a general knapsack problem.
Can somebody help me out with this or at least give me a hint?
The O(nT) running time gives you the hint: do dynamic programming on two axes. That is, let f(a,b) denote the maximum sum <= b which can be achieved with the first a integers.
f satisfies the recurrence
f(a,b) = max( f(a-1,b), f(a-1,b-s_a)+s_a )
since the first value is the maximum without using s_a and the second is the maximum including s_a. From here the DP algorithm should be straightforward, as should outputting the correct subset of S.
I did find a solution but with O(T(n2)) time complexity. If we make a table from bottom to top. In other words If we sort the array and start with the greatest number available and make a table where columns are the target values and rows the provided number. We will need to consider the sum of all possible ways of making i- cost [j] +j . Which will take n^2 time. And this multiplied with target.
I am having trouble starting off this particular homework problem. Here is the problem:
Suppose that you are given an algorithm as a black box – you cannot see how it is designed – it has the following properties: if you input any sequence of real numbers and an integer k, the algorithm will answer YES or NO indicating whether there is a subset of numbers whose sum is exactly k. Show how to use this black box to find the subset of a given sequence X1, …., Xn whose sum is k. You can use the black box O(n) times.
I figure that the sequence should be sorted first, and anything < k should only be considered. Any help to get started would be greatly appreciated. Thanks.
Sorting is the wrong approach. Think about it this way: how can you use the oracle to determine whether a particular item in the set is part of the sum? Once you know whether that item is part of the sum, how can you use the oracle to figure out whether some other item is part of the sum?
The blackbox is something like this, in C# (ignore that I used int instead of real for the sequence, it's inconsequential to the problem).
bool blackbox(List<int> subSequence, int k)
{
// unknown
}
You are tasked with passing in a subset of the sequence and finding what part of the sequence equals k.
Start with the whole sequence, just to see if k is in it at all.
Then, if it contains k, try a subsequence to see if that subsequence contains k.
Repeat until you have the subsequence that contains k.
(I'm banging my head here. Let X={x1,x2,...,xn} is an integer set. Let A1,A2,...Am be the m subsets of X. For any i and j, Ai and Aj are not necessarily disjoint. Now the goal is to find the maximal value on each Ai (i=1,...,m) efficiently, with the number of operations as fewer as possible.
For example, given X={2,4,6,3,1}, and its subsets A1={2,3,1}, A2={2,6,3,1}, A3={4,2,3,1}. We need to find Max{A1}, Max{A2}, Max{A3}, respectively.
The brute-force way for finding Max{A1}, Max{A2}, Max{A3} is to scan all the elements in each Ai, and (m*d) operations are required, with m the number of subsets of X, and d the average length of the subsets {Ai} of X.
Now, I have some observations:
(1) For any set Y⊆X, max{Y}≤max{X},
For instance, since Max{X}=6 and 6 is in A2, then Max{A2}=6 can be found directly.
(2) For any two sets A and B, if A∩B is non-empty, Max{A} and Max{B} can be identified as follows:
First, we find the common parts between A and B, deonted as c=max{A∩B}.
Then, we find Max{A}=Max{Max{A-(A∩B)}, c} and Max{B}=Max{Max{B-(A∩B)}, c}.
I am not sure whether there are some other interesting obervations for find these max values.
Any ideas are warmly welcome!
My question is what if for the general case when X={x1,x2,...,xn} and there are m subsets of X, denoted as A1,A2,...Am, is there some more efficient techniques to find such max values Max{Ai} (i=1,...,m) ?
Your help will be highly appreciated!
There is no method asymptotically better than brute force, assuming a typical representation of the given sets. Simply scanning through the sets to find the largest member of each requires linear time and linear time is optimal since every member of the set must be read in order to determine the maximum value.
Now if the input representation is not simply a listing of the elements in each set, than other bounds and algorithms may apply. For example, if we know the input sets are sorted and the length of the set is given as part of the input, we can obviously find the maximum elements in time linear only on the number of subsets but not on their length.
If your sets are implemented in a hash (or, more generally, if you can otherwise check for the presence of a value in the set in O(1) time) you can improve on a brute-force approach.
Instead of iterating through the elements of the subset and maintaining the maximum, iterate over the elements of the parent set in descending order, checking for the presence of those elements in the subset. The first found element is necessarily the subset's maximum. Technically, this still takes O(n) time (n = subset carnality) in the general case, but will generally carry a great performance benefit in practice. (If you have any data regarding the number and size of the subsets, and they favor this approach, you can improve on O(n) in the average case.)
This approach requires sorting of the parent set's elements (n log n), however, so it may only be worthwhile if the number of subsets is much greater than the carnality of the parent set.