PTAS, FPTAS and the Class APX
Some NP-hard problems can be approximated to any accuracy we like, at a price in running time. Others have a best constant, and some have none at all. This lesson defines the classes that sort problems by how well they can be approximated, proves the most useful dividing line (strong NP-hardness rules out an FPTAS), and runs a first approximation scheme.
Approximation schemes
Definition. An approximation scheme for a minimization problem is a family of algorithms , one for each , where is a -approximation. (For maximization, .)
- A PTAS (polynomial-time approximation scheme): for each fixed , runs in time polynomial in . The degree may depend on : is allowed.
- An EPTAS (efficient PTAS): time for some function , with the exponent of independent of .
- An FPTAS (fully polynomial): time polynomial in both and , such as .
| Running time of | , | class |
|---|---|---|
| PTAS only | ||
| about | EPTAS | |
| FPTAS |
Knapsack has an FPTAS (A Knapsack FPTAS by Profit Scaling); Euclidean TSP has a PTAS (Euclidean TSP: the Idea of Arora's PTAS); bin packing has an asymptotic PTAS but, as shown below, no PTAS unless P = NP.
A first scheme: makespan on a fixed number of machines
Jobs with processing times go to identical machines; the makespan is the largest machine load. Graham (1969) gave this scheme for fixed :
PTAS-MAKESPAN(p sorted decreasingly, m, k)
1 schedule jobs 1..k optimally by trying all m^k assignments
2 for j = k+1 .. n: put job j on the currently least loaded machine
3 return the scheduleTheorem (Graham, 1969). The schedule has makespan at most .
Proof. Let job finish last. If , the makespan is that of the optimal schedule of the first jobs, which is at most OPT. Otherwise started on the least loaded machine, whose load was at most the average of the other jobs, . So the makespan is at most . Finally, the longest jobs each take at least , and some machine receives at least of them, so .
To reach , choose ; the running time is polynomial for fixed and , but exponential in . The program shows the trade-off.
from itertools import product
def optimal_makespan(p, m):
loads = {tuple([0] * m)}
for x in p: # reachable sorted load vectors
loads = {tuple(sorted(L[:i] + (L[i] + x,) + L[i + 1:])) for L in loads for i in range(m)}
return min(max(L) for L in loads)
def ptas(p, m, k):
p = sorted(p, reverse=True)
best, tried = None, 0
for assign in product(range(m), repeat=k): # all m^k assignments of the k largest jobs
tried += 1
L = [0] * m
for x, i in zip(p[:k], assign):
L[i] += x
if best is None or max(L) < max(best):
best = L
L = list(best)
for x in p[k:]:
L[L.index(min(L))] += x
return max(L), tried
m = 3
for p in ([10, 10, 8, 8, 6, 6, 6, 3, 2, 1], [5, 5, 4, 4, 3, 3, 3]):
opt = optimal_makespan(p, m)
print("jobs", p, " OPT", opt)
for k in [0, 3, 6, 9][: 1 + len(p) // 3]:
ms, tried = ptas(p, m, k)
bound = 1 + (1 - 1 / m) / (1 + k // m)
print(f" k = {k}: makespan {ms}, ratio {ms / opt:.3f} <= bound {bound:.3f}, assignments tried {tried}")jobs [10, 10, 8, 8, 6, 6, 6, 3, 2, 1] OPT 20
k = 0: makespan 22, ratio 1.100 <= bound 1.667, assignments tried 1
k = 3: makespan 22, ratio 1.100 <= bound 1.333, assignments tried 27
k = 6: makespan 22, ratio 1.100 <= bound 1.222, assignments tried 729
k = 9: makespan 20, ratio 1.000 <= bound 1.167, assignments tried 19683
jobs [5, 5, 4, 4, 3, 3, 3] OPT 9
k = 0: makespan 11, ratio 1.222 <= bound 1.667, assignments tried 1
k = 3: makespan 11, ratio 1.222 <= bound 1.333, assignments tried 27
k = 6: makespan 11, ratio 1.222 <= bound 1.222, assignments tried 729
The guessing work grows as while the guarantee shrinks towards 1. On the first instance only guesses well enough to be optimal. The second instance shows that the bound is tight: with the six largest jobs are spread perfectly as , yet the last job of length 3 then pushes one machine to 11, while the optimum has makespan 9, a ratio of exactly .
In C++
The same scheme with the brute force over the assignments written as a counter in base .
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
int ptas(std::vector<int> p, int m, int k) {
std::sort(p.begin(), p.end(), std::greater<int>());
long long total = 1;
for (int i = 0; i < k; i++) total *= m;
std::vector<int> best;
for (long long code = 0; code < total; code++) {
std::vector<int> L(m, 0);
long long c = code;
for (int i = 0; i < k; i++) { L[c % m] += p[i]; c /= m; }
if (best.empty() || *std::max_element(L.begin(), L.end()) < *std::max_element(best.begin(), best.end())) best = L;
}
for (size_t j = k; j < p.size(); j++) *std::min_element(best.begin(), best.end()) += p[j];
return *std::max_element(best.begin(), best.end());
}
int main() {
std::vector<int> p = {10, 10, 8, 8, 6, 6, 6, 3, 2, 1};
for (int k : {0, 3, 6, 9}) std::printf("k = %d: makespan %d\n", k, ptas(p, 3, k));
}k = 0: makespan 22
k = 3: makespan 22
k = 6: makespan 22
k = 9: makespan 20
Strong NP-hardness rules out an FPTAS
A problem is strongly NP-hard if it stays NP-hard when all numbers in the input are bounded by a polynomial in the input length (equivalently, written in unary). Bin packing, makespan with part of the input, and TSP are strongly NP-hard; knapsack and subset sum are not, which is exactly why their pseudo-polynomial dynamic programs exist (Subset Sum).
Theorem. Let be a minimization problem with integer objective values, and suppose for a polynomial , where is the largest number in . If has an FPTAS, it has a pseudo-polynomial exact algorithm. So if is strongly NP-hard, it has no FPTAS unless P = NP.
Proof. Run the FPTAS with . The time is polynomial in and , hence in and . The result satisfies , and since values are integers, . On instances with numbers bounded by a polynomial this exact algorithm is polynomial, contradicting strong NP-hardness unless P = NP.
The program watches the argument happen on a small knapsack instance, using the profit-scaling FPTAS of A Knapsack FPTAS by Profit Scaling. Since , the choice is below , so the result must be exact, and the table of the dynamic program grows to the size of the pseudo-polynomial exact algorithm.
from itertools import combinations
def fptas(p, s, B, eps):
n, P = len(p), max(p)
K = eps * P / n
q = [int(x // K) for x in p] # rounded profits
best = {0: (0, ())} # rounded profit -> (smallest size, items)
for i in range(n):
for val, (size, items) in list(best.items()):
nv, ns = val + q[i], size + s[i]
if ns <= B and (nv not in best or ns < best[nv][0]):
best[nv] = (ns, items + (i,))
chosen = best[max(best)][1]
return sum(p[i] for i in chosen), n * (sum(q) + 1)
p, s, B = [18, 13, 11, 9, 7], [9, 7, 6, 5, 4], 16
opt = max(sum(p[i] for i in S) for r in range(6) for S in combinations(range(5), r) if sum(s[i] for i in S) <= B)
print("OPT =", opt)
for eps in [0.9, 0.5, 0.2, 1 / (len(p) * max(p) + 1)]:
val, cells = fptas(p, s, B, eps)
print(f"eps = {eps:.4f}: value {val}, table cells {cells}")OPT = 31
eps = 0.9000: value 31, table cells 85
eps = 0.5000: value 31, table cells 150
eps = 0.2000: value 31, table cells 400
eps = 0.0110: value 31, table cells 7320
On this small instance even the coarse roundings happen to find the optimum; only the last line is guaranteed to. With that the answer must be exact, at the price of a table as large as the one indexed by the original profits. For a strongly NP-hard problem such a pseudo-polynomial algorithm would be polynomial on the instances that remain hard, which is the contradiction.
APX and the limits of PTAS
The classes form a chain (each inclusion is strict unless P = NP):
APX is the class of NP optimization problems with some constant-factor approximation: vertex cover, metric TSP, MAX-3SAT, bin packing, -center. log-APX allows (set cover); poly-APX allows (maximum clique and independent set, where even is impossible unless P = NP, by Tight Thresholds: MAX-3SAT, Clique and Set Cover).
A problem is APX-hard if every APX problem reduces to it by an approximation-preserving reduction (a PTAS reduction). A consequence of the PCP theorem (The PCP Theorem) is that MAX-3SAT is APX-complete and that no APX-hard problem has a PTAS unless P = NP. Vertex cover, metric TSP, max-cut, -center and bin packing (in the absolute sense) are all APX-hard.
Bin packing shows the difference between absolute and asymptotic schemes. Deciding whether items fit into two bins is the NP-complete PARTITION problem, so no algorithm achieves an absolute ratio below unless P = NP: on a two-bin instance it would have to answer 2, not 3. Yet the large-OPT behaviour is excellent: for every there is a polynomial algorithm using at most bins (Fernandez de la Vega and Lueker, 1981), the subject of An Asymptotic PTAS for Bin Packing.
Test yourself
An algorithm runs in time and returns a -approximation. What is it?
- An FPTAS
- A PTAS but not, from this bound, an FPTAS
- An EPTAS
- Not an approximation scheme
Why does an FPTAS for a strongly NP-hard problem with polynomially bounded integer optimum imply P = NP?
- Because FPTAS algorithms are randomized
- Choosing below forces the answer to be exact, in time polynomial in the size and the largest number
- Because strongly NP-hard problems have no feasible solutions
- Because must be constant
In Graham's scheme for makespan with and , what ratio is guaranteed?
Why has bin packing no PTAS unless P = NP, although it has an asymptotic PTAS?
- It is not in NP
- Deciding whether the items fit into two bins is PARTITION, so an absolute ratio below would solve an NP-complete problem; the asymptotic scheme allows an additive constant
- Because first fit is optimal
- Because its LP relaxation is integral