Sub topic 14
From Yes/No Answers to Actual Solutions
This chapter assumes you already know decision problems, Turing (oracle) reductions, NP, and NP-completeness from earlier chapters. You should also be able to read short Python functions. It deals with a practical gap. A decision procedure tells you whether a solution exists, but in practice you usually need the solution itself. Self-reducibility closes that gap. It turns a yes/no oracle into an algorithm that builds a solution, using only polynomially many oracle calls.
The Search Problem Behind Every Decision Problem
Every NP problem comes with a verifier, and the verifier defines a search problem. Given an instance x, find a certificate y that the verifier accepts, or report that none exists. For Independent Set the certificate is a set of k pairwise non-adjacent vertices. For SAT it is a satisfying assignment.
Search is at least as hard as decision. If you can find a certificate, you can certainly say whether one exists. The interesting question runs the other way: can a decision oracle help you find a certificate? A problem is self-reducible when the search version reduces to the decision version in polynomial time.
The Pattern: Shrink, Ask, Commit
Most self-reductions follow one loop:
- Ask the oracle whether the full instance has a solution. If not, stop.
- Pick one piece of the instance, such as a vertex or a variable, and make a tentative decision about it.
- Build the smaller or more constrained instance that reflects that decision, and ask the oracle about it.
- If the oracle says yes, commit to the decision. Otherwise commit to the alternative, which must then be safe.
- Repeat until every piece is decided.
The key invariant is that the current instance always has a solution. The loop changes the instance only in ways the oracle has confirmed keep a solution in reach.
Worked Example 1: Finding an Independent Set
The oracle has_is(vertices, edges, k) answers whether the graph contains an independent set of size k. To make the code runnable, the oracle below uses brute force. In theory you would plug in any decision procedure.
from itertools import combinations
def brute_force_has_is(vertices, edges, k):
edge_set = {frozenset(e) for e in edges}
for cand in combinations(vertices, k):
if all(frozenset(p) not in edge_set for p in combinations(cand, 2)):
return True
return False
def find_independent_set(vertices, edges, k, has_is):
# has_is(vertices, edges, k) -> bool is a decision oracle
if not has_is(vertices, edges, k):
return None
chosen = list(vertices)
for v in list(vertices):
trial = [u for u in chosen if u != v]
trial_edges = [(a, b) for a, b in edges
if a in trial and b in trial]
if has_is(trial, trial_edges, k):
chosen = trial
return chosen # exactly k vertices, pairwise non-adjacent
vertices = [1, 2, 3, 4, 5]
edges = [(1, 2), (2, 3), (3, 4), (4, 5)]
result = find_independent_set(vertices, edges, 3, brute_force_has_is)
print(result) # [1, 3, 5]Here the tentative choice is to delete vertex v. If the graph without v still has a size-k independent set, v is not needed, so it is discarded for good.
Why the output is correct
Call the final vertex set F. The invariant guarantees that the graph induced on F has an independent set of size k. Take any vertex v that survived. When v was tested, removing it from the vertex set at that moment destroyed every size-k independent set. F is a subset of that set with v removed, so F without v also has no size-k independent set. Every size-k independent set of F must therefore contain v.
That holds for every vertex of F, so some size-k independent set of F contains all of F. This means F has exactly k vertices and no edges among them.
Cost
The algorithm makes one initial call and then one call per vertex, for |V| + 1 calls in total. Each call also needs O(|V| + |E|) work to build the trial graph. If the oracle runs in polynomial time, the whole search runs in polynomial time. This is why P = NP would give polynomial-time search and not just polynomial-time decision.
Worked Example 2: Finding a Satisfying Assignment
For SAT, the pieces are variables and the tentative choice is to set one of them to true. Setting a literal to true removes every clause that contains it and deletes its negation from the remaining clauses.
from itertools import product
def condition(clauses, lit):
# Make literal `lit` true and simplify.
return [tuple(l for l in c if l != -lit)
for c in clauses if lit not in c]
def brute_force_sat(clauses):
variables = sorted({abs(l) for c in clauses for l in c})
for bits in product([False, True], repeat=len(variables)):
val = dict(zip(variables, bits))
if all(any(val[abs(l)] == (l > 0) for l in c) for c in clauses):
return True
return False
def find_assignment(clauses, n, sat):
if not sat(clauses):
return None
assignment = {}
for x in range(1, n + 1):
trial = condition(clauses, x)
if sat(trial):
clauses, assignment[x] = trial, True
else:
clauses, assignment[x] = condition(clauses, -x), False
return assignment
formula = [(1, 2), (-1, 3), (-2, -3), (-3, 2)]
print(find_assignment(formula, 3, brute_force_sat))
# {1: False, 2: True, 3: False}The "else" branch needs no second oracle call. The current formula is satisfiable, so if x = true leads to an unsatisfiable formula, x = false cannot. An empty clause means a contradiction, and the brute-force oracle handles it correctly because any over an empty tuple returns False.
Optimization Problems Fit the Same Frame
To find a maximum independent set, first find the optimum size, then search. You can find the largest k with has_is true by binary search over 0 to |V|, which costs O(log |V|) calls. After that, find_independent_set makes |V| + 1 more calls. The pipeline from optimization to search to decision therefore stays polynomial.
Gotchas
| Issue | What goes wrong | Fix |
|---|---|---|
| Oracle domain | Trial instances must be valid instances of the same problem. Deleting a vertex keeps a graph a graph, but some problems lose their special structure, for example planarity with a fixed embedding, or a promise. | Pick a shrink step that keeps the instance inside the problem's domain. |
| Missing invariant check | Committing to a choice without asking the oracle can lead to a dead end. | Commit only to choices the oracle confirmed, or to alternatives that are forced. |
| Recomputing from scratch | Querying the original instance with extra constraints each time can make instances grow. | Carry the simplified instance forward, as `condition` does. |
| Assuming the reverse | Self-reducibility is about search, not hardness. It says nothing about whether the decision problem is easy. | Keep hardness claims in many-one reductions. |
Exercises
Exercise
In findindependentset, suppose the loop skipped the initial oracle call and the input graph had no independent set of size k. What would the function return, and why is that wrong?
Show answer
Every trial call would return False, so no vertex would ever be removed and the function would return the full vertex list. That list is not an independent set of size k (unless the graph happens to be edgeless with exactly k vertices). The initial call establishes the invariant that the current instance has a solution, and the correctness proof depends on it.
Exercise
Give a self-reduction for Vertex Cover that uses a decision oracle has_vc(vertices, edges, k). Describe the tentative choice and how k changes.
Show answer
Process vertices one at a time. Tentatively put v in the cover: delete v and all its incident edges, and ask has_vc on the remaining graph with k - 1. If yes, commit (add v to the cover, decrement k). If no, v is not in any valid cover of the current graph, so all of v's neighbors must be in it. Add them to the cover, delete them with their edges, and reduce k by their number (each such step can be confirmed with the oracle). This uses O(|V|) calls. Alternatively, find an independent set of size |V| - k by self-reduction and return its complement.
Exercise
How many oracle calls does find_assignment make on a formula with n variables, and can you give an input where the else branch is taken for every variable?
Show answer
It makes exactly n + 1 calls: one initial call and one per variable. For the formula with unit clauses (-1,), (-2,), ..., (-n,), setting any variable to true creates an empty clause, so every trial fails and every variable is set to False.