Algorithm 3.1 correctly computes the optimal value of the knapsack problem.
Algorithm 3.1 的时间复杂度为 O(nmin(B,V)), 其中 V=∑i∈Ivi. 若所有 input 都用 binary 编码,此时 input B 的 size 为 log2B, O(nB)=O(n2log2B) 为 input number B 的 exponential, 因此并不是 polynomial in input size.
Definition 3.2
An algorithm for a problem Π is said to be pseudopolynomial if its running time is polynomial in the size of of the input when the numneric part of the input is encoded in unary.
若 V 是 n 的 polynomial, 那么 O(nmin(B,V)) 就会是 n 的 polynomial. 我们可以通过将 vi round 到 n 的 polynomial 来得到一个真正的 polynomial-time (但是 approximation) algorithm, 合适的 rounding 技巧可以使结果的误差较小。
Definition 3.3
A polynomial-time approximation scheme (PTAS) is a family of algorithms {Aϵ}, where there is an algorithm for each ϵ>0, such that Aϵ is a (1+ϵ)-approximation algorithm (for minimization problems) or a (1−ϵ)-approximation algorithm (for maximization problems).
A fully polynomial-time approximation scheme (FPAS, FPTAS) is an approximation scheme such that the running time of Aϵ is bounded by a polynomial in 1/ϵ.
下面给出一个 knapsack problem 的 FPAS. 考虑用一个与 n 有关 (polynomial in n) 的数 μ 来估计 vi, 将 vi round 到 vi′=⌊vi/μ⌋, 然后跑 Algorithm 3.1 得到 item 集合 S′, 最终算法得到的解即为 ∑i∈S′vi.
不难想到将 job 分为 short job 和 long job, 然后对数量被限制到常数级别的 long job 求出 optimal schedule, 再将 short job 按照 list scheduling 加入当前 schedule 中。
具体来说,将 pi≥km1∑i=1npi2 的 job 视为 long job, 则 long job 至多有 km 个,其中 k 为常数,若假定 m 也是常数,那么 long job 的数量也是常数级别,此时可以直接暴力求出 optimal schedule for long jobs, 总的状态数为 O(mkm) 为常数,然后对 short jobs 从大到小排序后按照 list scheduling 加入 schedule 得到最终的 schedule, 最后的时间复杂度为 O(nlogn+mkm)=O(nlogn), 瓶颈在于排序,显然属于 polynomial-time algorithm. 记此类算法为 {Ak}.
The family of algorithms {Ak} is a polynomial-time approximation scheme for the problem of minimizing the makespan on any constant number of identical parallel machines.
如果只在最晚完成的 job 是 long job 时才对 long job 计算 optimal schedule, 否则沿用 knapsack 的 DP 方法计算一个非最优的 schedule, 这样可以移除 m 是常数的限制。
由于 Cmax∗ 未知,不妨设 target length 为 T, 令 pj≥T/k 的 job 为 long job, 若设置的 T≥Cmax∗, 此时 long job 的数量相对少,可以利用 DP 在 poly-time 得到一个关于 T 的近似解,然后便可调小 T 值;若设置的 T<Cmax∗, 此时 long job 的数量较多,poly-time 内难以找到与 T 相关的近似解,因此可以 return false 并调大 T 值,最终会得到一个 ≤Cmax∗ 的 T 值并由此计算出一个近似解,最后再利用 list scheduling 加入 short job 即可。
记上述算法为 {Bk}, 具体过程如下:将 long job 的 length round 到最近的 T/k2 的倍数 i⋅T/k2, 之后只需要关心 i 而不是 pj. 在 target length 为 T 的设定下,每个 machine 会被分配的 job 至多 T/kT=k 个,并且最大的 job 满足 pj≤T (否则必然有 T<Cmax∗), 因此按照 i 分类的话 long job 至多可以分为 T/k2T=k2 类,于是每个 machine 的状态可以用一个 k2 维的 vector (s1,s2,…,sk2) 表示,当满足下列不等式时称它为 machine configuration:
一个更 general 的结论是:对于任意优化问题,若该问题是 strongly NP-complete 的,那么它不存在 FPAS.
The bin-packing problem
bin-packing problem 指的是下列问题:
给定 n 个大小分别为 a1,a2,…,an 的 pieces (或 items), 满足
1>a1≥a2≥⋯≥an>0;
我们希望将这些 pieces 装进尽可能少的 bins, 其中每个 bin 的容量为 1.
bin-packing problem 的特殊情形下可以转化为 partition problem: 给定 n 个正整数 b1,…,bn 满足 B=∑i=1nbi 为偶数,问能否将下标集合 {1,…,n} 划分为集合 S 和 T 使得 ∑i∈Sbi=∑i∈Tbi.
只需令 ai=2bi/B 并检查能否装进 2 个 bin 即可将 bin-packing problem 转化为 partition problem. 由于 partition problem 是 NP-complete 的,于是有下列 theorem.
Theorem 3.8
Unless P=NP, there cannot exist a ρ-approximation algorithm for the bin-packing problem for any ρ<3/2.
记 FFD(I) 为 First-Fit-Descreasing 算法运行在 instance I 时得到的需要的 bin 的数量,OPT(I) 表示 I 最少需要的 bin 数量,有证明表示,对于任意 instance I 都有 FFD(I)≤(11/9)OPT(I)+4.
事实上,如果允许存在 small additive terms, 我们可以找到算法使得它得到的 packing 不会使用超过 OPT(I)+1 个 bin.
尽管有 Theorem 3.8 的结论,bin-packing problem 在允许 small additive terms 时却可以做到优化乘法系数是因为它没有 rescaling property, 即对于任意 input I 和任意值 K, 可以构造一个等价的 instance I′ 使得 objective function value of any feasible solution is rescaled by K.
前面讨论的所有 weighted problem 都拥有 rescaling property, ρOPT+c 的 solution 都可以等价转化得到一个 ρ-approximation 算法,例如 Section 3.2 中的 scheduling problem 若将所有 pj 乘上系数 K 就能利用结果是整数的特性摆脱 additive terms. 因此只需考虑 no additive terms 的情形即可。
下面设计一系列 poly-time approx algorithms parameterized by ϵ>0, 其中每个算法都在任意 input I 下保证得到的 packing 所需 bin 数量不超过 (1+ϵ)OPT(I)+1. 这样的算法不满足 PTAS 的定义,这启发了下列定义。
Definition 3.9
An asymptotic polynomial-time approximation scheme (APTAS) is a family of algorithms Aϵ along with a constant c where there is an algorithm Aϵ for each ϵ>0 such that Aϵ returns a solution of value at most (1+ϵ)OPT+c for minimization problems.
bin-packing problem 与前面 scheduling problem 中关于 T 的 decision problem 部分十分相似,不难想到用相同的 DP 来解决 bin-packing problem. 这要求:
Any packing of all pieces of size greater than γ into ℓ bins can be extended to a packing for the entire input with at most max{ℓ,1−γ1SIZE(I)+1} bins.
Lemma 3.10 的证明依赖于 First-Fit algorithm,记 First-Fit 算法的结果为 FF(I), 这里不妨先证明 FF(I)≤2OPT(I)+1. 假设已经使用了 ℓ 个 bin, 当前 piece 被放入第 ℓ+1 个 bin 当且仅当前 ℓ 个 bin 都放不下该 piece, 对于单个 bin 的情况不方便分析,但是两个 bin 结合起来就可以发现它们满足一些有用的 property:将 bin 1 和 bin 2 结合,bin 3 和 bin 4 结合,如此下去,当且仅当前 2k−1 个 bin 无法放入当前 piece 时会开启第 2k 个 bin, 因此这之后第 2k−1 个 bin 与第 2k 个 bin 所使用的容量之和必然 >1, 从而若一共使用了 ℓ 个 bin, 那么有 SIZE(I)≥⌊ℓ/2⌋, 于是 FF(I)≤2OPT(I)+1.
Proof of Lemma 3.10. 考虑 small pieces 按照 First-Fit 放入。若所有 small pieces 都被放入了原有的 ℓ bins 中,那么答案显然是 ℓ. 若当前的 small piece 被放入了一个新的 bin, 不妨设为第 k+1 个 bin, 那么前 k 个 bin 的剩余容量都 ≤1−γ, 这样的 bin 至多有 1−γ1SIZE(I) 个,于是答案至多为 1−γ1SIZE(I)+1, 得证。
Let I′ be the input obtained from an input I by applying linear grouping with group size k. Then
OPT(I′)≤OPT(I)≤OPT(I′)+k,
and furthermore, any packing of I′ can be used to generate a packing of I with at most k additional bins.
Proof idea. 从下图看很显然
最后分析算法性能来设定 k. I′ 包含不同 size 的数量至多为 n/k, 其中 n 是 I 中 piece 的数量,而 I 中去除了 small pieces, 从而 SIZE(I)≥ϵn/2. 令 k=⌊ϵSIZE(I)⌋, 于是 n/k≤2n/(ϵSIZE(I))≤4/ϵ2, 当 ϵSIZE(I)<1 时 large pieces 足够少可以直接 DP, 因此这里假定了 ϵSIZE(I)≥1. 再结合 Lemma 3.10, 最终的 bin 数量不会超过 (1+ϵ)OPT(I)+1.
Theorem 3.12
For any ϵ>0, there is a polynomial-time algorithm for the bin-packing problem that computes a solution with at most (1+ϵ)OPT(I)+1 bins; that is, there is an APTAS for the bin-packing problem.