Sub topic 6
Polynomial-Time Reductions
This chapter assumes you already know many-one reductions and how to prove them correct in both directions. It also assumes you can read big-O running times. Here we add one condition to the reduction, a limit on how long it may run. That condition is what makes reductions useful for studying efficient computation.
The definition
A polynomial-time reduction (also called a Karp reduction) from problem A to problem B is a function f with two properties:
- For every instance x, x is a yes-instance of A if and only if f(x) is a yes-instance of B.
- Some algorithm computes f(x) in time O(n^c) for a constant c, where n is the length of the encoding of x.
We write A ≤p B. The first property is the many-one condition you already know. The second is new.
Why the time bound matters
Suppose B has an algorithm that runs in time q(m) on inputs of size m, and f runs in time p(n). In p(n) steps, f can write at most p(n) symbols, so |f(x)| ≤ p(n). The combined algorithm for A therefore runs in time
p(n) + q(p(n)).
A polynomial composed with a polynomial is still a polynomial. So if B is in P, A is in P too. Without the time bound this conclusion fails. A reduction that is allowed exponential time could just solve A by brute force and output a fixed yes- or no-instance of B, and then it tells you nothing.
A worked example: Vertex Cover to Set Cover
In Set Cover you are given a universe U, a collection of subsets of U, and a number k. You ask whether k of the subsets have U as their union. To reduce Vertex Cover to Set Cover, make each edge an element of U and give each vertex the set of edges it touches.
from itertools import combinations
def vertex_cover_to_set_cover(vertices, edges, k):
universe = set(range(len(edges))) # one element per edge
sets = {v: {i for i, (a, b) in enumerate(edges) if v in (a, b)}
for v in vertices}
return universe, sets, k
def has_set_cover(universe, sets, k):
# brute force, only to check small cases
for r in range(k + 1):
for chosen in combinations(sets, r):
if set().union(*(sets[c] for c in chosen)) == universe:
return True
return False
vertices = [1, 2, 3, 4]
edges = [(1, 2), (2, 3), (3, 4)]
print(has_set_cover(*vertex_cover_to_set_cover(vertices, edges, 2))) # True: {2, 3}
print(has_set_cover(*vertex_cover_to_set_cover(vertices, edges, 1))) # FalseThe reduction runs in O(|V|·|E|) time, which is polynomial. Correctness holds in both directions. A vertex cover C covers every edge, so the sets for C cover U. Going the other way, any k sets that cover U name k vertices that touch every edge. The brute-force checker takes exponential time, but that does not matter. Only f has to be fast.
What counts as "polynomial"
| Situation | Polynomial? | Reason | Column | Column |
|---|---|---|---|---|
| Complementing a graph's edges | Yes | O(\ | V\ | ²) output |
| Building one gadget per clause | Yes | Linear in formula size | ||
| Converting CNF to an equivalent DNF | No | Output can grow exponentially | ||
| Writing a binary number N in unary | No | N is exponential in its bit length |
Exercise
A reduction f runs in O(n^2) time, and B has an algorithm running in O(m^3) time. What bound do you get for deciding A by computing f and then running B's algorithm?
Show answer
|f(x)| is O(n^2), so B's algorithm takes O((n^2)^3) = O(n^6) time. Adding the O(n^2) for computing f gives O(n^6) in total.
Exercise
Someone "reduces" A to B by solving A with an exponential-time search and then outputting a fixed yes-instance or a fixed no-instance of B. Is this a polynomial-time reduction, and what goes wrong?
Show answer
It is not polynomial-time, because f takes exponential time. It satisfies the if-and-only-if condition, but it says nothing about how A relates to B, since f does all the work and B's solver adds nothing.