Grouping algorithm for combinations
Let's say I have a list of items, each item is defined by a simple structure
struct simpleItem
{
String Category1;
String Category2;
...
String CategoryN;
}
Each item have a series of values belonging to some categories. The number
of categories N is known at the time of processing a list and each items
have the same amount of categories and only one value per category, there
are no duplicate items. However, each list can have a different category
set.
I'm looking for a way to group these items by categories in a way that if
those groups are deconstructed into single items by combining each
permutations of categories, I'll end up with the original combinaisons,
with no duplicates.
A group result will be:
struct grouped
{
String[] Category1;
String[] Category2;
...
String[] CategoryN;
}
Example
For the sake of this example, we'll limit to 3 categories, but there can
be N.
Categories
Animal, Eye Color, Fur
Choices for "Animal" category: Cat, Dog, Rat, Horse
Choices for "Eye Color" category: Blue, Yellow, Green, Red, Orange
Choices for "Fur" category: Long, Short, Curly
If the list had all the permutations for those 3 categories, the end
result would be
Group 1 :
Animal [Cat, Dog, Rat, Horse]
Eye Color [Blue, Yellow, Green, Red, Orange]
Fur [Long, Short, Curly]
If I have a sublist, for example:
Cat, Blue, Long
Cat, Blue, Short
Dog, Blue, Long
Dog, Blue, Short
Dog, Green Long
Rat, Red, Short
Rat, Blue, Short
Let's call this list, Input (A)
After grouping these items into group we could end up with : (there could
be other possibilities)
Group 1:
Animal [Cat, Dog]
Eye Color [Blue ]
Fur [Long, Short]
Group 2:
Animal [Dog]
Eye Color [Green ]
Fur [Long]
Group 3:
Animal [Rat ]
Eye Color [Red, Blue]
Fur [Short ]
Let's call these groups, Output (B)
As we can see, by combining each items of the resulting groups, we'll end
back to the original input list of 7 elements in (A).
Question
So, I'm trying to write an algorithm that generates these groups. I am
trying to do this with LINQ, but I am also open for other suggestions. Any
suggestion on how to get to (B) from (A)?
No comments:
Post a Comment