Sub topic 4
Which Way Does It Go? Transferring Algorithms and Hardness
This chapter assumes you know the definition of a many-one reduction from the previous chapter: a computable function f with x ∈ A if and only if f(x) ∈ B. It also assumes you can read basic proofs and follow short Python programs. The goal here is to get one fact right every time. When A reduces to B, written A ≤ B, algorithms move from B to A, and hardness moves from A to B.
Most mistakes with reductions come from getting this direction backwards. The mistake is easy to make because the arrow in A ≤ B points the opposite way from the direction hardness travels.
Reading A ≤ B Correctly
Read A ≤ B as "A is no harder than B." The symbol behaves like an inequality on difficulty. If you can solve B, you can solve A: transform the input with f, then ask the B-solver. So B is the larger side, at least as hard as A.
Two consequences follow, and they are contrapositives of each other:
- Algorithms transfer backward along the arrow. If B has an algorithm, then A has one: run f, then run B's algorithm.
- Hardness transfers forward along the arrow. If A has no algorithm (or no efficient one), then B has none either. If B had one, statement 1 would give A one, which is a contradiction.
The two statements are one logical fact seen from two sides. The statements that look similar but do not follow are listed in the table below.
| Given A ≤ B and... | Conclusion | Valid? |
|---|---|---|
| B is solvable (efficiently) | A is solvable (efficiently) | Yes |
| A is unsolvable (or hard) | B is unsolvable (or hard) | Yes |
| A is solvable (efficiently) | B is solvable (efficiently) | No |
| B is unsolvable (or hard) | A is unsolvable (or hard) | No |
The two invalid rows are the usual traps. An easy A says nothing about B, because every easy problem reduces to very hard ones. A hard B says nothing about A for the same reason.
Transferring an Algorithm: A Worked Example
PARTITION asks whether a list of nonnegative integers can be split into two parts with equal sums. SUBSET-SUM asks whether some sublist of the numbers adds up exactly to a target t. PARTITION reduces to SUBSET-SUM. If the total is even, ask for a subset summing to half the total. If the total is odd, the answer is no, so output any fixed no-instance of SUBSET-SUM.
The code below uses the reduction in the algorithm-transfer direction. It takes a SUBSET-SUM solver and gets a PARTITION solver from it without writing any partition-specific search.
def subset_sum(nums, target):
# Dynamic programming over reachable sums; nums are nonnegative ints.
reachable = {0}
for x in nums:
reachable |= {s + x for s in reachable if s + x <= target}
return target in reachable
def partition_to_subset_sum(nums):
total = sum(nums)
if total % 2 == 1:
return [], 1 # fixed no-instance: the empty list cannot sum to 1
return list(nums), total // 2
def partition(nums):
# Algorithm for A (PARTITION) built from an algorithm for B (SUBSET-SUM).
return subset_sum(*partition_to_subset_sum(nums))
assert partition([3, 1, 1, 2, 2, 1]) is True # {3, 2} vs {1, 1, 2, 1}
assert partition([2, 3]) is False # total is odd
assert partition([1, 5]) is False # total even, no split
assert partition([]) is True # two empty halves
print("all checks passed")The odd-total branch matters. A many-one reduction must output an instance of B for every input, including inputs whose answer is obviously no. Returning False directly would make this a slightly different kind of reduction. Mapping to a fixed no-instance keeps it many-one.
Transferring Hardness: The Same Reduction, Read the Other Way
Now suppose you know PARTITION is hard. It is NP-complete, a term defined in a later chapter. The same function f shows SUBSET-SUM is hard too. If SUBSET-SUM had a polynomial-time algorithm, the partition function above would solve PARTITION in polynomial time.
This is the template for every hardness proof in this tutorial. To show a new problem X is hard:
- Pick a problem K that is already known to be hard.
- Build a reduction from K to X, that is, K ≤ X.
- Prove the reduction is correct in both directions and that it is cheap enough.
The known-hard problem is always the source, and the new problem is always the target.
Why the Wrong Direction Feels Right
The backwards argument usually goes like this: "My problem looks like SAT, and I can write it as a SAT formula, so my problem is as hard as SAT." Writing a problem as a SAT formula is a reduction to SAT. It gives you an algorithm, since you can hand the formula to a SAT solver. That is useful in practice, and a later chapter is built around it. It does not give you a lower bound.
Here is a quick check. Ask which problem's solver you are pretending to have. In A ≤ B you pretend to have a solver for B and use it to build a solver for A. Whatever you pretend to have is the problem that must be at least as hard.
Edge Cases That Affect What Transfers
The cost of the reduction limits what transfers. If f takes exponential time, then a fast algorithm for B gives only an exponential algorithm for A. To transfer "solvable in polynomial time," f must run in polynomial time, and its output size must be polynomial too. To transfer decidability, f only needs to be computable. The next chapter on polynomial-time reductions makes this precise. For now, the rule is that the reduction must be no more expensive than the class you want to transfer.
Trivial targets. If B is the language of all strings, or the empty language, almost nothing reduces to it, because f would have to send every no-instance or every yes-instance somewhere impossible. This is why NP-hardness proofs never target the trivial languages, and why the fixed no-instance in the PARTITION example has to exist in B.
Equivalence needs two reductions. One reduction gives an inequality. To say A and B are equally hard, you need A ≤ B and B ≤ A. PARTITION and SUBSET-SUM do reduce to each other, but the code above shows only one direction.
Hardness of B is not a property of A's instances. f may map A's instances into a small, easy corner of B. A hard B can therefore contain an easy A without any contradiction. This is the same point as the invalid fourth row of the table, seen from inside B.
| You want to show | Reduction to build | Reduction cost needed |
|---|---|---|
| A is decidable, given B decidable | A ≤ B | Computable |
| X is undecidable, given K undecidable | K ≤ X | Computable |
| A in P, given B in P | A ≤ B | Polynomial time |
| X is NP-hard, given K NP-hard | K ≤ X | Polynomial time |
Exercise
A classmate proves "Problem X is NP-hard" by giving a polynomial-time function that turns every instance of X into a 3-SAT formula that is satisfiable exactly when the X-instance is a yes-instance. What has actually been shown, and what would they need to show instead?
Show answer
They showed X ≤ 3-SAT, so X is no harder than 3-SAT. This puts X in NP (given that the reduction runs in polynomial time), but it gives no lower bound. To show X is NP-hard they need a polynomial-time reduction in the other direction, from 3-SAT (or another NP-hard problem) to X.
Exercise
You know A ≤ B with a computable reduction, and you learn that A is decidable. What can you conclude about B?
Show answer
Nothing. A decidable source tells you nothing about the target, because decidable problems reduce to undecidable ones as well. Only a decidable B, or an undecidable A, leads to a conclusion.
Exercise
Suppose f reduces A to B but runs in time 2^n, and B has a linear-time algorithm. What running time does this give for A, and why does that not show A is in P?
Show answer
Computing f takes about 2^n time and can produce an output of size up to 2^n. B's linear algorithm then takes time linear in that output, so the total is O(2^n). The reduction's own cost dominates, so polynomial solvability does not transfer. You would need a polynomial-time f.
Exercise
Modify partitiontosubset_sum so that for an odd total it returns a different valid no-instance of SUBSET-SUM that uses the original numbers. Explain why it is a no-instance.
Show answer
Return (list(nums), sum(nums) + 1). Every subset of nonnegative numbers sums to at most sum(nums), so no subset can reach sum(nums) + 1, and the instance is always a no-instance.