mardi 6 septembre 2016

Return random item by its weight, advanced level

There are many typical questions like http://ift.tt/2bUmbVn

Imagine more advanced problem.

You have N sources of pair (item_id, weight) information. Let's call them Shards. Shards contain lists of pairs (item_id, weight).

And you have central node, let's call it Central.

The problem is: on Central choose random item from The Big List (the list virtually merged from all lists on all shards) according to their weight through all weights.

For example, we have two shards:

+-------+---------+--------+ | shard | item_id | weight | +-------+---------+--------+ | 1 | 1 | 7 | | 1 | 2 | 4 | | 1 | 3 | 2 | | 2 | 4 | 5 | | 2 | 5 | 1 | +-------+---------+--------+

(Let item_id will be unique through all shards.)

First problem:

How to choose item_id randomly but weighted through all shards? I.e. total_weight == 7+4+2+5+1 == 19, so 1 will be chosen with probability of 7/19, 2 - 4/19, 3 - 2/19 and so on.

Second problem:

How to range all items from all shards randomly, but weighted through all shards?

I.e. ideal ranging will be: 1, 4, 2, 3, 5 (according to their weights),

but there may be another ranging like 1, 2, 4, 3, 5, but slightly less frequently than previous,

...

and worst case 5, 3, 2, 4, 1 can also appear, but with very-very little probability.

Is there common problem in computer science for this?




Aucun commentaire:

Enregistrer un commentaire