Distribution of items from "x" sets of plants in "y" baskets equally
I need ideas about algorithm for distribution of plants in groups. I have, say 5, sets of plants and each set depict开发者_StackOverflow中文版s a certain property e.g.,
setA= {set of all plants that are green in color}
setB = {set of all plants red in color}
setC={ all the plants that are roots also}
setD= {fruits}
setE= [flowers}
A plant can be a member of more than one set. The total no of plants is "n" and I've got "m" baskets. What is needed is to distribute all these "n" plants in "m" baskets so that for each set, all baskets have equal (or almost equal) numbers of plants from that set. And here comes the problem. If I start distribution in "m" baskets one by one for each sets, then in most of the scenarios, the last distribution will be the only valid one, i.e., the baskets will have even distribution of flowers in them but not necessarily of fruits, roots or colors etc.
How to distribute ALL of these equally? Any ideas?
This is a variation of maximum match problem in a bipartite graph.
E.g., you have 20 plants and 5 groups. Then, first set of nodes will consist of 20 plants and another - of 5 baskets, but each basket will be presented 4 times (also 20 nodes total).
Now, you just have to match 20 plants to 20 basket-nodes, which is precisely the problem above.
If n
isn't divisible by m
, you can add some spare busket-nodes. E.g., if n = 23
and m = 5
, you can repeat each basket 5 times. When perfect match is found, busket-set will still have two (5*5 - 23
) free nodes.
To make 'almost equal' requirement work better (when there's no perfect match), you can make graph weighted instead. I.e., greenPlantsBasket#1 costs 1, greenPlantsBasket#2 costs 2, greenPlantsBasket#3 costs 4, etc.
Wikipedia provides algorithms for all flavours of max matching problem and there're implementation libraries too.
精彩评论