Sub topic 1
Problems, Instances, and Decision Versions
Before you can reduce one problem to another, you need a precise idea of what a "problem" is. This chapter fixes that idea. Every later chapter relies on it.
Prerequisites
This chapter assumes you know:
- Sets, functions, and basic graph terms (vertices, edges, adjacency)
- Big-O notation and what "polynomial time" means
- Enough Python to read and run short functions
Problems and Instances
A problem is a general question with parameters left open. An instance is one specific choice of those parameters. "Does this graph have an independent set of size k?" is a problem. A particular graph with 4 vertices, together with k = 2, is an instance of it.
An independent set is a set of vertices in which no two vertices are joined by an edge.
Decision Problems
A decision problem is a problem whose answer is always yes or no. Many natural problems ask for something else:
| Version | Question | Answer type |
|---|---|---|
| Search | Find a largest independent set | A set of vertices |
| Optimization | What is the size of the largest independent set? | A number |
| Decision | Is there an independent set of size at least k? | Yes or no |
Reductions are usually defined for decision problems because yes/no answers are easy to compare. A reduction only has to preserve one bit per instance. The decision version is rarely much easier than the others. If you can answer the decision question, you can find the optimum by asking about k = 1, 2, 3, … or by binary search on k:
from itertools import combinations
def has_independent_set(vertices, edges, k):
"""Decision version: is there an independent set of size >= k?"""
edge_set = {frozenset(e) for e in edges}
for subset in combinations(vertices, k):
if all(frozenset(p) not in edge_set for p in combinations(subset, 2)):
return True
return False
def max_independent_set_size(vertices, edges):
"""Optimization version, answered with calls to the decision version."""
lo, hi = 0, len(vertices) # size 0 always works
while lo < hi:
mid = (lo + hi + 1) // 2
if has_independent_set(vertices, edges, mid):
lo = mid
else:
hi = mid - 1
return lo
vertices = [1, 2, 3, 4]
edges = [(1, 2), (2, 3), (3, 4)]
print(has_independent_set(vertices, edges, 2)) # True ({1, 3})
print(has_independent_set(vertices, edges, 3)) # False
print(max_independent_set_size(vertices, edges)) # 2Binary search needs only O(log |V|) decision calls, so the optimization version costs at most a logarithmic factor more than the decision version. A later chapter shows how to recover the set itself.
Encodings and Languages
Algorithms read strings, not graphs. So we fix an encoding, a rule that writes each instance as a string over some alphabet (for example, an adjacency list in binary). A decision problem then becomes a language: the set of strings that encode yes-instances.
Two details cause trouble in practice:
- Malformed strings. Some strings encode no valid instance. By convention they are not in the language, so a decider rejects them.
- Number encoding. Running time is measured against input length. The number 1,000,000 takes about 20 bits in binary but a million symbols in unary. An algorithm that runs in time polynomial in a number's value can be exponential in its binary length.
Exercises
Exercise
State the decision version of the Shortest Path problem (given a weighted graph and vertices s and t, find a minimum-weight path from s to t). Then explain how to find the minimum weight using only the decision version, assuming nonnegative integer weights that sum to at most W.
Show answer
Decision version: given G, s, t, and an integer B, is there an s–t path of total weight at most B? The minimum weight lies between 0 and W, so binary search on B finds it with O(log W) decision calls. That count is polynomial in the binary length of the input.
Exercise
An algorithm decides whether an integer n is prime by trying every divisor from 2 to n − 1. Is it polynomial time when n is written in binary? When n is written in unary?
Show answer
It takes about n steps. In binary, the input length is about log n, so n steps is exponential in the input length. In unary, the input length is n, so the algorithm is polynomial. The encoding changes the verdict.