I have a combinatorial problem that can be solved inefficiently using the cartesian product of multiple sets. Concretely, I have multiple items and multiple elements that satisfy each item. The problem consists of finding all possible combinations of elements that satisfy all items. For example:
items -> elements
------------------------
1 -> {a,b} // a and b cover the item 1
2 -> {a,b} // a and b cover the item 2
3 -> {a,b,c} // a, b and c cover the item 3
4 -> {a,b,e,f} // a, b, e, f cover the item 4
Alternative representation:
element -> items covered
------------------------
a -> {1,2,3,4}
b -> {1,2,3,4}
c -> {3}
e -> {4}
f -> {4}
The goal is to find all combinations that cover items 1,2,3,4. Valid solutions are:
{a},{a,b},{a,c},{a,e},{a,f},{a,b,c},{a,b,e},{a,b,f},{a,b,c,e},{a,b,c,f}
{b},{b,c},{b,e},{b,f},{b,c,e},{b,c,f}
Note that the order is not important, so {a,b} = {b,a} ({a,b} x {c,d} = {c,d} x {a,b})
.
Also, note that {a,a,a,a}, {a,a,a,b}...
are redundant combinations.
As you can see, this problem is similar to the set cover problem
, where the universe
of elements for this example are the items U={1,2,3,4}
and the set of subsets from U is S={ab={1,2,3,4},c={3},ef{4}}
, where set {1,2,3,4}
is the set of items covered by the element a
and b
, {3}
is the set of elements covered by c
, and {4}
is the set of elements covered by elements e
and f
. However, the goal here is not finding the
minimal combination of sets from S
that covers all elements from U
, but finding all combinations of elements {a,b,c,e,f}
that cover all items {1,2,3,4}
.
A näive implementation could be done by performing a cartesian product between sets for 1,2,3 and 4, and then filtering the combinations that are redundant. However, this approach is very inefficient. Suppose I have this situation:
1 -> {a,b,c,d,e,f,g,h}
2 -> {a,b,c,d,e,f,g,h}
3 -> {a,b,c,d,e,f,g,h}
4 -> {a,b,c,d,e,f,g,h}
5 -> {a,b,c,d,e,f,g,h}
6 -> {a,b,c,d,e,f,g,h,i}
A cartesian product between the six sets will result in a 8^5*9=294912
combinations,
when there are actually many fewer combinations, which are: {a,b,c,d,e,f,g} U {a,b,c,d,e,f,g} x {i}
.
Another way to solve this problem is to enumerate all elements, skipping the combinations that are equivalent to other previously generated, and also skipping repeated elements. This is kinda easy to compute and can be implemented as an iterator that returns a combination at a time, but I don't know if there is a better way to solve this problem, or if this problem was studied before.
How would you solve this problem?
{a,b,e}
as a valid solution in your first example? Is it because you listed only some of the valid combinations? By the way, I added Haskell code to my answer, which provides an immediate result for the two examples given..how big would you expect the input data to be? – גלעד ברקן