I have the following problem.
I've got a set of items {a1, a2, a3, ... aN}
. Each one of those items can contain another set of items {b1, b2, b3, ... bN}
. So the end result looks something like this:
a1
b4
b22
b40
b11
b9
a2
b30
b23
b9
b4
b11
a3
b22
b4
b60
b9
As a result of the execution of the algorithm I would like to get the groups of b-type objects that fall under following rules:
- If more than one b-type object under an a-type object only exists under that a-type object, they should be grouped.
- If more than one b-type object is used in more then one a-type object they also should be grouped.
Example:
b4, b9
b30, b23
b40, b60, b11 and b22 shouldn't be grouped because there are no pairs for them.
I would be writing the implementation of this algorithm in C#, so it would be nice to avoid data structures that don't exist in it, such as binary trees, linked lists, etc. But this is not a requirement; all of these could be implemented too.
Clarification: Sets can contain as many objects as needed, but not more than 1 of each. The rules are that all unique objects of b-type within the same a-type should be grouped (more than 1), and if more than 1 b-type object fall within more than 1 a-type object, they should be grouped. The groups should be as large as possible.
Real life example: Web-pages are a-type and the CSS files used on those pages are b-type. In order to speed up the loading of the pages, you want to have as few requests as possible going to the server, so you combine CSS files, but you don't want to combine files that are used by themselves only on a few pages, since they will be cached and you don't have to re-download them again.