Sub topic 1
Problems as Yes/No Questions
Prerequisites
This chapter assumes you can read basic Python and Big-O notation. You should also know basic graph terms (vertex, edge, adjacency list) and Boolean logic.
Decision Problems
Complexity theory classifies decision problems, which are questions whose answer is yes or no. An instance is one specific input. "Can this graph be colored with 2 colors so that no edge joins two vertices of the same color?" is a decision problem. Each particular graph you feed it is an instance.
Many problems we care about are optimization problems, such as "what is the shortest route?" These usually have a decision version, such as "is there a route of length at most k?" Using yes/no questions keeps the theory uniform.
Why Polynomial Time Means Tractable
An algorithm runs in polynomial time if its running time is O(n^c) for some constant c, where n is the size of the input. Exponential growth such as 2^n becomes useless quickly. At n = 60, 2^n is already about 10^18 steps. Polynomial bounds also stay polynomial when you combine algorithms, which makes them a stable working definition of "efficient."
P: Problems We Can Solve Efficiently
P is the class of decision problems that some algorithm solves in polynomial time. 2-coloring is in P because breadth-first search decides it in O(V + E):
from collections import deque
def is_two_colorable(adj):
color = {}
for start in adj:
if start in color:
continue
color[start] = 0
queue = deque([start])
while queue:
u = queue.popleft()
for v in adj[u]:
if v not in color:
color[v] = 1 - color[u]
queue.append(v)
elif color[v] == color[u]:
return False
return True
print(is_two_colorable({0: [1], 1: [0, 2], 2: [1]})) # True
print(is_two_colorable({0: [1, 2], 1: [0, 2], 2: [0, 1]})) # FalseChecking an Answer Versus Finding One
A certificate is a proposed proof that the answer is yes. A verifier is an algorithm that checks a certificate. In Subset Sum, the question is whether some subset of the numbers adds up to a target. A certificate is a list of indices, and checking it is fast:
def verify_subset_sum(numbers, target, certificate):
if len(set(certificate)) != len(certificate):
return False
if any(i < 0 or i >= len(numbers) for i in certificate):
return False
return sum(numbers[i] for i in certificate) == target
print(verify_subset_sum([3, 34, 4, 12, 5, 2], 9, [2, 4])) # True: 4 + 5Finding a certificate by brute force can mean trying all 2^n subsets.
NP: Problems Whose Answers We Can Check Efficiently
NP is the class of decision problems where every yes-instance has a certificate of polynomial size, and a polynomial-time verifier accepts only valid certificates. Every problem in P is also in NP, because the verifier can ignore the certificate and solve the problem directly.
Exercise
Why does verifysubsetsum show that Subset Sum is in NP but not that it is in P?
Show answer
It checks a given certificate in polynomial time, and that is the requirement for NP. Membership in P would need an algorithm that finds the answer in polynomial time without being given a certificate, and no such algorithm is known.