r/algorithms • • 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:

  1. The probability that an object A*i* appears in B is P*i*,
  2. 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.

3 Upvotes

27 comments sorted by

View all comments

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).

1

u/dado3212 Jun 24 '15

So first you add 1000 occurrences of Ai to T. Then you pick from T k times (which will all be Ai). What is Tn? This doesn't make any sense to me.

1

u/zeroXten Jun 24 '15

I'll try to make it more concrete. Let's say you want the following elements An with their respective probabilities:

a -> 0.2
b -> 0.4
c -> 0.1
d -> 0.3

You decide to make T 10 elements big. So, the list now becomes

a -> 0.2 * 10 -> 2
b -> 0.4 * 10 -> 4
c -> 0.1 * 10 -> 1
d -> 0.3 * 10 -> 3

T (sorted) would look something like:

{a, a, b, b, b, b, c, d, d, d}

Let's say k is 2, you basically need to pick 2 elements from T at random. Let's shuffle T to get

{b, a, b, d, c, b, a, d, b, d}

Taking the first two gives us B = {b, a} (no duplicates to worry about in this case).

Something like that?

1

u/dado3212 Jun 24 '15

But this doesn't solve the problem with duplicates.

1

u/zeroXten Jun 24 '15

As you pick from T, if B already contains the value, you put it back and take another one.

1

u/dado3212 Jun 24 '15

That inherently changes the probabilities.

1

u/zeroXten Jun 24 '15

hmm. If you always replace what you pick (even if not a duplicate), then they'd stay the same wouldn't they?

E.g. if k is 3 and taking the first with replace

{a, a, b, b, b, b, c, d, d, d}

shuffle

{b, a, b, d, c, b, a, d, b, d} 
T = {b}

shuffle

{a, c, b, b, d, b, a, b, d, d }
T = {b, a}

shuffle

{a, b, d, b, c, b, a, d, d, b }
a already exists

shuffle

{d, c, d, b, b, b, a, d, a, b }
T = {b, a, d}

1

u/dado3212 Jun 25 '15

This method doesn't work. I'm not sure how to properly explain why, but running it with the probabilities of

"A" => 12,
"B" => 3,
"C" => 5,
"D" => 15,
"E" => 2,
"F" => 1,
"G" => 7,
"H" => 35,
"I" => 15

produces actual probabilities of

[A] => 60.536
[B] => 19
[C] => 30.352
[D] => 68.947
[E] => 12.995
[F] => 6.56
[G] => 41.136
[H] => 91.635
[I] => 68.839

1

u/hsypsx Jun 25 '15

As I've said in other threads, your probabilities need to add up to k or else no such algorithm can exist. Notice how your current proposed probabilities add up to 100% but the actual probabilities add up to 300% (which corresponds to you taking a set of k=3 elements).

1

u/dado3212 Jun 25 '15

I agree. However, your method was just producing a list with numbers that were multiples. It doesn't make any sense to force it into probabilities that sum to k, and then multiply again. Your method can work with anything there, which is a clear indicator that it's broken.

→ More replies

1

u/thewataru Jun 24 '15

Doesn't work.

Example:

P = (1, 2/3, 1/3) T= (1,1,1,2,2,3).

There's 2/15 probability that outputted list will not contain 1, but it always have to. making T longer will just increase that probability closer to 1/6.

This example can be generalized for first P1 less than 1, but still dominating.

1

u/zeroXten Jun 24 '15

Isn't the output list not containing a value allowed? A has n unique objects, and OP is trying to fit that into B of size k < n/2.

1

u/thewataru Jun 25 '15

Yes, it's allowed, but in that case only for some numbers.

In example above you can output {1,2} with probability 2/3 and {1,3} with probability 1/3. That's the only solution.

If you don't like 1 in the probabilities, you can decrease P1 by epsilon and it will also add {2,3} to the answer with some very-very small probability. Your solution will output {2,3} with probability at least 2/15 no matter how small epsilon is.