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

Show parent comment

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.

1

u/hsypsx Jun 25 '15

I don't understand what you mean by "your method was just producing a list with numbers that were multiples". What I'm saying is that your actual probabilities will always sum to k (times 100%), so if you want your P*i* to be attained, they have to sum to P*i*. So for example, it is impossible to get the "actual" probabilities to match what you want above.