Sub topic 6
Polynomial-Time Reductions
This chapter assumes you know what a many-one reduction is and how to prove one correct in both directions. It also assumes Big-O notation, the idea of input length measured in bits, and the Independent Set, Vertex Cover, and Clique reductions from the previous chapter. Here we add one more requirement to a reduction: it has to be fast. That requirement is what lets reductions say something about efficient computation, and not only about whether a problem can be computed at all.
The Definition
A polynomial-time reduction from problem A to problem B, also called a Karp reduction and written A ≤p B, is a function f with two properties:
- Correctness: for every string x, x ∈ A if and only if f(x) ∈ B.
- Efficiency: some deterministic algorithm computes f(x) in time O(|x|^c) for a fixed constant c, where |x| is the length of the encoding of x.
The first condition is the many-one condition you have already seen. The second is new. Without it, a reduction could secretly solve A by itself, for example by brute force, and then output a fixed yes-instance or no-instance of B. That reduction would be correct and would tell you nothing about B.
Why the output stays small
An algorithm that runs for t steps can write at most t symbols. So if f runs in time p(n) on inputs of length n, then |f(x)| ≤ p(|x|). This bound is the core of every argument that uses polynomial-time reductions.
What Transfers: Algorithms Forward
Theorem. If A ≤p B and B can be decided in polynomial time, then A can be decided in polynomial time.
Proof. Let f run in time p(n), and let the decider for B run in time q(m). On input x with |x| = n:
- Compute y = f(x). This takes at most p(n) steps, and |y| ≤ p(n).
- Run the decider for B on y. This takes at most q(|y|) ≤ q(p(n)) steps.
- Output its answer. Correctness says this answer is also the right answer for x.
The total is p(n) + q(p(n)). Polynomials are closed under addition and composition, so the total is a polynomial in n. ∎
The contrapositive gives you the use you will rely on later in the tutorial. If A ≤p B and A has no polynomial-time algorithm, then B has none either. Hardness moves backward along the arrow.
Concrete exponents matter when you estimate costs. If f runs in O(n²) and B is decided in O(m³), the composed algorithm runs in O((n²)³) = O(n⁶). The result is still polynomial, but much slower than either piece. Polynomial-time reductions keep the class P intact. They do not keep running times close to the original.
Input Length Is Measured in Bits
The most common mistake here comes from measuring "polynomial" against the wrong quantity. Input length is the number of symbols in a reasonable encoding. A number N written in binary takes about log₂ N bits. An algorithm whose running time grows with N, and not with log N, can be exponential in the input length.
| Instance feature | Encoding size | Running time O(N) as a function of input length |
|---|---|---|
| Graph with n vertices, adjacency matrix | n² bits | polynomial |
| Integer N in binary | ⌈log₂(N+1)⌉ bits | exponential (2^bits) |
| Integer N in unary | N symbols | polynomial |
| List of n integers, each below 2^b | about n·b bits | depends on whether time grows with b or with 2^b |
A reduction that looks efficient but is not
Subset Sum asks: given non-negative integers a₁, …, aₙ and a target T, does some subset add up to exactly T? We can map it to Reachability: build a layered graph whose node (i, s) means "some subset of the first i numbers adds up to s", then ask whether (n, T) can be reached from (0, 0). Reachability can be solved in linear time with breadth-first search.
from collections import deque
def subset_sum_to_reachability(nums, target):
# Node (i, s): some subset of nums[:i] sums to s.
edges = []
for i, a in enumerate(nums):
for s in range(target + 1):
edges.append(((i, s), (i + 1, s))) # skip nums[i]
if s + a <= target:
edges.append(((i, s), (i + 1, s + a))) # take nums[i]
return edges, (0, 0), (len(nums), target)
def reachable(edges, src, dst):
adj = {}
for u, v in edges:
adj.setdefault(u, []).append(v)
seen, queue = {src}, deque([src])
while queue:
u = queue.popleft()
if u == dst:
return True
for v in adj.get(u, []):
if v not in seen:
seen.add(v)
queue.append(v)
return False
def solve_subset_sum(nums, target):
return reachable(*subset_sum_to_reachability(nums, target))
assert solve_subset_sum([3, 5, 7], 12) is True # 5 + 7
assert solve_subset_sum([3, 5, 7], 4) is False
# Output size versus input size as the target grows
nums = [3, 5, 7]
for bits in (4, 8, 12, 16):
target = 2 ** bits - 1
input_bits = sum(x.bit_length() for x in nums) + target.bit_length()
edges, _, _ = subset_sum_to_reachability(nums, target)
print(f"input ~{input_bits:3d} bits -> {len(edges):7d} edges")The mapping is correct: a path to (n, T) picks out exactly one subset that sums to T. But the graph has about 2n(T + 1) edges. When the target gains one bit, the graph roughly doubles. The printed table shows this: the input grows by 4 bits per row while the edge count grows about 16 times per row. This is a pseudo-polynomial reduction. It runs in time polynomial in the value T and exponential in the length of T. It does not show that Subset Sum is in P, and Subset Sum is in fact NP-complete.
Checking That a Reduction Is Polynomial
The reductions in the previous chapter pass this check easily, and it helps to see why:
- Independent Set to Vertex Cover copies the graph and replaces k with |V| − k. Copying takes linear time. The new number has at most as many bits as |V|.
- Clique to Independent Set complements the edge set. It looks at each of the n(n−1)/2 vertex pairs once, so it takes O(n²) time and writes O(n²) edges. That is polynomial in the input size, whatever the encoding.
A useful routine for any new reduction:
- Bound the size of the output in terms of the input size n.
- Bound the time to compute each output element.
- Confirm that both bounds hold for every input, including malformed strings.
On the third point: f has to be defined on all strings, not only valid encodings. The standard fix is to check the input format first, which takes polynomial time, and map every malformed string to one fixed no-instance of B.
Edge Cases Worth Knowing
Problems already in P reduce to almost anything
If A ∈ P and B has at least one yes-instance y₁ and one no-instance y₀, then A ≤p B. The reduction decides A directly in polynomial time and outputs y₁ or y₀. This is legal. It is also why a reduction into B, by itself, tells you nothing about B unless A is believed to be hard.
The trivial languages
The empty language ∅ and the language of all strings Σ cannot be reduction targets for any other problem, because there is no yes-instance or no-instance available to map to. Only ∅ reduces to ∅, and only Σ reduces to Σ*. This is a technical point, but it explains why statements such as "every problem in P is P-complete under ≤p" exclude these two languages.
Polynomial-time reductions are too strong inside P
Inside P, a reduction allowed polynomial time can solve the source problem on its own, so ≤p cannot tell problems in P apart. To compare problems inside P, researchers use weaker reductions, such as log-space reductions or the fine-grained reductions covered in the final chapter.
Composition stays polynomial
If A ≤p B through f, which runs in time p, and B ≤p C through g, which runs in time q, then g(f(x)) runs in time p(n) + q(p(n)). The proof is the same as the main theorem. The chapter on chaining reductions builds on this fact.
Exercise
A reduction f from A to B runs in O(n^3) time, and B can be decided in O(m^2 log m) time on inputs of length m. Give a polynomial bound on the time to decide A, and explain why the bound is valid.
Show answer
Since f runs in O(n^3), its output has length O(n^3). Deciding B on that output takes O((n^3)^2 log(n^3)) = O(n^6 log n). Adding the O(n^3) cost of computing f gives O(n^6 log n) in total. The bound is valid because a running time also bounds the output length, and polynomials are closed under composition.
Exercise
The subsetsumto_reachability function is a correct many-one reduction, and Reachability is in P. Why does this not prove Subset Sum is in P? Under what restriction on the inputs would it?
Show answer
The output graph has about 2n(T+1) edges. That is exponential in the number of bits needed to write T, so the reduction does not run in polynomial time. If the numbers are written in unary, or if T is bounded by a polynomial in n, the graph size is polynomial in the input length. The reduction is then a valid Karp reduction and puts that restricted version in P.
Exercise
Let A be any problem in P and let B be the language of graphs that contain a triangle. Construct a polynomial-time reduction from A to B.
Show answer
Fix y1 = the graph made of a single triangle, which is in B, and y0 = a single vertex with no edges, which is not in B. On input x, run the polynomial-time decider for A. Output y1 if it accepts and y0 otherwise. The reduction runs in polynomial time, and x is in A exactly when the output is in B.