Polynomial-Time Reductions
You have used reductions all along: bipartite matching was solved by building a flow network and running a max-flow algorithm, and difference constraints by building a graph and running Bellman-Ford. A reduction turns instances of one problem into instances of another, so that an algorithm for the second solves the first. NP-completeness uses the same tool in the opposite direction: if problem reduces to problem and is hard, then must be hard too. This lesson defines polynomial-time reductions, proves the two facts that make them useful, and practises the most common mistake, reducing in the wrong direction.
The definition
A language is polynomial-time reducible to a language , written , if there is a function computable in polynomial time such that for every input ,
The function is the reduction. Both directions of the "if and only if" are needed: yes-instances must map to yes-instances, and no-instances to no-instances. The reduction does not solve anything; it only translates.
Lemma 1. If and , then .
Proof. To decide , compute in time ; its length is then also . Run the decider for on it, in time , and return its answer, which is correct by the "if and only if". The total is polynomial.
Lemma 2. Reductions compose: if and , then .
Proof. Use ; a polynomial of a polynomial is a polynomial, and the equivalences chain.
Read Lemma 1 in contrapositive form and it becomes the tool for hardness: if and , then . "" says that is at least as hard as .
A reduction as an algorithm: matching to flow
The first example uses a reduction to get an algorithm. Bipartite matching ("does this bipartite graph have a matching of size ?") reduces to maximum flow ("does this network have a flow of value ?"): add a source joined to every left vertex, a sink joined from every right vertex, direct every edge from left to right, and give all edges capacity 1 (Maximum Bipartite Matching). The program builds , solves it with a small augmenting-path max flow and checks the answers against brute force.
from itertools import combinations
def reduce_matching_to_flow(left, right, edges):
"""f(x): build the network; matching of size k <=> flow of value k"""
cap = {}
for u in left:
cap[("s", u)] = 1
for v in right:
cap[(v, "t")] = 1
for u, v in edges:
cap[(u, v)] = 1
return cap
def max_flow(cap, s="s", t="t"):
r = dict(cap)
for (u, v) in list(cap):
r.setdefault((v, u), 0)
nodes = {x for e in r for x in e}
flow = 0
while True:
parent, stack = {s: None}, [s]
while stack and t not in parent:
u = stack.pop()
for v in sorted(nodes):
if v not in parent and r.get((u, v), 0) > 0:
parent[v] = u; stack.append(v)
if t not in parent:
return flow
v = t
while parent[v] is not None:
r[(parent[v], v)] -= 1; r[(v, parent[v])] += 1; v = parent[v]
flow += 1
def brute_matching(edges):
for k in range(len(edges), 0, -1):
for M in combinations(edges, k):
if len({u for u, v in M}) == k and len({v for u, v in M}) == k:
return k
return 0
left, right = ["a", "b", "c", "d"], ["W", "X", "Y", "Z"]
edges = [("a", "W"), ("a", "X"), ("b", "W"), ("c", "X"), ("c", "Y"), ("d", "X")]
net = reduce_matching_to_flow(left, right, edges)
print("network has", len(net), "edges; max flow =", max_flow(net), "; brute-force matching =", brute_matching(edges))network has 14 edges; max flow = 3 ; brute-force matching = 3
The answers agree: three of the four left vertices can be matched, since a, b and d together have only the two partners W and X.
A reduction for hardness: partition to subset sum
PARTITION asks whether a multiset of positive integers can be split into two parts with equal sums. SUBSET-SUM asks whether some subset of a multiset sums to exactly a target . The reduction:
If the numbers split into equal halves, either half is a subset with sum . Conversely, a subset with sum leaves a complement that also sums to . So , and takes linear time. Hence : subset sum is at least as hard as partition. A reduction is a claim about all inputs, and testing it on many random ones is a good habit (not a proof):
import random
from itertools import combinations
def partition(a):
return any(2 * sum(c) == sum(a) for k in range(len(a) + 1) for c in combinations(a, k))
def subset_sum(a, t):
return any(sum(c) == t for k in range(len(a) + 1) for c in combinations(a, k))
def f(a):
total = sum(a)
return (a, total // 2) if total % 2 == 0 else ([1], 2) # [1] with target 2 is a no-instance
rng = random.Random(12)
agree = yes = 0
for trial in range(500):
a = [rng.randint(1, 30) for _ in range(rng.randint(1, 8))]
lhs, rhs = partition(a), subset_sum(*f(a))
agree += lhs == rhs
yes += lhs
print(f"500 random instances: answers agree on {agree}, of which {yes} are yes-instances")500 random instances: answers agree on 500, of which 83 are yes-instances
Which direction?
To prove that a new problem is hard, reduce a known hard problem to : . The reduction must transform every instance of , so it shows that a fast algorithm for would give a fast algorithm for . Reducing to proves nothing about 's hardness: it only shows is no harder than . Mixing these up is the most common error in NP-completeness proofs.
Two more points worth keeping in mind:
- The reduction need not be onto. It may map into a special, restricted kind of instance of . PARTITION maps only to subset-sum instances whose target is half the total; that is fine.
- It must not solve along the way, unless that is cheap. A "reduction" that first finds the answer by brute force and then outputs a fixed yes- or no-instance is correct but takes exponential time, so it is not polynomial.
The standard chain of NP-completeness proofs in the next lessons is
CIRCUIT-SAT <=p SAT <=p 3-CNF-SAT <=p CLIQUE <=p VERTEX-COVER <=p HAM-CYCLE <=p TSP
\
<=p SUBSET-SUMeach arrow a reduction that transforms the left problem's instances into the right problem's.
Test yourself
What does require?
- A polynomial-time algorithm for
- A polynomial-time computable with for all
- That every instance of is an image of some instance of
- That and have the same yes-instances
You want to prove that a new problem is NP-hard, using a known NP-complete problem . Which reduction do you need?
- Both
- Neither: a brute-force algorithm for suffices
The partition instance is mapped to which subset-sum instance?
- Numbers with target 10
- Numbers with target 5
- Numbers with target 4
- Numbers with target 0
If and , what follows?
- Nothing