Sub topic 2
What It Means to Reduce One Problem to Another
This chapter assumes you can read short Python programs and know what a decision problem is: a question about an encoded instance whose answer is yes or no. The previous chapter covered that. Here we build the intuition for reductions. The formal definitions come in the next chapter.
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. You do not need to know how the B solver works. You only need a way to turn a question about A into a question about B, and then a way to read B's answer as an answer to A.
People reduce problems all the time without naming it. To find the median of a list, you can sort the list and take the middle element. Median-finding has been reduced to sorting. If someone gives you a faster sorting routine, your median code gets faster without any change.
A small worked example
Take the decision problem Distinctness: given a list of numbers, are all the elements different? We can reduce it to sorting. After a sort, any duplicate values sit next to each other, so checking adjacent pairs is enough.
def has_all_distinct(xs, sort_solver=sorted):
# Transform the instance: hand the list to the sorting solver
ys = sort_solver(xs)
# Interpret the answer: duplicates must now be adjacent
return all(ys[i] != ys[i + 1] for i in range(len(ys) - 1))
print(has_all_distinct([4, 1, 7, 3])) # True
print(has_all_distinct([4, 1, 7, 1])) # FalseThe reduction has two parts. The transformation prepares input for the B solver. The interpretation step turns the solver's output into the answer A needs. In this example the transformation does nothing to the list, and the interpretation step is a linear scan.
What a reduction tells you
A reduction compares two problems. If A reduces to B with cheap transform and interpret steps, then A is no harder than B, up to that extra cost. This gives you two uses:
| Use | What you know | What you conclude |
|---|---|---|
| Solving | B has a good algorithm | A has a good algorithm too |
| Proving hardness | A is known to be hard | B must be hard as well |
Both rows describe the same fact read in opposite directions. A later chapter covers why the direction matters and why it is easy to get backward.
Two styles you will meet
Some reductions call the B solver exactly once and return its answer unchanged. These are many-one reductions, the subject of the next chapter. Others call the solver several times or post-process its answers, as the distinctness example does. Those are Turing reductions, covered later. Keep the black-box picture in mind for both: you add work around the solver and never look inside it.
Exercise
You have a black-box function that returns the shortest path length between two vertices in an unweighted graph. Describe a reduction that decides whether two vertices s and t are connected, naming the transform and interpret steps.
Show answer
The transform passes the graph, s, and t to the shortest-path solver unchanged. The interpret step answers yes if the returned length is finite, and no if the solver reports that no path exists (infinity). Both steps take constant extra time, so connectivity is no harder than shortest paths.
Exercise
In the distinctness example, which part of the reduction would you change if the sorting solver returned the list in descending order?
Show answer
Neither part. Duplicates are still adjacent in a descending sort, so the adjacent-pair check still works. The interpret step relies only on equal values being next to each other.