Sub topic 3
P: Problems We Can Solve Efficiently
This chapter assumes you know Big-O notation, can read Python, and know basic graph terms such as vertices, edges, and adjacency lists. It builds on the two previous chapters: problems stated as yes/no questions, and polynomial running time as the test for tractability.
The Definition
P is the class of decision problems that some algorithm solves in polynomial time. In other words, a problem is in P if there is an algorithm and a constant k such that, on every instance of size n, the algorithm gives the correct yes or no answer in O(n^k) steps.
The definition has three conditions, and each one rules something out:
- Decision problems only. P contains yes/no questions. "Find the shortest path" is not in P as stated, but "Is there a path of length at most L?" can be.
- Every instance. The bound must hold in the worst case. If an algorithm is fast on typical inputs but exponential on some inputs, it does not place the problem in P.
- Size of the encoding. n is the number of bits needed to write the input down. Graphs are measured by V + E, and a number is measured by its digit count, not by its value. A later chapter on pseudo-polynomial time depends on this detail.
A Problem in P: 2-Coloring
A graph is 2-colorable if you can give each vertex one of two colors so that no edge joins two vertices of the same color. The algorithm below uses breadth-first search. It colors a starting vertex, gives each neighbor the opposite color, and reports a conflict if one appears.
from collections import deque
def is_two_colorable(adj):
"""adj: dict mapping each vertex to a list of neighbors (undirected graph)."""
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
square = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [2, 0]}
triangle = {0: [1, 2], 1: [0, 2], 2: [0, 1]}
print(is_two_colorable(square)) # True
print(is_two_colorable(triangle)) # FalseEach vertex enters the queue once, and the algorithm scans each adjacency list once. The total running time is O(V + E), which is polynomial in the input size, so 2-coloring is in P.
Common Errors
Other mistakes to avoid:
| Mistake | Correction |
|---|---|
| "My code is slow, so the problem is not in P." | P concerns the best possible algorithm, not one particular implementation. |
| "O(n^100) is not efficient, so it doesn't count." | It still counts as polynomial. In practice, natural problems in P usually have small exponents. |
| "Average-case speed is enough." | P requires a worst-case bound. |
| "P is closed under nothing useful." | P is closed under complement. If you can answer yes in polynomial time, you can answer no in polynomial time by flipping the output. |
Familiar members of P include shortest paths, checking whether a graph is connected, bipartite matching, linear programming, and primality testing. The AKS algorithm proved in 2002 that primality testing is in P.
Exercise
An algorithm decides whether an n-vertex graph has a clique of size 4 by checking every set of 4 vertices. Does this show that "Is there a clique of size 4?" is in P? Does it show that "Is there a clique of size k?" is in P, where k is part of the input?
Show answer
Yes for size 4: there are C(n, 4) = O(n^4) sets, and each one takes constant time to check, so the total is polynomial. No for size k: with k as part of the input, C(n, k) can grow exponentially, for example when k = n/2. That general problem is NP-complete.
Exercise
Why does closure under complement follow directly from the definition of P?
Show answer
Run the polynomial-time algorithm for the problem and invert its answer. Inverting adds constant time, so the complement problem is also decided in polynomial time.