TExcelTAD

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 AA reduces to problem BB and AA is hard, then BB 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 L1L_1 is polynomial-time reducible to a language L2L_2, written L1≤PL2L_1 \le_P L_2, if there is a function ff computable in polynomial time such that for every input xx,

x∈L1  ⟺  f(x)∈L2.x \in L_1 \iff f(x) \in L_2 .

The function ff 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 L1≤PL2L_1 \le_P L_2 and L2∈PL_2 \in \mathrm{P}, then L1∈PL_1 \in \mathrm{P}.

Proof. To decide xx, compute f(x)f(x) in time O(∣x∣a)O(\lvert x \rvert^a); its length is then also O(∣x∣a)O(\lvert x \rvert^a). Run the decider for L2L_2 on it, in time O(∣f(x)∣b)=O(∣x∣ab)O(\lvert f(x) \rvert^b) = O(\lvert x \rvert^{ab}), and return its answer, which is correct by the "if and only if". The total is polynomial.

Lemma 2. Reductions compose: if L1≤PL2L_1 \le_P L_2 and L2≤PL3L_2 \le_P L_3, then L1≤PL3L_1 \le_P L_3.

Proof. Use g∘fg \circ f; 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 L1≤PL2L_1 \le_P L_2 and L1∉PL_1 \notin \mathrm{P}, then L2∉PL_2 \notin \mathrm{P}. "L1≤PL2L_1 \le_P L_2" says that L2L_2 is at least as hard as L1L_1.

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 kk?") reduces to maximum flow ("does this network have a flow of value kk?"): 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 f(x)f(x), solves it with a small augmenting-path max flow and checks the answers against brute force.

python · try it yourself
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 tt. The reduction:

f(a1,…,an)=(a1,…,an; t=12∑iai),or a fixed no-instance if the total is odd.f(a_1, \ldots, a_n) = \Bigl(a_1, \ldots, a_n;\ t = \tfrac{1}{2}\textstyle\sum_i a_i\Bigr), \quad \text{or a fixed no-instance if the total is odd.}

If the numbers split into equal halves, either half is a subset with sum tt. Conversely, a subset with sum tt leaves a complement that also sums to tt. So x∈PARTITION  ⟺  f(x)∈SUBSET-SUMx \in \text{PARTITION} \iff f(x) \in \text{SUBSET-SUM}, and ff takes linear time. Hence PARTITION≤PSUBSET-SUM\text{PARTITION} \le_P \text{SUBSET-SUM}: 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):

python · try it yourself
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 BB is hard, reduce a known hard problem AA to BB: A≤PBA \le_P B. The reduction must transform every instance of AA, so it shows that a fast algorithm for BB would give a fast algorithm for AA. Reducing BB to AA proves nothing about BB's hardness: it only shows BB is no harder than AA. 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 BB. PARTITION maps only to subset-sum instances whose target is half the total; that is fine.
  • It must not solve AA 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

text
CIRCUIT-SAT  <=p  SAT  <=p  3-CNF-SAT  <=p  CLIQUE  <=p  VERTEX-COVER  <=p  HAM-CYCLE  <=p  TSP
                                   \
                                    <=p  SUBSET-SUM

each arrow a reduction that transforms the left problem's instances into the right problem's.

Test yourself

What does L1≤PL2L_1 \le_P L_2 require?

  1. A polynomial-time algorithm for L1L_1
  2. A polynomial-time computable ff with x∈L1  ⟺  f(x)∈L2x \in L_1 \iff f(x) \in L_2 for all xx
  3. That every instance of L2L_2 is an image of some instance of L1L_1
  4. That L1L_1 and L2L_2 have the same yes-instances

You want to prove that a new problem BB is NP-hard, using a known NP-complete problem AA. Which reduction do you need?

  1. B≤PAB \le_P A
  2. A≤PBA \le_P B
  3. Both
  4. Neither: a brute-force algorithm for BB suffices

The partition instance 3,1,4,23, 1, 4, 2 is mapped to which subset-sum instance?

  1. Numbers 3,1,4,23, 1, 4, 2 with target 10
  2. Numbers 3,1,4,23, 1, 4, 2 with target 5
  3. Numbers 3,13, 1 with target 4
  4. Numbers 3,1,4,23, 1, 4, 2 with target 0

If L1≤PL2L_1 \le_P L_2 and L2≤PL3L_2 \le_P L_3, what follows?

  1. L3≤PL1L_3 \le_P L_1
  2. L1≤PL3L_1 \le_P L_3
  3. L2∈PL_2 \in \mathrm{P}
  4. Nothing