Two recent questions by the same author 1 are generally solved by the same technique. This feels to me like it would be a studied and perhaps well solved problem.
I would like to know what it might be called and where to find more information about it, assuming it is a well-known problem
This is my own phrasing of the question:
If we have a Set, M
, of MultiSets2, M_1
- M_n
made up of elements of Set S
, and we have a MultiSet of components, C
, also drawn from S
, how do we find all subsets of M
such that the for each subset, the multiplicities of each element of S
in its union is no greater than the multiplicity of that element in C
?3
For example, paraphrasing my own answer to the first question:
I think I understand that what you want would be for something like this:
const catalog= ["ad", "aab", "ace", "cd", "ba", "bd", "adf", "def"] const components = ["a", "b", "a", "c", "d", "e"] simultaneousItems (catalog, components)
to yield
[ ["ad", "ace"], ["ad", "ba"], ["ad"], ["aab", "cd"], ["aab"], ["ace", "ba"], ["ace", "bd"], ["ace"],["cd", "ba"], ["cd"], ["ba"], ["bd"], [] ]
where every array in the result is subset of catalog items that can be made together out of the components supplied. So, for instance, none of the output arrays contains
"adf"
or"def"
, since we have no"f"
in the components, and none of them contain both"ad"
and"aab"
since we only have two"a"
s in the component list.
Note that I'm using "abc"
as a shorthand for the set containing the strings "a"
, "b"
, and "c"
.
The original problems seemed to be looking for maximal subsets, so there is one additional step, and that is perhaps an equally interesting problem, but I'm more curious about the version above.
Please don't look at my solution as any sort of algorithmic ideal. It was the first idea that came to mind.
Is this a well-known problem? There's some vague relationship to the Knapsack problem, and other packing problems, but I can't quite figure out what and where to search.
1 The first one is closer to what I think is the fundamental algorithmic question. I'm assuming the second one is closer to the real problem the OP is trying to solve. Without any feedback yet from the OP, I don't know for certain that my answers match the real need, but I find this an interesting question regardless.
2 Or Bag
or mset
. (definition)
3 More explicitly (and forgive me for being too far away from formal Math classes) if C = {S_1^m(z_1), S_2^m(z_2), ... S_j^m(z_j)} for some integer j, and S_1, S_2, ... S_j in S, and if N = {M_a_1, M_a_2, ... M_a_k} is one of the subset of M in the result, and ∪ {M_a_1, M_a_2, ... M_a_k} = {S_1^m(x_1), S_2^m(x_2), ... S_j^m(x_j)}, then for all i, x_i ≤ z_i.