Sub topic 2
What It Means to Reduce One Problem to Another
This chapter assumes you can read short Python programs, know what a function is in the mathematical sense, and have met decision problems as described in the previous chapter: questions with a yes or no answer about an encoded instance. Big-O notation helps in one section but is not essential.
The goal here is intuition. Later chapters give formal definitions of many-one and Turing reductions and work out exactly what each one proves. This chapter covers the basic move that all of them share.
The core idea
A reduction from problem A to problem B is a method for solving A that uses a solver for B as a black box. The method may change the input before it calls the solver, and it may interpret the answer afterwards. It never looks inside the solver.
If such a method exists and is cheap, then any solver for B gives you a solver for A at little extra cost. Programmers do this all the time without calling it a reduction. Calling a library sort to find duplicates is one example. Calling a shortest-path routine to plan a route is another. In both cases you solve your problem by restating it as a problem someone has already solved.
The notation is A ≤ B, read "A reduces to B." The symbol points the way it does because B is at least as powerful as A: a solver for B is enough to solve A.
A worked example: string rotation
Problem A, called Rotation, asks: given strings a and b, is b a rotation of a? For example, "cdeab" is a rotation of "abcde" because you get it by moving "ab" from the front to the back.
Problem B, called Substring, asks: given strings p and t, does p occur somewhere inside t?
The reduction rests on one observation. When a and b have the same length, b is a rotation of a exactly when b is a substring of a + a. Doubling a lays out every rotation of it as a window of length len(a).
from itertools import product
def is_substring(p, t):
"""Black-box solver for Substring. We only call it; we never inspect it."""
return p in t
def is_rotation(a, b):
"""Solve Rotation by reducing to Substring."""
if len(a) != len(b):
# Lengths differ, so the answer is no. Send a fixed
# no-instance to the solver to keep the single-call shape.
return is_substring("x", "")
return is_substring(b, a + a)
def is_rotation_brute_force(a, b):
"""Direct definition, used only to test the reduction."""
if len(a) != len(b):
return False
return any(a[i:] + a[:i] == b for i in range(max(len(a), 1)))
# Check the reduction against the definition on every pair of
# strings over {a, b} with length up to 4.
alphabet = "ab"
strings = [""]
for n in range(1, 5):
strings += ["".join(chars) for chars in product(alphabet, repeat=n)]
for a in strings:
for b in strings:
assert is_rotation(a, b) == is_rotation_brute_force(a, b), (a, b)
assert is_rotation("abcde", "cdeab")
assert not is_rotation("abcde", "abced")
print("Rotation reduces to Substring: all checks passed")This example shows three habits that carry through the rest of the tutorial.
- The transformation is cheap. Building
a + atakes linear time. Any cost you care about lives in the Substring solver. - Correctness has two directions. If
bis a rotation ofa, thenboccurs ina + a. Ifboccurs ina + aand the lengths match, thenbis a rotation. If either direction fails, the reduction gives wrong answers. - Edge cases need a deliberate decision. When the lengths differ, the code still calls the solver once, using an instance whose answer is known to be no. Keeping that single-call shape matters for the formal definition in the next chapter.
Two shapes of reduction
Reductions differ in how they use the black box. Two shapes matter most.
| Shape | How the solver for B is used | Formal name (later chapter) | Example |
|---|---|---|---|
| Transform and ask once | Build one instance of B and return its answer unchanged | Many-one (mapping) reduction | Rotation to Substring |
| Use as a subroutine | Call the solver any number of times and combine the answers freely | Turing (oracle) reduction | Finding an actual solution by repeatedly asking yes/no questions |
The first shape is stricter. It cannot even negate the answer. Because of that strictness it preserves more structure, and NP-completeness is defined with it. The second shape is more flexible and closer to how programmers think about subroutines. Later chapters treat both in detail.
Everyday reductions you already use
Many familiar algorithms are reductions in disguise.
| Problem A | Reduced to B | Transformation |
|---|---|---|
| Does a list contain a duplicate? | Sorting | Sort, then compare neighbours |
| Find the median | Sorting | Sort, then take the middle element |
| Least common multiple of m and n | Greatest common divisor | lcm(m, n) = m·n / gcd(m, n) |
| Maximum bipartite matching | Maximum flow | Add a source and a sink, and give every edge capacity 1 |
| Scheduling with conflicts | SAT | Encode each constraint as a clause |
The first three are not decision problems, so they are reductions in the informal, subroutine sense. The last two come back in the chapter on practical reductions. In all of them the hard part of the work is done by the solver for B.
What a reduction tells you
A reduction from A to B is a statement about relative difficulty. It says A is no harder than B, up to the cost of the transformation. That statement can be read two ways.
- Forward use: you have a good algorithm for B, so you now have one for A. This is how practitioners use reductions.
- Backward use: you believe A has no good algorithm, so B cannot have one either, because a fast B solver would give a fast A solver. This is how theorists prove hardness.
The most common mistake in the whole subject is to reverse this. Reducing A to B says nothing about whether B is easy, and nothing about whether A is hard. The Rotation reduction does not show that Substring is as easy as Rotation. It shows that Rotation is no harder than Substring. The chapter after next covers direction carefully.
The cost of the transformation matters
A reduction is only useful if the transformation is cheaper than the problem you are studying. Consider a "reduction" from Rotation to Substring that first works out the answer by brute force and then sends the solver either ("a", "a") or ("x", ""). The output is technically always correct. But all the work happens before the solver is called, so the reduction shows nothing about how the two problems relate.
For this reason every formal reduction has a resource bound attached. For undecidability, the transformation only has to be computable. For NP-completeness, it has to run in polynomial time. The bound must be weaker than the difficulty you are trying to measure. Otherwise the reduction could solve the problem on its own.
Common errors at this stage
- Checking only one direction of correctness. A mapping that turns every yes-instance into a yes-instance can still turn some no-instances into yes-instances.
- Looking inside the black box. A reduction may use only the solver's answers, never how it works internally.
- Getting the direction backwards when stating the conclusion, as described above.
- Forgetting degenerate inputs such as empty strings, empty graphs, or mismatched sizes.
Exercise
Problem A asks whether two lists x and y contain the same elements with the same multiplicities, so that one is a permutation of the other. Describe a reduction from A to sorting, state the cost of the transformation, and explain why it is correct in both directions.
Show answer
Sort both lists with the sorting black box and compare the results element by element, answering yes exactly when the sorted lists are equal. Apart from the sorting calls, the work is a linear-time comparison (plus a length check). If y is a permutation of x, the two lists hold the same multiset of elements, so sorting gives the same sequence. If the sorted sequences are equal, then the multisets are equal, so y is a permutation of x. This is the subroutine shape: it makes two calls and post-processes the outputs.
Exercise
A classmate shows that Rotation reduces to Substring and concludes that "Substring is no harder than Rotation." What is wrong with the conclusion, and what does the reduction actually show?
Show answer
The conclusion has the direction backwards. Rotation ≤ Substring shows that Rotation is no harder than Substring: any Substring solver yields a Rotation solver after a linear-time transformation. It says nothing about solving Substring with a Rotation solver. Showing that would need a separate reduction from Substring to Rotation.
Exercise
Someone proposes this reduction from Rotation to Substring: run the brute-force rotation check, and if the answer is yes, call issubstring("a", "a"), otherwise call issubstring("x", ""). Every answer is correct. Why is this not a meaningful reduction?
Show answer
The transformation solves Rotation by itself, so the Substring solver adds nothing. A reduction is meaningful only when the transformation is limited to a resource bound below the difficulty being measured, so that the real work falls to the solver for B. This construction ignores that requirement, so it relates the two problems no more closely than any trivial mapping would.