Sub topic 1
Problems as Yes/No Questions
Complexity theory sorts problems by how hard they are. To compare problems fairly, it first puts each one in the same shape: a question with a yes or no answer. This chapter explains that shape and why it is used.
Prerequisites
This chapter assumes you can:
- Read and run short Python functions.
- Read Big-O notation, such as O(n²).
- Use basic graph terms: vertices, edges, and adjacency lists.
Decision Problems and Instances
A decision problem is a general question whose answer is always yes or no. An instance is one concrete input to that question.
| Decision problem | Example instance | Answer |
|---|---|---|
| Is the integer n prime? | n = 91 | No (91 = 7 × 13) |
| Does graph G have a path from s to t? | A specific G, s, t | Yes or no |
| Does some subset of these numbers sum to T? | [3, 34, 4, 12, 5, 2], T = 9 | Yes (4 + 5) |
The problem is the full set of instances. Each instance has exactly one correct answer. You can also describe a decision problem as the set of all instances whose answer is yes. Later chapters call this set a language.
A Decision Problem in Code
Here is the path question written as a function. It takes an instance and returns a boolean.
from collections import deque
def has_path(adj, s, t):
"""adj: dict mapping each vertex to a list of neighbors."""
seen = {s}
queue = deque([s])
while queue:
u = queue.popleft()
if u == t:
return True
for v in adj[u]:
if v not in seen:
seen.add(v)
queue.append(v)
return False
graph = {"a": ["b"], "b": ["c"], "c": [], "d": ["a"]}
print(has_path(graph, "a", "c")) # True
print(has_path(graph, "c", "a")) # FalseThe function does not return the path, the path's length, or a list of every path. It only answers the question.
Why Restrict to Yes/No?
A yes/no output keeps the theory simple:
- Comparisons are uniform. When every problem returns one bit, you can turn one problem into another just by converting its inputs. Reductions, covered later, depend on this.
- Nothing important is lost. Most optimization problems have a decision version with a threshold, such as "is there a route of length at most k?" If you can answer that version quickly, you can usually solve the optimization problem quickly too. A later chapter shows how.
- Output size stops mattering. Some problems have huge outputs. A single bit means running time measures only the work of deciding.
Common Errors
- Treating a search problem as a decision problem. "Find the shortest path" is a search problem. "Is there a path of length at most k?" is its decision version.
- Mixing up the problem and the instance. "Is 91 prime?" is an instance. "Is n prime?" is the problem.
- Forgetting the no answers. A decision procedure must give the right answer on every instance, including every instance whose answer is no.
Exercise
Rewrite "Find the minimum number of colors needed to color graph G so no edge joins two same-colored vertices" as a decision problem, and give its input.
Show answer
Input: a graph G and an integer k. Question: can G's vertices be colored with at most k colors so that no edge joins two vertices of the same color? The answer is yes or no.
Exercise
For the instance n = 1,000,003 of "is n prime?", is the input size about 1,000,003 or about 20?
Show answer
About 20. Writing n in binary takes ⌈log₂(n + 1)⌉ = 20 bits, and input size counts bits.