Question

Consider the following problem:

There are N coins numbered 1 to N.

You can't see them, but are given M facts about them of the form:

struct Fact
{
    set<int> positions
    int num_heads
}

positions identifies a subset of the coins, and num_heads is the number of coins in that subset that are heads.

Given these M facts you need to work out the maximum number of heads there could possibly be.

Is this problem NP-complete? If yes, what is the reduction? If no, what is a polynomial time solution?

For example:

N = 5
M = 3
fact1 = { {1, 2}, 1 } // Either coin 1 or coin 2 is a head
fact2 = { {4}, 0 } // Coin 4 is a tail
fact3 = { {2, 4, 5}, 2 } // Out of coins 2, 4 and 5, two are heads

A configuration with the most heads that matches the facts is:

T H H T H

So the answer is 3 heads.

Était-ce utile?

La solution

Let's say you have a 3-SAT problem. You can map every boolean variable v in that problem to two coins. Call them 'true(v)' and 'false(v)'. The idea is that if v in a solution to the 3-SAT problem is true, then 'true(v)' is heads; otherwise 'false(v)' is heads. For every v you add the coin constraint

{true(v), false(v)} has 1 heads, and has 1 tails

After this, you can translate a 3-SAT clause with literals l1, l2, l3

l1 or l2 or l3

to the coin constraint

{t/f(l1), t/f(l2), t/f(l3)} has at least 1 heads

where t/f(l1) is either 'true(l1)' or 'false(l1)' depending on if l1 is positive (not negated) or negative (negated) in the clause. We just need to show that 'at least 1 heads' can be implemented in the coin problem as 'at least 1 heads' is not expressible directly. This can be done with the following device. Let C1, C2, C3 be three coins for which we want to state the constraint 'at least one of them is heads'. Create three other coins X1, X2, X3 and put in constraint

{X1, X2, X3, C1, C2, C3} has 4 heads

but no other constraints for X1, X2, X3. This constraint is satisfied only if at least one of C1, C2, C3 is heads; the coins X1..3 can be used to provide the remaining needed heads.

Note that this reduction does not use the "maximum number of heads" aspect of the problem at all; it is plainly impossible to choose heads/tails status for the coins that represent boolean variables at all if the 3-SAT formula is unsatisfiable.

This is a polynomial reduction FROM 3-SAT TO your coin problem, showing it is NP-hard. To show it is NP-complete, just observe that a solution to your coin problem can be checked in polynomial time, QED.

Autres conseils

One-in-three 3SAT reduces trivially to the decision version of your problem (does there exist a configuration at all?), which is trivially in NP. The maximization version is NP-hard (but not complete because it's not a decision problem), even the promise version where there must be a satisfying configuration: add to the output of the decision reduction one extra coin that appears in all of the subsets, creating one bad, extra solution where that coin is heads and all of the others are tails.

Perfect matching has a straightforward reduction to this problem - represent edges as coins, for every vertex in the graph create a fact stipulating that exactly one of incidental edges is heads. A perfect matching in the original graph exists if and only if there's a solution in coins.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top