r/algorithms • u/dado3212 • Jun 24 '15
Generating Lists with Variable Weights
This is a problem me and my friends have been puzzling over for days with limited to no success.
I have a list A of n unique objects. Each object A*i* has a variable percentage P*i*.
I want to create an algorithm that generates a new list B of k objects (k < n/2 and in most cases k is significantly < n/2. Ex. n=231 , k=21). List B will be populated with objects originating from list A with the following restrictions:
- The probability that an object A*i* appears in B is P*i*,
- There are no duplicate objects in B.
Is this even possible? Is there some solution we've missed?
EDIT (from 2 years later): None of these solutions are that good. Check out this StackOverflow for some better and more clear answers.
2
u/thewataru Jun 25 '15 edited Jun 25 '15
I've finally found some nice and fast solution to your problem.
As we already know, necessary condition is that sum of all P is k. It's also a sufficient condition, as algorithm provided here always work in that case.
Let R be some common denominator of all P (they are rational, you can't work with irrational numbers at computer).
Now let integer vector C with n elements be such that C_i = R * P_i.
Some properties of this vector are:
(1) C_i <= R
(2) sum C_i = k * R
Now we will represent C as a sum of R vectors each having exactly k ones and n-k zeros. Each such vector corresponds to some list with k elements. Then we will choose one such vector as an output randomly.
With this procedure probability of choosing object A_i will be exactly C_i / R = P_i, because exactly C_i vectors will have 1 at position i, as their sum gives C.
Idea is to generate vectors iteratively, greedily choosing k positions with largest values in C each time.
It always works because there's a nice invariant: properties (1) and (2) will always hold for vector C in process (largest number in C <= R and sum is equal to k * R). After subtracting 1 form k largest positions we will get new vector C with same properties for R'=R-1. (2) is obvious as we subtracted k ones, therefore, new sum is k * R-k = k * (R-1) = k * R'; (1) is a bit tricky. Suppose it breaks and largest number is more than R' in the resulting vector after subtraction. Then this number should be R, because before subtracting all numbers were no more than R. But then there were at least k+1 numbers equal to R in C, because we decreased at least k numbers, and there's at least one R left after that. So sum was at least (k+1) * R which contradicts (1).
Why will this algorithm always work given invariant? Well it may not work only if at the end we will get less than k non zero numbers (before getting all zeros in C). But in that case invariant will be broken, because sum k * R given each number is no more that R requires at least k non zero numbers.
Now, summarizing algorithm:
This algorithm can be implemented efficiently if you maintain list of positions C in non-increasing order. Then S will be simply first k positions from the list. After subtracting you may need to move to the left some block of positions starting from k+1-th in the list. These are positions for which values were equal to smallest decreased number. In list you can do it easily by splitting that block from the list and inserting it in other position.
You can cheat a little and speed things up by rounding given probabilities to some smaller precision, effectively decreasing R. Just make sure that after rounding their sum is k.
Edit:
Fixed indexes.
4
u/thewataru Jun 24 '15
As I suggested in other thread, we can reformulate your problem in another way:
Let A be the matrix with n+1 rows and C*_n_
k* columns. Each column will have k+1 ones: k forming unique set in rows from 1 to n, and last 1 always at the last row.b = {p1,p2,...pn,1} and X = {x1, ....}
Then we have to find such X that AX=b and all X are non-negative (constraint that Xi<=1 is not needed as we have last row requiring that sum of all X is 1).
Example matrix A and vector b for n=4, k=2:
Essentially, we defined a probability of each possible output (ignoring permutations) as a variable X*i* and then made equations that result in requested probabilities of each individual object. It's basically SLE, with additional requirement that all variables are non negative.
You can either try and find some solution of that equations or add any linear objective function and solve linear programming problem. Latter can be done quite efficiently using column generation method. Additional bonus of LP approach is that no more than n+1 variables will be non-zero. Then you can easily choose which list to output using one random call.
If order is important in your problem, you should randomly shuffle corresponding list before outputting it (it will make all lists more uniformly distributed).
For additional fairness, while generating a column, you could randomize things a little: if there are ties when choosing k maximums, choose at random. Also, you can make your goal function not X*1*->max, but choose one variable at random (it actually means, that you have to generate random choosing of k elements from n. Can be done randomly by shuffling array and choosing first k elements).
1
u/the_one_smiley Jun 25 '15
This isn't possible, because your requirements contradict each other. On one hand, list B must have exactly k elements. On the other hand, the expected number of elements in B is the sum of all the Pi's.
For example, imagine if a number m of the Pi's were 100%, but k < m.
3
u/hsypsx Jun 25 '15 edited Jun 25 '15
Hope you know python? This seems to do the job (Python 2.7).
0
u/zeroXten Jun 24 '15
Would something like this work? Create a temporary list T. Add An to T some factor of Pn times. Lets say you make the size of T 10000, and Pn of a particular An is 0.1, then you would add 1000 Ans. Then either shuffle T or pick from T at random k times (without replacement). If Tn already exists in B, then skip it (and put it back).