Introduction

  • Dealing with HARD problems
  • Getting around NP-completeness
    • If NN is small, even O(2N)O(2^N) is acceptable
    • Solve some important special cases in polynomial time
    • Find near-optimal solutions in polynomial time — approximation algorithm

Note: greedy 等 heuristics 不管效果,approximation 有近似解的质量方面的讨论

Approximation Ratio

[Definition] An algorithm has an approximation ratio of ρ(n)\rho(n) if, for any input of size nn, the cost CC of the solution produced by the algorithm is within a factor of ρ(n)\rho(n) of the cost CC^* of an optimal solution:

max(CC,CC)ρ(n)\max\left(\frac{C}{C^*},\frac{C^*}{C}\right)\leq\rho(n)

  • If an algorithm achieves an approximation ratio of ρ(n)\rho(n), we call it a ρ(n)\rho(n)-approximation algorithm.

[Definition] An approximation scheme for an optimization problem is an approximation algorithm that takes as input not only an instance of the problem, but also a value ϵ>0\epsilon > 0 such that for any fixed ϵ\epsilon, the scheme is a (1+ϵ)(1+ \epsilon)-approximation algorithm.

  • We say that an approximation scheme is a polynomial-time approximation scheme(PTAS) if for any fixed ϵ>0\epsilon > 0, the scheme runs in time polynomial in the size nn of its input instance.

  • O((1/ϵ)2n3)O((1/\epsilon)^2n^3) is a fully polynomial-time approximation scheme(FPTAS) 完全多项式时间近似模式

    Note: fully 的意思是关于(1/ϵ)(1/\epsilon)nn都是多项式级的

Approximate Bin Packing

  • Given NN items of sizes S1,S2,,SNS_1 , S_2 , \cdots, S_N , such that 0<Si10 < S_i \leq 1 for all 1iN1 \leq i \leq N. Pack these items in the fewest number of bins, each of which has unit capacity.
image-20210524100629865
  • NP-Hard
  • Decision problem: given KK bins, can we pack NN items? — NPC

Next Fit

1
2
3
4
5
6
7
8
9
10
11
void NextFit ( )
{
read item1;
while ( read item2 ) {
if ( item2 can be packed in the same bin as item1 )
place item2 in the bin;
else
create a new bin for item2;
item1 = item2;
} /* end-while */
}

[Theorem] Let MM be the optimal number of bins required to pack a list II of items. Then next fit never uses more than 2M12M – 1 bins. There exist sequences such that next fit uses 2M12M – 1 bins.

A simple proof for Next Fit

  • If Next Fit generates 2M2M(or 2M+12M+1) bins, then the optimal solution must generate at least M+1M+1 bins.

  • Let S(Bi)S ( B_i ) be the size of the iith bin. Then we must have:

    S(B1)+S(B2)>1S(B3)+S(B4)>1S(B2M1)+S(B2M)>1i=12MS(Bi)>MS(B_1)+S(B_2)>1\\ S(B_3)+S(B_4)>1\\ \cdots\\ S(B_{2M-1})+S(B_{2M})>1\\ \rarr\sum_{i=1}^{2M}S(B_i)>M

  • The optimal solution needs at least total size of all the items / 1\lceil\text{total size of all the items / 1}\rceil bins

    total size of all the items / 1=i=12MS(Bi)M+1\lceil\text{total size of all the items / 1}\rceil=\lceil\sum_{i=1}^{2M}S(B_i)\rceil\geq M+1

First Fit

1
2
3
4
5
6
7
8
9
void FirstFit ( )
{
while ( read item )
{
scan for the first bin that is large enough for item; /*Can be implemented in O(NlogN)*/
if ( found ) place item in that bin;
else create a new bin for item;
} /* end-while */
}

[Theorem] Let MM be the optimal number of bins required to pack a list I of items. Then first fit never uses more than 17M/1017M / 10 bins. There exist sequences such that first fit uses 17(M1)/1017(M – 1) / 10 bins.

Best Fit

  • Place a new item in the tightest spot among all bins.

  • T=O(NlogN)T = O( N \log N ) and bin number 1.7M\leq 1.7M

On-line Algorithms

  • Place an item before processing the next one, and can NOT change decision.

  • Never know when the input might end.

  • No on-line algorithm can always give an optimal solution.

[Theorem] There are inputs that force any on-line bin-packing algorithm to use at least 5/3 the optimal number of bins.

Off-line Algorithms

  • View the entire item list before producing an answer.
  • Trouble-maker: The large items
  • Solution: Sort the items into non-increasing sequence of sizes. Then apply first (or best) fit — first(or best) fit decreasing.

[Theorem] Let MM be the optimal number of bins required to pack a list II of items. Then first fit decreasing never uses more than 11M/9+6/911M / 9 + 6/9 bins. There exist sequences such that first fit decreasing uses 11M/9+6/911M / 9 + 6/9 bins.

  • Simple greedy heuristics can give good results

The Knapsack Problem

Fractional version

  • A knapsack with a capacity MM is to be packed. Given NN items. Each item ii has a weight wiw_i and a profit pip_i. If xix_i is the percentage of the item ii being packed, then the packed profit will be pixip_i x_i.

  • An optimal packing is a feasible one with maximum profit. That is, we are supposed to find the values of xix_i such that i=1npixi\sum_{i=1}^np_ix_i obtains its maximum under the constrains

    i=1nwixiMandxi[0,1]for1in\sum_{i=1}^n w_ix_i\leq M\,\text{and}\,x_i\in[0,1]\,\text{for}\,1\leq i\leq n

  • Solution:

    • We must pack one item into the knapsack in each stage
    • We shall be greedy on the criterion that choose maximum profit density pi/wip_i/w_i for each stage.

0-1 version

  • xix_i is either 0 or 1

  • NP-Hard

A Greedy Solution

  • greedy on taking the maximum profit or profit density

  • The approximation ratio is 2

    image-20210525194002966

A Dynamic Programming Solution

  • Wi,p=W_{i,p} = the minimum weight of a collection from {1,,i}\{1, \cdots, i\} with total profit being exactly pp

    1. take ii: Wi,p=wi+Wi1,ppiW_{i,p}=w_i+W_{i-1,p-p_i}
    2. skip ii: Wi,p=Wi1,pW_{i,p}=W_{i-1,p}
    3. impossible to get pp: Wi,p=W_{i,p}=\infin

    Wi,p={,i=0Wi1,p,pi>pmin{Wi1,p,wi+Wi1,ppi},otherwisei=1,,np=1,,npmaxO(n2pmax)W_{i,p}= \begin{cases} \infin,&i=0\\ W_{i-1,p},&p_i>p\\ \min\{W_{i-1,p},w_i+W_{i-1,p-p_i}\},&\text{otherwise} \end{cases}\\ i=1,\cdots,n\\ p=1,\cdots,np_{max}\\ \rarr O(n^2p_{max})

Note: input size包括pmaxp_{max}的二进制编码长度dd,所以pmax=O(2d)p_{max}=O(2^d)是指数级的复杂度

  • If pmaxp_{max} is very large, we can round all profit values up to lie in smaller range
  • (1+ϵ)PalgP(1+\epsilon)P_{alg}\leq P for any feasible solution PP, ϵ\epsilon is a precision parameter

KK-center Problem

image-20210525194524114
  • Input: Set of nn sites s1,,sns_1, \cdots, s_n

  • Center selection problem: Select KK centers CC so that the maximum distance from a site to the nearest center is minimized.

  • distance

    • Identity: dist(x,x)=0dist(x,x)=0
    • Symmetry: dist(x,y)=dist(y,x)dist(x,y)=dist(y,x)
    • Triangle inequality: dist(x,y)dist(x,z)+dist(z,y)dist(x,y)\leq dist(x,z)+dist(z,y)
  • dist(si,C)=mincCdist(si,c)=dist(s_i,C)=\min_{c\in C} {dist(s_i,c)}= distance from sis_i to the closest center

  • r(C)=maxidist(si,C)=r(C)=\max_idist(s_i,C)= smallest covering radius

  • Task: Find a set of centers CC that minimizes r(C)r(C), subject to C=K|C| = K.

  • Number of candidate centers ==\infin

A Greedy Solution

  • Put the first center at the best possible location for a single center, and then keep adding centers so as to reduce the covering radius each time by as much as possible.
1
2
3
4
5
6
7
8
9
10
11
12
Centers  Greedy-2r ( Sites S[ ], int n, int K, double r )
{
Sites S’[ ] = S[ ]; /* S’ is the set of the remaining sites */
Centers C[ ] = empty;
while ( S’[ ] != empty )
{
Select any s from S’ and add it to C;
Delete all s’ from S’ that are at dist(s’, s) <= 2r;
} /* end-while */
if ( |C| <= K ) return C;
else ERROR(No set of K centers with covering radius at most r);
}

[Theorem] Suppose the algorithm selects more than K centers. Then for any set C* of size at most K, the covering radius is r(C*) > r.