Algorithm to find the set when the set of sums of subsets of power set is given - algorithm

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.

Related

When these two elements will be equal/unequal : nearest element to average vs median?

I understand that median element is calculated based on index/total number of elements in an Array while Average is based on total value of the array.
I am trying to solve the question :
Given an integer K and a matrix of N rows and M columns, the task is to find the minimum number of operations required to make all the elements of the matrix equal. In a single operation, K can be added to or subtracted from any element of the matrix.
Which element we should take here (to which all the elements will be made equal in minimum steps) and why?
I am more interested in understanding the why part.
Average value provides minimal sum of absolute distances from it to all elements, so it is suitable here.

Maximize sum of integer entries under a weight constraint

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

What is the following known as?

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.

Efficient algorithm for finding k largest elements with range of values

Assume there is a list of elements each with a range, such that the value of the element would lie in the range. The ranges between elements may overlap. The exact value is unknown, but it can be calculated. What would be an optimal algorithm to select the elements with highest k values, such that the number of exact computations is minimum?
I have a very naive and straight-forward algorithm, but definitely this is not optimal.
Sort the ranges according to maximum range values.
Compute first k values.
Remove the elements for which the maximum range value is less than the value of the k^{th} highest value till now.
From the remaining elements, calculate the value of the element with the maximum range value and update highest k list. If there are no remaining elements, then stop.
Go to 3
This can be improved without leaving the realm of naivité:
It is assured, that an element A, where the range-max is lower than the range-min of an element B also has a lower real value. So you drop all elements, that have a range-max lower than the 5th highest range-min. This leaves you with a much smaller list: If your original list is long (i.e.: Disk-based) you most likely can reduce it to a mem-based version. In addition to that, the selection run will most likely leave you with this sub-list already sorted.
If still necessary, sort the smaller list
(*) Now cycle similar to your original algorithm:
remove the highest-max element from the list and calculate the real value for it, ordering it into a sorted working list
move all values, that have range-max below this value from the current list to a secondary list, keeping the sortedness
This gives you an even shorter working list, that is assured to contain the highest values
if this has enough entries, chose the k highest and be done
if this not, make the secondary list your new primary list and goto (*)

Covering N sets of contiguous integers with minimum nos

We are Given N sets of contiguous integers. Each such set is defined by two numbers. Ex : 2,5 represents a set containing 2,3,4,5. We have to print minimum nos. of numbers to select in order to cover all N sets. A nos. is said to cover a set if it is contained in the set.
Ex: Given sets [2,5] , [3,4] , [10,100]. We can choose for example {3,10} so we cover up all 3 sets. Hence answer is 2.
I can't find a proper algorithm for N<=5000.
Here is an O(nlogn) approach to solve the problem:
Sort the sets by the last element (for example, your example will be sorted as [3,4], [2,5] , [10,100]).
Choose the end of the first interval
Remove all intersecting sets
If there is some uncovered set, return to 2.
Example (based on your example):
sort - your list of sets is sorted as l =[3,4], [2,5] , [10,100]
Choose 4
Remove the covered sets, you now have l=[10,100]
back to 2 - choose 100
Remove the last entry from the list l=[]
Stop clause is reached, you are done with two points: 4,100.
Correctness Proof (Guidelines) by #j_random_hacker:
Some element in that first (after sorting) range [i,j] must be
included in the answer, or that range would not be covered. Its
rightmost element j covers at least the same set of ranges as any
other element in [i,j]. Why? Suppose to the contrary that there was
some element k < j that covered a range that is not covered by j: then
that range must have an endpoint < j, which contradicts the fact that
[i,j] has the smallest endpoint (which we know because it's the first
in the sorted list)
Note the following is a greedy algorithm that doesn't work (see the comments). I am leaving it here, in case it helps someone else.
I would approach this using a recursive algorithm. First, note that if the sets are disjoint, then then you need "n" numbers. Second, the set of "covering" points can be the ends of the sets, so this is a reduced number of options.
You can iterate/recurse your way through this. The following is a high-level sketch of the algorithm:
One iteration step is:
Extract the endpoints from all the sets
Count the number of sets that each endpoint covers
Choose the endpoint with the maximum coverage
If the maximum coverage is 1, then choose an arbitrary point from each set.
Otherwise, choose the endpoint with the maximum coverage. If there are ties for the maximum, arbitrarily choose one. I don't believe it makes a difference when there are ties.
Remove all the sets covered by the endpoint, and add the endpoint to your "coverage points".
Repeat the process until either there are no sets left or the maximum coverage is 1.

Resources