I have a set X including n elements. I want to find out m subsets of X with the same size. Given a constant k, every element of X occurs exactly in the k subsets. And the size of intersection between any two (three, four,...,k) subsets is constant such as |S1∩S2|=|S1∩S3|=c2,|S1∩S2∩S5|=|S1∩S3∩S4|=c3..., but c2 doesn't need to equal c3. How to solve it?
Related
Given a non-negative integer n and a positive real weight vector w with dimension m, partition n into a length-m non-negative integer vector that sums to n (call it v) such that max w_iv_i is the smallest, that is, we want to find the vector v such that the maximum of element-wise product between w and v is the smallest. There maybe several partitions, and we only want the smallest value of max w_iv_i among all possible v.
Seems like this problem can use a greedy algorithm to solve. From a target vector v for n-1, we add 1 to each entry, and find the minimum among those m vectors. but I don't think it's correct. The intuition is that it might add "over" the minimum. That is, there exists another partition not yielded by the add 1 procedure that falls in between the "minimum" of n-1 produced by this greedy algorithm and that of n produced by this greedy algorithm. Can anyone prove if this is correct or incorrect?
If you already know the maximum element-wise product P, then you can just set vi = floor(P/wi) until you run out of n.
Use binary search to find the smallest possible value of P.
The largest guess you need to try is n * min(w), so that means testing log(n) + log(min(w)) candidates, spending O(m) time for each test, or O(m*(log n + log(min(w))) all together.
I have an array list of distinct positive integers representing a set L, and an integer S. What's the fastest way to count all subsets of L which have the sum of their elements equal to S, instead of iterating over all subsets and just checking if each subset's sum equal is equal to S?
This can be solved in O(NS) using a simple dynamic programming approach, similar to knapsack problem. Let's for each Q and for each i solve the following problem: how many subsets of first i elements of L exist so that their sum is equal to Q. Let us denote the number of such subsets C[i,Q].
Obviously, C[0,0]=1 and C[0,Q]=0 for Q!=0. (Note that i=0 denotes first 0 elements, that is no elements.)
For a bigger i we have two possibilities: either the last available element (L[i-1]) is taken to our set, then we have C[i-1, Q-L[i-1]] such sets. Either it is not taken, then we have C[i-1, Q] such sets. Therefore, C[i,Q]=C[i-1, Q-L[i-1]]+C[i-1, Q]. Iterating over i and Q, we calculate all Cs.
Note that if all elements in L are non-negative, then you can solve the problem only for non-negative Qs, and the first term disappears if Q<L[i-1]. If negative elements are allowed, then you need to consider negative Qs too.
I have a set of integers and I want to find the largest subset in which the elements does not correlate with each other in a specific way. For example a subset in which if any of the elements is multiplied by 13 the result is not in the subset.
My first thought is to iterate through all the possible subsets, filter out these that don't meet the condition and then find the largest one, but this is too slow and I don't know how to generate all possible subsets.
I'll be answering this one (from comments). In general there's no good solution for any "correlation"
Relationship is the following : if you multiple any of the elements in the subset by some number the resulting number does not have to be in the subset.
If your number is m
You can generate all chains x, x*m, x*m*m, ...., such that all number in chain are in the set, x/m is not
Remove every second element, i.e x*m^2, x*m^4 from original set. Elements left are your target set.
A better way is to build a graph and find the vertexes with most edges and remove them until you get rid of all edges. Complexity is about O(N^2).
Here is a detailed algorithm:
for each possible pair (x, y) from the source set
begin
if x = y * 13 or y = x * 13 then make edge between x and y
end
while graph has edges
begin
let V = find: a vertex with maximum count of edges (it can be 1 or 2)
remove V from the graph
end
result: the remaining vertexes in the graph
let there be a set S with n distinct elements namely a1, a2, a3, a4 and so on.
let the be subsets of S which have exactly k elements.
number of such subsets= (n choose k ) or nCk
out of all such subsets with exactly k number of elements, how many of them will contain a1?
I know you asked this a long time ago, so I'm sorry if this is completely irrelevant now, but maybe it can help someone else.
In theory, consider that if we let a set P be S, but without the element a1, then P has n-1 elements. In this new set P, if we choose subsets with k-1 elements, then there will be a corresponding subset of S containing k elements, one of which is a1. So we're just using a set with one less element (n-1 elements) and choose k-1 elements for the subsets. Our formula is then: (n-1)C(k-1).
I have an array A of positive integers [a0, a1, a2, ..., an] and a positive number K. I need to find all (or almost all) pairs of subsets U and V of array A such as:
sum of all elements in U are less or equal to K
sum of all elements in V are less or equal to K
U + V may contain not all elements of original array A
all elements from U should come before all elements in V in initial array A. For example, let's imagine that we choose U = [a1, a3, a5] then we can start building array V only from a6. It is not allowed to use element a0, a2 or a4 in this case.
I was able to find DP solution, which is O(N^2 * K^2) (where N is total number of elements in A). Although N and K are small (< 100) it is still too slow.
I'm looking for some approximation algorithm or pseudo-polynomial dynamic programming algorithm. Bin packing problem looks similar to mine, but I'm not sure how I can apply it to my constraints...
Please advise.
EDIT: each number has upper bound equal to 50