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;
}
For the sake of this example, we'll limit to 3 categories, but there can be N.
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:
Let's call this list, Input (A)
After grouping these items into group we could end up with : (there could be other possibilities). The grouping criteria would be to have the less output groups as possible.
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).
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)?
So, walking through your example. First it would pair up the first and second groups. The first two category sets are the same, the third is different, so they can be merged. You now have a list that is:
Next we compare the (new) first and second groups. Both the first and third categories don't match, no merge. Next we compare the first and third, again, same two categories won't match. The first group won't match any others. So now we move onto the second group. We pair it up with the third. It can be merged, as the first two categories are different:
Now we start over, pairing the first and second groups. They match. The first category is different, the second the same, and the third the same. It is now:
We'd now compare the first to each of the other three, none will match. We then compare the second to the other two, none will match. Finally the third and fourth will match, as only the second category is different:
Finally we'll go through all of the combinations, none of the groups will match merge conditions, and we're done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With