I have n
sets that each contain some elements, for example:
{2, 3}, {6, 3}, {2, 7, 3}, {7}
Now the goal is to find the minimum number of elements that occur in all sets. For this example it would be 2 ({3, 7}
). Some more examples:
{2}, {4}
- minimum is 2 ({2, 4}
)
{4, 15, 3}, {3, 2}, {24, 4, 2}, {24}, {15, 6}
- minimum is 3 ({15, 2, 24}
or {15, 3, 24}
or {3, 24, 6}
)
I don't need to know the set of those minimum number of elements necessarily, just the size of that set is enough.
I've written a recursive algorithm myself already that goes over all possibilities, but the execution time really gets out of hand as the number of different elements rises. Is there an algorithm available (maybe already written but I couldn't find it) for this specific problem? I've looked at the set cover problem, it looks a bit like this one but it's not exactly the same.
resultSet
starts with all elements that are members of singleton sets. In your original example,7
. Then, I would remove7
from all sets (removing{7}
entirely rather than leaving it empty), and calculate the intersection of all remaining sets. If the intersection is empty, you find the smallest set, and add all of its elements to the result, and repeat. If it's not empty (in this case, it will be{3}
), remove the intersection elements from all sets. Repeat until all original sets are "checked off".