Reducing Subset Sum Problem to positive numbers only - subset-sum

I want to know is there is a way to reduce a Subset Sum problem with a set 'A' with negative and positive integers to the same problem but only with positive numbers.

You cant technically have the same problem with positive integers because any subset of positive integers (except empty subset) will have sum greater than 0.
You can have slightly different problem with positive integers (and positive subset sum). If you add one positive number X to each element in A thus forming A+ so that there are only positive elements in A+ then you'll search for a subset B of A+ for which the sum of its elements equals X * number of its(B's) elements. However this differs from the original Subset Sum problem by having a dynamic (dependant on the amount of elements in subset) sum to look for.
You might want to look here : http://www.or.deis.unibo.it/alberto/mssp_g_f.ps which is basically free version of this: http://www.sciencedirect.com/science/article/pii/S0020019000000107 as stated here: https://mathoverflow.net/questions/92504/multiple-disjoint-subset-sum-problem

Related

Matlab lsqlin constrain N number of elements in solution

I have a standard
x = lsqlin(C,d,A,b,Aeq,beq,lb,ub) problem. However, given I have a universe of approx. 700 rows in C, I find that the solution in x contains hundreds of very small values and a large N (number of elements).
If my solution should only possibly contain N*non-zero elements and with the vector x remaining the same length, is there a way to prevent small values? By specifying either a maximum number of non-zero N, or a minimum value if an element is chosen?
Vectors lb=0 and ub=1 apply.

Which optimization type is this case?

The picture shows minimised case of my assignment, I try to minimize sum of all "y" values with using Matlab. These y values are changing with X matrix. So, y values are the functions of X matrix.
X variables are binary numbers and the sum of consecutive two numbers in the second row must be greater than or equal to 1. In addition, sum of consecutive three numbers in the third row must be greater than or equal to 1.
How can I solve this problem? Thanks for your help.
This appears to be a mixed integer linear programming problem. (All linear constraints, and a binary one)
These can be solved with intlinprog.
[x,fval] = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub)

How to calculate hash of a set (unordered list) of values?

I want to calculate sha1 hash of a set (unordered list) of elements. I have already calculated sha1 hash of each element. I'm considering two solutions:
Sort elements by their hashes and calculate top hash of such list.
Treat element hashes as 160 bits integer values and XOR (bitwise operation) them together into one 160 bits hash.
Does second solution is weaker in terms of secure hash function properties? (pre-image resistance, second pre-image resistance, collision resistance).
Option 1 is what is done in ERS: that standard uses hash trees, where each node contains a hash value computed over the set of hash values from the child nodes; since order is not significant in the tree, the values are sorted lexicographically before hashing. This is good, and, as far as we know, safe.
Option 2 is very unsafe: if the hash function has 160-bit output, then I can easily generate 160 random inputs such that the corresponding hash values constitute a basis of the vector space GF(2)160, at which point I can produce a matching set for any aggregate hash value. Attack cost is negligible.
Option 3 suggested by #paj28 (sorting the values to hash, then hash them) is fine, too, as long as you "concatenate" the sorted values with an unambiguous separator. For instance, if you hash the set of strings containing "bar" and "foo", you don't want to obtain the same hash value as with the set of strings containing "ba" and "rfoo". It is easier to get something safe when all values to hash have the same length.
Therefore, use option 1: hash each value in the set, then sort the hash values in lexicographic order, and hash the sorted list of values again.
On the attack with option 2: this is linear algebra. Suppose that you have k vectors of n bits, such that none of them is equal to the XOR of some of the k-1 other vectors (they are said to be linearly independent). Then consider a new random vector v; the probability that this vector is equal to the XOR of some of the k vectors is equal to 2k-n, i.e. it is small as long as k < n. If the new vector v indeed linearly independent with the k vectors you already have (thus with probability 1-2k-n), then add it to the set: you now have k+1 linearly independent vectors.
Recurse: you will soon obtain n vectors of n bits which are linearly independent to each other. But you cannot go further, because probability of any new vector to be linearly independent from the n previous has dropped to 0. The n vectors are said to be a basis for the vector space.
In this case, the vectors are obtained by simply hashing values (random values, or values with structure, it does not matter much, because the hash function acts as a randomizer).
For a given set of k vectors, determining whether a new vector v is linearly independent with the k vectors is easy with Gaussian elimination. The same algorithm lets you know, once you have a basis, which of your n basis vectors shall be XORed together to yield any vector v'. In the setup of this question, this means that once I have produced n values mi such that the h(mi) constitute a basis, then for any target n-bit output t, I can use Gauss elimination to work out which of my h(mi) may be XORed together to yield exactly the value t. The corresponding mi values are then a preimage set for t.
The other option (3) is to sort the elements first, then combine them into a single string using a separator that cannot appear as part of an element.
Of these possibilities, 2 would concern me the most. I can't think now how you could attack it in a practical way, but it seems the riskiest.
So 1 and 3 are basically fine. But I would recommend 3 because you are using the hash in the way it is intended.

How does matlab compare two complex numbers?

I saw a file in matlab with used max() on a matrix whose entries are complex numbers. I can't understand how does matlab compare two complex numbers?
ls1=max(tfsp');
Here , tfsp contains complex numbers.
The complex numbers are compared first by magnitude, then by phase angle (if there is a tie for the maximum magnitude.)
From help max:
When X is complex, the maximum is computed using the magnitude
MAX(ABS(X)). In the case of equal magnitude elements, then the phase
angle MAX(ANGLE(X)) is used.
NaN's are ignored when computing the maximum. When all elements in X
are NaN's, then the first one is returned as the maximum.

Matlab function for creating random number satisfying constraints

As an input I have two number x and y. x>y.
I want to create exactly y non-zero random number which their sum will be equal to x. I know randi([min max]) function . Can you help me?
If I got it right, you want something like this:
data = rand(1,y);
data = data * x / sum(data);
data will contain exactly y positive uniformly distributed numbers which sum equals to x.
Check out the file random vectors generator with fixed sum in Matlab FEX. I believe this will answer your question.
Leonid's approach will certainly generate a set of random numbers that have the correct sum, but it won't select uniformly over the allowed space. If this is important, an approach that will work is the following:
(with x = 1):
Generate Y-1 random numbers uniformly over [0,1].
Sort the Y-1 numbers from smallest to largest. Call these {y1,...,y_{N-1}}
Take as the Y random numbers the set {y_1-0 ,y_2-y1,...,1-y_{N-1}} == {n_1,... n_Y}.
These n_i clearly sum to one. It is easy to prove uniformity by considering the probability for a given realization of the n_i.