Sub topic 7
Reductions: Comparing the Difficulty of Problems
This chapter assumes you know what a decision problem is, what the classes P and NP are, and how to read Big-O running times. It also assumes you are comfortable with basic graph terms: vertices, edges, and subsets of vertices.
So far, problems have been classified one at a time. That only goes so far. Usually you can't prove that a problem has no fast algorithm. You can often prove something weaker that is still useful: problem A is no harder than problem B. A reduction is the tool that makes this comparison precise.
The Definition
A polynomial-time many-one reduction from decision problem A to decision problem B is a function f with two properties:
- f can be computed in time polynomial in the size of its input.
- For every instance x, x is a yes-instance of A if and only if f(x) is a yes-instance of B.
When such an f exists, we write A ≤p B and read it as "A reduces to B." These reductions are also called Karp reductions. "Many-one" means that f may send many different instances of A to the same instance of B. It also means the reduction calls B exactly once and returns B's answer unchanged.
The reduction does not solve A. It translates each question about A into a question about B that has the same answer.
Which Direction Hardness Flows
The diagram shows how to build an algorithm for A from an algorithm for B:
def solve_A(x):
return solve_B(f(x))This gives two consequences that point in opposite directions:
| If you know... | and A ≤p B, then... |
|---|---|
| B is in P | A is in P (easiness flows from B down to A) |
| A has no polynomial-time algorithm | B has no polynomial-time algorithm (hardness flows from A up to B) |
| A is in P | nothing about B |
| B is hard | nothing about A |
The first row needs one more check. Suppose f runs in time p(n) and B's solver runs in time q(m). The instance f(x) can be at most p(n) symbols long, because f can't write more symbols than it has steps. So the total running time is p(n) + q(p(n)). A polynomial composed with a polynomial is still a polynomial.
One way to remember it: "A ≤p B" means "B is at least as hard as A." The arrow points toward the problem that is harder or equally hard.
A Concrete Reduction: Independent Set to Clique
Two problems are needed for the example:
- Independent Set: given a graph G and an integer k, does G contain k vertices with no edge between any two of them?
- Clique: given a graph H and an integer k, does H contain k vertices with an edge between every pair of them?
The complement graph of G has the same vertices as G. It has an edge between u and v exactly when G does not. A set S is independent in G if and only if S is a clique in the complement of G, because "no pair is joined" in G means "every pair is joined" in the complement. So the reduction is f(G, k) = (complement of G, k).
The following script implements f. It then checks the if-and-only-if property by brute force on a few hundred small random graphs.
import random
from itertools import combinations
def complement_edges(vertices, edges):
present = {frozenset(e) for e in edges}
return [(u, v) for u, v in combinations(vertices, 2)
if frozenset((u, v)) not in present]
def independent_set_to_clique(vertices, edges, k):
"""Maps an Independent Set instance (G, k) to a Clique instance (complement of G, k)."""
return vertices, complement_edges(vertices, edges), k
def has_independent_set(vertices, edges, k):
es = {frozenset(e) for e in edges}
return any(all(frozenset(p) not in es for p in combinations(s, 2))
for s in combinations(vertices, k))
def has_clique(vertices, edges, k):
es = {frozenset(e) for e in edges}
return any(all(frozenset(p) in es for p in combinations(s, 2))
for s in combinations(vertices, k))
random.seed(0)
for trial in range(300):
n = random.randint(1, 7)
vertices = list(range(n))
edges = [(u, v) for u, v in combinations(vertices, 2) if random.random() < 0.5]
k = random.randint(0, n)
lhs = has_independent_set(vertices, edges, k)
rhs = has_clique(*independent_set_to_clique(vertices, edges, k))
assert lhs == rhs, (vertices, edges, k)
print("Reduction preserved the answer on all 300 instances.")The checkers has_independent_set and has_clique take exponential time, which is acceptable here because they exist only for testing. The reduction itself runs in O(V²) time and produces O(V²) edges, so it is polynomial. Random tests like these are useful for catching bugs, but they don't prove anything. The proof is the one-sentence argument about complements.
Proving a Reduction Correct
Every reduction proof has three parts. Skipping any one of them is a common failure.
- Polynomial time. Bound the running time of f. This bound also limits the size of its output. A transformation that writes out all 2ⁿ subsets is not a reduction, even if it preserves answers.
- Yes maps to yes. If x is a yes-instance of A, show that f(x) is a yes-instance of B. In practice, you take a certificate for x and build a certificate for f(x).
- Yes in B comes only from yes in A. If f(x) is a yes-instance of B, show that x was a yes-instance of A. You can also prove the contrapositive: no-instances map to no-instances.
A second subtle point is that f must work on all instances of A, including degenerate ones. In the example, k = 0 and k > n must map correctly. For k = 0, both problems answer yes because the empty set counts. For k > n, both answer no. The test loop includes both cases.
Properties Worth Knowing
Transitivity. If A ≤p B and B ≤p C, then A ≤p C. The composed map g(f(x)) still runs in polynomial time, by the same polynomial-composition argument as before. This is why a family of hard problems can grow from a single starting problem. Each new problem only needs a reduction from some problem already known to be hard.
Reflexivity. A ≤p A holds through the identity map. So ≤p is a preorder: a relation that is reflexive and transitive. Two problems that reduce to each other are equally hard up to polynomial factors. Independent Set and Clique are an example, since the complement trick works in both directions.
Many-one versus Turing reductions. A Turing reduction, also called a Cook reduction, may call B's solver many times and process the answers afterward, for example by negating them. Turing reductions are more powerful. Under a Turing reduction, every problem reduces to its own complement just by flipping the answer. That would erase the distinction between NP and co-NP, which a later chapter covers. Many-one reductions keep that distinction, and that is why NP-completeness is defined with them.
| Kind | Calls to B | May change B's answer | Typical use |
|---|---|---|---|
| Many-one (Karp) | exactly one | no | defining NP-completeness |
| Turing (Cook) | polynomially many | yes | relating search and decision problems |
Common Errors Summary
- Reducing in the wrong direction when trying to prove hardness.
- Proving only one half of the if-and-only-if.
- A transformation f that secretly solves A. This is allowed only if it takes polynomial time, and if it does, A was already in P.
- Ignoring how numbers are encoded. A reduction that writes out a number k in unary takes exponential time if k was given in binary. This issue comes back in the chapter on pseudo-polynomial time.
Exercise
You have a polynomial-time reduction f from problem X to problem Y. You also know that Y is solvable in O(m³) time, where m is the size of a Y-instance, and that f runs in O(n²) time on X-instances of size n. Give a polynomial upper bound on the time to solve X, and explain why the size of f(x) matters.
Show answer
f(x) has size O(n²), because f can't output more symbols than its running time allows. Solving Y on f(x) costs O((n²)³) = O(n⁶). The total is O(n²) + O(n⁶) = O(n⁶). If you used the original size n instead of the size of f(x), you would wrongly conclude O(n³).
Exercise
A student wants to prove that a new problem Z is hard. They give a polynomial-time reduction Z ≤p Clique. Does this show Z is hard? What should they do instead?
Show answer
No. Z ≤p Clique shows only that Z is no harder than Clique, which is an upper bound on Z's difficulty. To show Z is hard, they need a reduction in the other direction, Clique ≤p Z (or from any other known hard problem to Z). They must prove that it runs in polynomial time and that it maps yes-instances to yes-instances and no-instances to no-instances.
Exercise
Does the map f(G, k) = (complement of G, k) also reduce Clique to Independent Set? Justify your answer.
Show answer
Yes. The complement of the complement of G is G itself. So S is a clique in G exactly when S is independent in the complement of G. The same O(V²) construction, applied to a Clique instance, gives an equivalent Independent Set instance. The two problems therefore reduce to each other.