Algorithm to optimize grouping

I would like to know if there's a known algorithm or best practice way to do the following:

I have a collection with a subcollection, for example:

``````R1 R2 R3
-- -- --
M  M  M
N  N
L  L
A
``````

What i need is an algorithm to get the following result:

``````R1, R2: M N L
R2: A
R3: M
``````

This is -not- what i want, it has more repeating values for R than the above:

``````R1, R2, R3: M
R1, R2: N L
R2: A
``````

I need to group in way that i get the most optimized groups of R. The least amount of groups of R the better so i get the largest sub collections.

Another example (with the most obvious result):

``````R1 R2 R3
-- -- --
M  M  A
V  V  B
L  L  C
``````

Should result in:

``````R1, R2: M V L
R3: A B C
``````

I need to do this in LINQ/C#.

• It's not clear what you are trying to minimize or maximize. Do you always want to list the largest set common to more than one R group? Suppose you have R1: ABCD; R2: ABCD; R3: ABC ? Then would you like to get R1,R2: ABCD; R3: ABC ? Nov 28, 2012 at 17:56
• I want the least combinations for R. The more "R"s in a group, the better. In your example: Yes, you're right. Nov 28, 2012 at 18:05
• You're "not what I want" has the most R's in a group. Do you mean you want the least number of total R's in all groups ? I think that's what you're trying to say.. Nov 28, 2012 at 18:11
• Did anyone else initially misread the title as "algorithm to optimize groping"? Nov 28, 2012 at 18:17
• You're right. I guess my explanation isn't the best. By the more "R"s in a group i meant that ideally i have them all in 1 group. The less they repeat the better. Nov 28, 2012 at 18:19