Sub topic 15
co-NP: Efficiently Checkable No Answers
This chapter assumes you know the definitions of P and NP from earlier chapters, can read a polynomial-time verifier written in Python, and know what an NP-complete problem is. It also uses SAT and 2-coloring from earlier chapters.
The definition of NP is lopsided. A problem is in NP when every yes-instance has a short certificate that a polynomial-time verifier accepts. Nothing in that definition requires no-instances to have short proofs. This chapter covers the class you get by switching the roles of yes and no.
The Asymmetry in NP
Take SAT. If a formula is satisfiable, you can prove it by giving a satisfying assignment, and anyone can check that assignment in linear time. Now suppose the formula is unsatisfiable. How would you convince a skeptic? Showing that one assignment fails proves nothing. The obvious proof lists all 2^n assignments and shows that each one fails, which is exponentially long.
No one knows a way to give short, efficiently checkable proofs of unsatisfiability in general. That gap is the reason co-NP needs its own definition.
Defining co-NP
The complement of a decision problem L, written co-L or L̄, has the same instances with the answers flipped. Every yes-instance of L is a no-instance of co-L, and the reverse.
A problem L is in co-NP exactly when its complement is in NP. In terms of certificates:
- L is in NP if every yes-instance has a polynomial-length certificate that a polynomial-time verifier accepts, and no certificate fools the verifier on a no-instance.
- L is in co-NP if every no-instance has a polynomial-length certificate (often called a counterexample or disqualifier) that a polynomial-time verifier accepts, and no such certificate exists for a yes-instance.
| Property | NP | co-NP |
|---|---|---|
| Short proof exists for | yes-instances | no-instances |
| Typical certificate | a solution | a counterexample |
| Canonical complete problem | SAT | UNSAT, TAUTOLOGY |
| Example | "Is this graph 3-colorable?" | "Is this graph *not* 3-colorable?" |
| Example | "Is this number composite?" | "Is this number prime?" |
Examples of co-NP Problems
- UNSAT: Is this CNF formula unsatisfiable? A no-answer means the formula is satisfiable, and a satisfying assignment certifies it.
- TAUTOLOGY: Is this Boolean formula true under every assignment? A no-answer is certified by one assignment that makes the formula false.
- Non-Hamiltonicity: Does this graph have no Hamiltonian cycle? A no-answer is certified by a Hamiltonian cycle.
- Primality: Is n prime? A no-answer (n is composite) is certified by a nontrivial factor.
The pattern is the same each time. The certificate is a witness that the universal claim ("for every assignment", "no cycle exists") fails.
A co-NP verifier for TAUTOLOGY
A DNF formula is an OR of terms, and each term is an AND of literals. Deciding whether a DNF formula is a tautology is co-NP-complete. The verifier below checks a counterexample: an assignment under which every term is false.
def verify_dnf_counterexample(terms, assignment):
"""terms: list of terms, each a list of nonzero ints (3 means x3, -3 means NOT x3).
assignment: dict mapping variable number to bool.
Returns True if the assignment makes the whole DNF false, which proves
the formula is NOT a tautology."""
for term in terms:
if all(assignment[abs(lit)] == (lit > 0) for lit in term):
return False # this term is true, so the formula is true here
return True
# (x1 AND x2) OR (NOT x1): not a tautology, since x1=True, x2=False falsifies it
print(verify_dnf_counterexample([[1, 2], [-1]], {1: True, 2: False})) # True
# (x1) OR (NOT x1): a tautology, so no counterexample can pass
print(verify_dnf_counterexample([[1], [-1]], {1: True})) # False
print(verify_dnf_counterexample([[1], [-1]], {1: False})) # FalseThe verifier runs in time linear in the formula size. Finding a counterexample, or showing none exists, is the hard part.
How P, NP, and co-NP Relate
P sits inside both
P is closed under complement. If a polynomial-time algorithm decides L, you can flip its output and get a polynomial-time algorithm for co-L. So P equals co-P. Every problem in P is in NP, so every problem in P is also in co-NP:
P ⊆ NP ∩ co-NP
NP ∩ co-NP: problems with short proofs both ways
Some problems have short certificates for yes and for no answers. Bipartiteness is a simple case. A graph is 2-colorable if and only if it has no odd cycle. A valid 2-coloring certifies yes, and an odd cycle certifies no. This problem is also in P, since breadth-first search decides it, but the two-sided certificates show the structure clearly.
def verify_odd_cycle(adj, cycle):
"""adj: dict mapping each vertex to a list of neighbors (undirected).
cycle: list of distinct vertices v0, v1, ..., v(k-1) claimed to form a cycle.
Returns True if the cycle is valid and has odd length, which proves
the graph is NOT 2-colorable."""
k = len(cycle)
if k < 3 or k % 2 == 0:
return False
if len(set(cycle)) != k or any(v not in adj for v in cycle):
return False
neighbors = {u: set(vs) for u, vs in adj.items()}
return all(cycle[(i + 1) % k] in neighbors[cycle[i]] for i in range(k))
triangle_plus_tail = {"a": ["b", "c"], "b": ["a", "c"], "c": ["a", "b", "d"], "d": ["c"]}
print(verify_odd_cycle(triangle_plus_tail, ["a", "b", "c"])) # True: not bipartite
print(verify_odd_cycle(triangle_plus_tail, ["a", "b"])) # False: too shortPrimality is the historically important example. Pratt showed in 1975 that primes have short certificates, which put PRIMES in NP ∩ co-NP decades before the AKS algorithm (2002) proved it is in P. The decision version of integer factoring ("does n have a factor at most k?") is in NP ∩ co-NP and is not known to be in P. This is why many researchers doubt factoring is NP-complete, as the next section explains.
What would follow from various answers
- If NP ≠ co-NP, then P ≠ NP. The reason is that P is closed under complement, so P = NP would force NP = co-NP.
- If any NP-complete problem is in co-NP, then NP = co-NP. Every NP problem reduces to that problem, so every NP problem would have short no-certificates.
- P = NP ∩ co-NP is also open. Most researchers believe P is strictly smaller.
The second point gives a practical rule of thumb. If you find a problem with short, checkable proofs for both answers, it is very unlikely to be NP-complete.
co-NP-Completeness
A problem is co-NP-complete if it is in co-NP and every co-NP problem reduces to it by a polynomial-time many-one reduction. The complement of any NP-complete problem is co-NP-complete. The reason is that a reduction f from A to B, which satisfies x ∈ A ⇔ f(x) ∈ B, also satisfies x ∉ A ⇔ f(x) ∉ B. The same f therefore reduces co-A to co-B.
UNSAT and TAUTOLOGY are the standard co-NP-complete problems. In practice, proving a program correct, checking that two circuits are equivalent, and showing that a schedule has no conflicts all have this "for all" shape.
Common Errors
- Treating the complement as just flipping the output. That trick only works for deterministic algorithms. A verifier that rejects a bad certificate tells you nothing about whether a good certificate exists.
- Ignoring malformed inputs. Formally, the complement of a language also contains strings that are not valid encodings. Most texts restrict attention to well-formed instances, and you should state that you are doing so.
- Assuming NP-hard problems are not in co-NP. No one has proved this. It holds only if NP ≠ co-NP.
- Confusing co-NP with practical difficulty. Modern SAT solvers often prove unsatisfiability of large industrial formulas. They do this by emitting proofs (such as DRAT proofs) that can be very long, which fits with the belief that UNSAT has no short certificates in general.
Exercise
Explain why "Is this graph 3-colorable?" is in NP, and state what certificate would place its complement, "Is this graph not 3-colorable?", in NP. Is such a certificate known?
Show answer
3-colorability is in NP because a coloring that assigns one of three colors to each vertex can be checked edge by edge in O(V + E) time. To put the complement in NP, you would need a polynomial-length, polynomial-time-checkable proof that no 3-coloring exists. No such certificate is known. Because 3-coloring is NP-complete, finding one would imply NP = co-NP.
Exercise
Suppose someone proves that SAT is in co-NP. What follows, and why?
Show answer
NP = co-NP. Every NP problem A reduces to SAT by some f. A no-instance x of A maps to an unsatisfiable formula f(x), which by assumption has a short checkable certificate. Computing f(x) and checking that certificate certifies that x is a no-instance, so A is in co-NP. That gives NP ⊆ co-NP, and by symmetry co-NP ⊆ NP.
Exercise
Write the certificate and verifier logic showing that "Is n composite?" is in NP, and explain why that same fact puts PRIMES in co-NP.
Show answer
The certificate is an integer d with 1 < d < n. The verifier checks the bounds and that n % d == 0, which takes time polynomial in the number of digits of n. A no-instance of PRIMES is a composite number, and the factor d certifies it. Short certificates for no-instances are exactly what membership in co-NP requires.