Sub topic 1
Problems as Yes/No Questions
Complexity theory classifies problems by how hard they are. Before it can do that, it needs a precise definition of "problem." This chapter gives that definition. It explains why the theory works with questions that have only two possible answers, and it shows how to separate a problem from its individual inputs.
Prerequisites
This chapter assumes you can:
- Read and run short Python programs.
- Use Big-O notation to describe running time, such as O(n) or O(n²).
- Use basic graph vocabulary: vertices, edges, paths, and adjacency lists.
- Follow simple Boolean logic with AND, OR, and NOT.
You do not need any background in formal languages or Turing machines. Where those ideas come up, the chapter explains them.
What a Decision Problem Is
A decision problem is a question about an input whose answer is always either yes or no. Here are some examples:
- Given an integer n, is n prime?
- Given a graph G and two vertices s and t, is there a path from s to t?
- Given a list of integers and a target T, does some subset of the list add up to exactly T?
- Given a Boolean formula, is there an assignment of true and false to its variables that makes the formula true?
Each of these is a general question. A specific input to the question is called an instance. "Is 91 prime?" is an instance of the primality problem, and its answer is no, because 91 = 7 × 13. "Is 97 prime?" is another instance, and its answer is yes.
The problem is the whole infinite family of instances together with the correct answer for each one. An algorithm solves a decision problem when it gives the correct answer on every instance, not only on the instances you happened to test.
Yes-instances and no-instances
A decision problem splits all possible instances into two groups:
- Yes-instances are inputs where the answer is yes.
- No-instances are inputs where the answer is no.
You can describe the problem completely by saying which instances are yes-instances. In formal treatments, each instance is written as a string of symbols, and the problem is identified with the language of strings that encode yes-instances. "Deciding the language" and "solving the decision problem" mean the same thing.
Why Restrict Attention to Yes/No Questions?
Many real problems do not look like yes/no questions. A route planner wants the shortest route, not just confirmation that a route exists. Theory still starts with decision problems, for three reasons.
- Comparing problems is simpler. If every problem outputs one bit, then asking whether problem A is "no harder than" problem B only requires you to translate inputs. You never have to translate outputs. Later chapters on reductions depend on this.
- Little is lost. Most optimization problems can be rephrased as decision problems by adding a threshold. "What is the shortest path from s to t?" becomes "Is there a path from s to t with at most k edges?" If you can answer the threshold question quickly, you can usually recover the optimum quickly too, for example by binary search on k. A later chapter covers this in detail.
- The classes are defined this way. P, NP, and the classes built on them are sets of decision problems. To use them correctly, you have to state your problem in yes/no form.
A Worked Example: Bounded Path
The problem BOUNDED-PATH asks: given an undirected graph G, vertices s and t, and an integer k, is there a path from s to t that uses at most k edges?
An instance is the whole tuple (G, s, t, k). Changing any part of it, including only k, gives a different instance that may have a different answer.
from collections import deque
def bounded_path(adj, s, t, k):
"""Decision problem BOUNDED-PATH.
adj: dict mapping each vertex to a list of neighbors (undirected graph).
Returns True exactly when some path from s to t uses at most k edges."""
if s not in adj or t not in adj or k < 0:
return False
dist = {s: 0}
queue = deque([s])
while queue:
u = queue.popleft()
if u == t:
return dist[u] <= k
for v in adj[u]:
if v not in dist:
dist[v] = dist[u] + 1
queue.append(v)
return False # t is unreachable
graph = {
"a": ["b", "c"],
"b": ["a", "d"],
"c": ["a", "d"],
"d": ["b", "c", "e"],
"e": ["d"],
}
print(bounded_path(graph, "a", "e", 3)) # True: a-b-d-e has 3 edges
print(bounded_path(graph, "a", "e", 2)) # False: shortest path needs 3
print(bounded_path(graph, "a", "z", 9)) # False: z is not a vertexThe function returns only a Boolean. Internally it computes distances, but the answer to the decision problem is one bit. The same graph produces a yes-instance when k = 3 and a no-instance when k = 2.
The function also treats malformed input as a no-instance. Here that means a vertex that is not in the graph or a negative k. This is the usual convention: strings that do not encode a valid instance are simply not in the language.
Input Size and Encoding
Running time is measured as a function of the input size, usually written n. Input size is the number of symbols, typically bits, needed to write the instance down. The definition sounds obvious, but it is where many mistakes start.
| Instance | Reasonable size measure | Why |
|---|---|---|
| A graph with V vertices and E edges | about V + E (times log V for vertex labels) | An adjacency list stores each vertex and edge once |
| A Boolean formula | number of symbols in the formula | Every literal and operator must be written |
| An integer N | about log₂ N bits | Binary notation writes N in log₂ N digits |
| A list of m integers, each at most N | about m · log₂ N bits | Each number takes log₂ N bits |
The integer row matters most. The number 1,000,000 takes about 20 bits to write, not a million. An algorithm that loops N times on input N therefore runs in time exponential in the input size, because N is about 2ⁿ when n is the number of bits. Trial division for primality is a familiar example: it looks linear in N but is exponential in n. A later chapter on pseudo-polynomial time returns to this point with Subset Sum.
Any reasonable encoding, such as adjacency lists, adjacency matrices, or binary integers, gives sizes that differ by at most a polynomial factor. So the choice among them does not affect which problems count as efficiently solvable. Unary encoding is the main unreasonable one. It writes N as N tally marks, which inflates the input size exponentially and makes slow algorithms look fast.
Common Errors
- Confusing the problem with an instance. "This graph is hard" is not meaningful in complexity theory. Hardness is a property of a problem across all instances, measured as input size grows.
- Dropping the threshold. "Find the largest clique" is not a decision problem. "Does G contain a clique of size at least k?" is one, and k is part of the input.
- Mixing up yes and no. "Is G free of cycles?" and "Does G contain a cycle?" are different decision problems with swapped yes-instances. For some classes, which side is "yes" makes a real difference. The chapter on co-NP shows why.
- Mis-measuring size. Treating a numeric value as the input length makes exponential algorithms look polynomial.
Exercises
Exercise
Rewrite "Find the minimum number of colors needed to color the vertices of a graph G so that no edge joins two vertices of the same color" as a decision problem. State the input and the question.
Show answer
Input: a graph G and an integer k. Question: can the vertices of G be colored with at most k colors so that no edge has both endpoints the same color? The integer k is part of each instance.
Exercise
An algorithm decides whether an integer N is prime by testing every divisor from 2 to the square root of N. Is its running time polynomial in the input size? Explain.
Show answer
No. The input size is n ≈ log₂ N bits, and the algorithm performs about √N = 2^(n/2) divisions. That is exponential in n, even though it looks sublinear in the value N.
Exercise
Using the boundedpath function and the graph from this chapter, what does boundedpath(graph, "c", "b", 1) return, and is (graph, "c", "b", 1) a yes-instance or a no-instance?
Show answer
It returns False, so it is a no-instance. Vertices c and b are not adjacent. The shortest path, c-a-b or c-d-b, uses 2 edges, which is more than k = 1. Changing k to 2 gives a yes-instance.