2
$\begingroup$

I am trying to design an algorithm that will generate the full list of ways to minimally satisfy multiple requirements, given information on how the individual requirements are fulfilled.

For example, let's say you can either have (1) or not have (0) four different foods: Apple, Banana, Carrot, Donut.

Requirement 1 says "You must have an Apple and a Banana." (1,1,0,0) satisfies this. So do (1,1,1,0), (1,1,0,1), and (1,1,1,1), but these are not minimal solutions, since you could take away the Carrot and/or Donut and still satisfy it.

Requirement 2 says "You must have three foods." (1,1,1,0), (1,1,0,1), (1,0,1,1), and (0,1,1,1) are all minimal solutions for this. (1,1,1,1) is not minimal.

I already have ways of generating the minimal satisfying solutions above. They aren't optimal but the search space for each individual requirement is small enough that it doesn't matter. Here's where it gets sticky: generating a list of ways to minimally satisfy both at once.

The brute-force approach to minimally satisfying both at once is then easy, as long as no requirement turns from satisfied to unsatisfied by adding a food (i.e. there's no requirement that says "You must not have a Donut"). Simply (first) look at all combinations of successful arrays, one from each requirement, and take the maximum of each element. For example, combine (1,1,0,0) from Requirement 1 with (1,0,1,1) from Requirement 2 to get (1,1,1,1) to satisfy both. Then (second) prune out non-minimal solutions by comparing each array that satisfies all requirements against each other to get the minimal solutions (1,1,0,1) and (1,1,1,0).

But this is very slow! The runtime grows very quickly with the number of requirements, or successful arrays per requirement. And it generates a lot of redundant output before pruning (for example, (1,1,0,0) plus either (1,0,1,1) or (0,1,1,1) both make (1,1,1,1)).

I'm at a bit of a loss as to how to attack this differently so as to avoid searching every combination, and would appreciate any help. I only have minimal theoretical-CS training and feel like there's probably some terminology or analogous known algorithm I'm not aware of.

$\endgroup$
  • $\begingroup$ Let $\rm x \in \{0,1\}^4$. From requirement 1, we have $x_1 = x_2 = 1$. From requirement 2, we have $\sum_{i=1}^4 x_i \geq 3$. Hence, there are only three solutions, namely, $(1,1,1,0)$, $(1,1,0,1)$ and $(1,1,1,1)$. If we want to minimize the number of food types, then minimize $\sum_{i=1}^4 x_i$. If we have $n$-tuples and other requirements, then we would like to enumerate all vertices of the hypercube $\{0,1\}^n$ inside a polytope. With an objective function to be minimized, e.g., $\sum_{i=1}^n x_i$, then we can obtain a single $n$-tuple. $\endgroup$ – Rodrigo de Azevedo Apr 13 '17 at 21:52
  • $\begingroup$ The heap sort could be a solution. $\endgroup$ – LetTheWritersWrite Apr 13 '17 at 21:52
0
$\begingroup$

It depends what you mean by "minimal". You might mean either of the following two:

  • you want to minimize the number of foods selected (variables set to true).

  • you want a set of foods that's a valid solution, but where any subset of that set is no longer a valid solution.

I'll discuss solution to both variants of the problem.

Minimizing the size of the set

If you want to minimize the number of foods selected (the number of variables set to true), the problem is NP-hard, so there is not likely to be any algorithm that is both efficient and always provides the optimum answer for all possible instances.

However, don't give up. There are reasonable approaches. Your problem can be formulated as an instance of integer linear programming (ILP), and then you can use an off-the-shelf ILP solver to solve it. ILP solvers have gotten pretty good so they might be adequate.

You have $n$ zero-or-one integer variables $x_1,\dots,x_n$, corresponding to the $n$ foods. Each requirement then translates to one (or more) linear inequalities. For instance, the requirement that you must have food #1 and food #2 corresponds to $x_1=x_2=1$. The requirement that you must have at least three foods corresponds to $x_1+x_2+x_3+x_4 \ge 3$. Some kinds of requirements take a bit more work to formulate as linear inequalities; see Express boolean logic operations in zero-one integer linear programming (ILP) for some tools you can use. Then, you want to minimize the objective function $x_1+x_2+\dots + x_n$ subject to those constraints. An ILP solver will do that for you.

Alternatively, you could use an off-the-shelf pseudo-boolean satisfiability solver.

The ILP solver will generate one minimal solution. To generate all of them, there are various ways to do that. For instance, if you know there's a solution with 3 variables set to true and no solution with 2 variables set to true, then you can add the requirement $x_1+x_2+\dots + x_n=3$ and enumerate all solutions to the corresponding integer linear program.

Minimal sets of foods

Alternatively, suppose you want to enumerate all ways to choose a set of foods that is minimal (no proper subset is a valid solution). This requires different methods.

One way is to find a valid solution, then start removing foods until you can no longer remove any more. That will be one minimal solution. To enumerate minimal valid solutions, you can now look for a new solution that is not a superset of any prior minimal solution. This could be very slow. One way to find a valid solution is by using an integer linear program (ILP), as above; you can formulate the constraint that the solution must not be a superset of any prior minimal solution as well.

An alternative approach might be to view this as the problem of converting a Boolean formula to DNF form. I think that each DNF clause corresponds to one minimal subset of foods (of variables that can be set to true). There are various methods for conversion to DNF form; e.g., Quine-McCluskey, the Espresso logic minimizer. I haven't thought about this carefully and I don't know for sure whether this will find all minimal solutions. It appears that in your specific situation, you have a monotone Boolean formula; I don't know whether there are better algorithms for converting monotone Boolean formulas to DNF form.

$\endgroup$
  • $\begingroup$ I did have the latter meaning for "minimal" but this really gives me something to go on. It's also good to know that this is NP-hard. There's not going to be a super quick algorithm! $\endgroup$ – NickCHK Apr 14 '17 at 0:24
  • $\begingroup$ @NickCHK, OK, I edited my answer to discuss both variants. $\endgroup$ – D.W. Apr 14 '17 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.