Академический Документы
Профессиональный Документы
Культура Документы
Capacity constraint ix wi W
wlog assume i wi > W , i : wi < W
Maximize profit ix pi
Sanders/van Stee: Approximations- und Online-Algorithmen 2
L = {x Rn : 1 i m : xi 0 ai x ./i bi } .
Let ai j denote the j-th component of vector ai .
Sanders/van Stee: Approximations- und Online-Algorithmen 3
Complexity
maximize p x
subject to
w x W, xi {0, 1} for 1 i n.
maximize p x
subject to
w x W, 0 xi 1 for 1 i n.
Capacity constraint ix wi W
wlog assume i wi > W , i : wi < W
Maximize profit ix pi
Sanders/van Stee: Approximations- und Online-Algorithmen 8
opt xi pi 2opt
i
10
B 15 W
Proof. We have k 20
iB pi opt. Furthermore, since wk < W ,
pk opt.
S 20
We get
Two-approximation of Knapsack
10
B
15
1 if i B
W
W iB wi k
xi = if i = k 20
wk
0 if i S
S 20
Exercise: Prove that either B or {k} is a
2-approximation of the (nonrelaxed) knapsack problem.
Sanders/van Stee: Approximations- und Online-Algorithmen 17
Dynamic Programming
Building it Piece By Piece
Principle of Optimality
An optimal solution can be viewed as constructed of
optimal solutions for subproblems
Solutions with the same objective values are
interchangeable
Define
P(i,C) = optimal profit from items 1,. . . ,i using capacity C.
Lemma 4.
Lemma 4.
1 i n : P(i,C) = max(P(i 1,C), P(i 1,C wi ) + pi )
Proof
P(i,C) P(i 1,C): Set xi = 0, use optimal subsolution.
Sanders/van Stee: Approximations- und Online-Algorithmen 20
Lemma 4.
1 i n : P(i,C) = max(P(i 1,C), P(i 1,C wi ) + pi )
Proof
P(i,C) P(i 1,C): Set xi = 0, use optimal subsolution.
Lemma 4.
1 i n : P(i,C) = max(P(i 1,C), P(i 1,C wi ) + pi )
Proof
P(i,C) max(P(i 1,C), P(i 1,C wi ) + pi ).
Assume the contrary :
x that is optimal for the subproblem such that
Lemma 4.
1 i n : P(i,C) = max(P(i 1,C), P(i 1,C wi ) + pi )
Proof
P(i,C) max(P(i 1,C), P(i 1,C wi ) + pi )
Assume the contrary :
x that is optimal for the subproblem such that
Lemma 4.
1 i n : P(i,C) = max(P(i 1,C), P(i 1,C wi ) + pi )
Proof
P(i,C) max(P(i 1,C), P(i 1,C wi ) + pi )
Assume the contrary :
x that is optimal for the subproblem such that
Procedure knapsack(p, c, n, W )
array P[0 . . .W ] = [0, . . . , 0]
bitarray decision[1 . . . n, 0 . . .W ] = [(0, . . . , 0), . . . , (0, . . . , 0)]
for i := 1 to n do
// invariant: C {1, . . . ,W } : P[C] = P(i 1,C)
for C := W downto wi do
if P[C wi ] + pi > P[C] then
P[C] := P[C wi ] + pi
decision[i,C] := 1
Sanders/van Stee: Approximations- und Online-Algorithmen 25
Recovering a Solution
C := W
array x[1 . . . n]
for i := n downto 1 do
x[i] := decision[i,C]
if x[i] = 1 then C := C wi
endfor
return x
Analysis:
i \C 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0, (0) 10, (1) 10, (1) 10, (1) 10, (1) 10, (1)
2
3
4
Sanders/van Stee: Approximations- und Online-Algorithmen 27
i \C 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0, (0) 10, (1) 10, (1) 10, (1) 10, (1) 10, (1)
2 0, (0) 10, (0) 10, (0) 20, (1) 30, (1) 30, (1)
3
4
Sanders/van Stee: Approximations- und Online-Algorithmen 28
i \C 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0, (0) 10, (1) 10, (1) 10, (1) 10, (1) 10, (1)
2 0, (0) 10, (0) 10, (0) 20, (1) 30, (1) 30, (1)
3 0, (0) 10, (0) 15, (1) 25, (1) 30, (0) 35, (1)
4
Sanders/van Stee: Approximations- und Online-Algorithmen 29
i \C 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0, (0) 10, (1) 10, (1) 10, (1) 10, (1) 10, (1)
2 0, (0) 10, (0) 10, (0) 20, (1) 30, (1) 30, (1)
3 0, (0) 10, (0) 15, (1) 25, (1) 30, (0) 35, (1)
4 0, (0) 10, (0) 15, (0) 25, (0) 30, (0) 35, (0)
Sanders/van Stee: Approximations- und Online-Algorithmen 30
Define
C(i, P) = smallest capacity from items 1,. . . ,i giving profit P.
Lemma 5.
A Faster Algorithm
Algorithm A is a
(Fully) Polynomial Time Approximation Scheme
minimization
for problem if:
maximization
Example Bounds
PTAS FPTAS
1
n + 21/ 2
n +
log 1 1
n n+ 4
1
n n/
42/3 ..
n .
21000/ ..
n+2 .
.. ..
. .
Sanders/van Stee: Approximations- und Online-Algorithmen 35
Lemma 6. p x0 (1 )opt.
Lemma 6. p x0 (1 )opt.
Lemma 6. p x0 (1 )opt.
Simplifying assumptions:
1/ N: Otherwise := 1/ d1/e.
Proof.
values: M = 1/2
M M
M 1 log(1/)
pieces: M M ln M = O 2
i=1/
j i=1/
j
time dynamic programming: O log(1/)
4
Sanders/van Stee: Approximations- und Online-Algorithmen 44
The best work in near linear time for almost all inputs! Both in a
probabilistic and in a practical sense.
[Beier, Vcking, An Experimental Study of Random Knapsack
Problems, European Symposium on Algorithms, 2004.]
[Kellerer, Pferschy, Pisinger, Knapsack Problems, Springer
2004.]
Main additional tricks:
p x p x0 c x c x0
6 x0 : x0 dominates x
In General
d objectives
various problems
Example
Items: (1, 10), (3, 20), (2, 15), (4, 20), prune at W = 5
L =<(0,0)>
(1,10)->L= <(1,10)>
merge ->L =<(0,0),(1,10)>
(3,20)->L= <(3,20),(4,30)>
merge ->L =<(0, 0),(1,10),(3,20),(4,30)>
(2,15)->L= <(2,15),(3,25),(5,35)>
merge ->L=<(0, 0),(1,10),(2,15),(3,25),(4,30),(5,35)>
(4,20)->L= <(4,20),(5,30)>
merge ->L=<(0, 0),(1,10),(2,15),(3,25),(4,30),(5,35)>
Sanders/van Stee: Approximations- und Online-Algorithmen 51
Horowitz-Sahni Decomposition
|LA | 2n/2