MCS 312: NP Completeness and Approximation algorthms Instructor Neelima Gupta ngupta@cs.du.ac.in
Table of Contents FPTAS for the Knapsack Problem Reference: Vazirani.
Definition Given a set {a1, a2 . . . an} of n objects with profit p(ai) and size s(ai). And, a knapsack with capacity B. Find a collection of objects fitting into the knapsack that maximizes the profit.
Pseudo Polynomial Algorithms
Pseudo Polynomial for KS Let P = max { p(ai) } Let S(i, p) denotes a subset of {a1, a2 . . . ai} with profit exactly equal to p that minimizes the size. Let A(i,p) denotes the size of S(i, p). A(i,p) = infinity if no such set exists. A(1,p) is easy to compute (in fact known directly) for every p in {1 … nP} A(i+1,p) = min{ A(i, p), size (ai+1, p – p(ai+1) } if p(ai) < p = A(i, p) otherwise
Example n = 4 B = 5 Elements (size, profit): (2,3), (3,4), (4,5), (5,6) Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
(2,3), (3,4), (4,5), (5,6) W 0 1 2 3 4 5 i 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w<w1 ; m[1,1] = m[1-1,1] = m[0,1] (2,3), (3,4), (4,5), (5,6) As w<w1 ; m[1,1] = m[1-1,1] = m[0,1] W 0 1 2 3 4 5 i 1 2 3 4 inf 2 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w<w2 ; m[2,1] = m[2-1,1] = m[1,1] (2,3), (3,4), (4,5), (5,6) As w<w2 ; m[2,1] = m[2-1,1] = m[1,1] W 0 1 2 3 4 5 i 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w<w3 ; m[3,1] = m[3-1,1] = m[2,1] (2,3), (3,4), (4,5), (5,6) As w<w3 ; m[3,1] = m[3-1,1] = m[2,1] W 0 1 2 3 4 5 i 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w<w4 ; m[4,1] = m[4-1,1] = m[3,1] (2,3), (3,4), (4,5), (5,6) As w<w4 ; m[4,1] = m[4-1,1] = m[3,1] W 0 1 2 3 4 5 i 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w>=w1 ; m[1,2] = max{m[1-1,2] , m[1-1,2-2]+3} =max{ 0,0+3} (2,3), (3,4), (4,5), (5,6) As w>=w1 ; m[1,2] = max{m[1-1,2] , m[1-1,2-2]+3} =max{ 0,0+3} W 0 1 2 3 4 5 i 1 2 3 4 3 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w<w2 ; m[2,2] = m[2-1,2] = m[1,2] (2,3), (3,4), (4,5), (5,6) As w<w2 ; m[2,2] = m[2-1,2] = m[1,2] W 0 1 2 3 4 5 i 1 2 3 4 3 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w<w3 ; m[3,2] = m[3-1,2] = m[2,2] (2,3), (3,4), (4,5), (5,6) As w<w3 ; m[3,2] = m[3-1,2] = m[2,2] W 0 1 2 3 4 5 i 1 2 3 4 3 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w<w4 ; m[4,2] = m[4-1,2] = m[3,2] (2,3), (3,4), (4,5), (5,6) As w<w4 ; m[4,2] = m[4-1,2] = m[3,2] W 0 1 2 3 4 5 i 1 2 3 4 3 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w>=w1 ; m[1,3] = max{m[1-1,3] , m[1-1,3-2]+3} =max{ 0,0+3} (2,3), (3,4), (4,5), (5,6) As w>=w1 ; m[1,3] = max{m[1-1,3] , m[1-1,3-2]+3} =max{ 0,0+3} W 0 1 2 3 4 5 i 1 2 3 4 3 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w>=w2 ; m[2,3] = max{m[2-1,3] , m[2-1,3-3]+4} =max{ 3,0+4} (2,3), (3,4), (4,5), (5,6) As w>=w2 ; m[2,3] = max{m[2-1,3] , m[2-1,3-3]+4} =max{ 3,0+4} W 0 1 2 3 4 5 i 1 2 3 4 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w<w3 ; m[3,3] = m[3-1,3] = m[2,3] (2,3), (3,4), (4,5), (5,6) As w<w3 ; m[3,3] = m[3-1,3] = m[2,3] W 0 1 2 3 4 5 i 1 2 3 4 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w<w4 ; m[4,3] = m[4-1,3] = m[3,3] (2,3), (3,4), (4,5), (5,6) As w<w4 ; m[4,3] = m[4-1,3] = m[3,3] W 0 1 2 3 4 5 i 1 2 3 4 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w>=w1 ; m[1,4] = max{m[1-1,4] , m[1-1,4-2]+3} =max{ 0,0+3} (2,3), (3,4), (4,5), (5,6) As w>=w1 ; m[1,4] = max{m[1-1,4] , m[1-1,4-2]+3} =max{ 0,0+3} W 0 1 2 3 4 5 i 1 2 3 4 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w>=w2 ; m[2,4] = max{m[2-1,4] , m[2-1,4-3]+4} =max{ 3,0+4} (2,3), (3,4), (4,5), (5,6) As w>=w2 ; m[2,4] = max{m[2-1,4] , m[2-1,4-3]+4} =max{ 3,0+4} W 0 1 2 3 4 5 i 1 2 3 4 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w>=w3 ; m[3,4] = max{m[3-1,4] , m[3-1,4-4]+5} =max{ 4,0+5} (2,3), (3,4), (4,5), (5,6) As w>=w3 ; m[3,4] = max{m[3-1,4] , m[3-1,4-4]+5} =max{ 4,0+5} W 0 1 2 3 4 5 i 1 2 3 4 3 4 5 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w<w4 ; m[4,4] = m[4-1,4] = m[3,4] (2,3), (3,4), (4,5), (5,6) As w<w4 ; m[4,4] = m[4-1,4] = m[3,4] W 0 1 2 3 4 5 i 1 2 3 4 3 4 5 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w>=w1 ; m[1,5] = max{m[1-1,5] , m[1-1,5-2]+3} =max{ 0,0+3} (2,3), (3,4), (4,5), (5,6) As w>=w1 ; m[1,5] = max{m[1-1,5] , m[1-1,5-2]+3} =max{ 0,0+3} W 0 1 2 3 4 5 i 1 2 3 4 3 4 5 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w>=w2 ; m[2,5] = max{m[2-1,5] , m[2-1,5-3]+4} =max{ 3,3+4} (2,3), (3,4), (4,5), (5,6) As w>=w2 ; m[2,5] = max{m[2-1,5] , m[2-1,5-3]+4} =max{ 3,3+4} W 0 1 2 3 4 5 i 1 2 3 4 3 4 7 5 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w>=w3 ; m[3,5] = max{m[3-1,5] , m[3-1,5-4]+5} =max{ 7,0+5} (2,3), (3,4), (4,5), (5,6) As w>=w3 ; m[3,5] = max{m[3-1,5] , m[3-1,5-4]+5} =max{ 7,0+5} W 0 1 2 3 4 5 i 1 2 3 4 3 4 7 5 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
As w>=w4 ; m[4,5] = max{m[4-1,5] , m[4-1,5-5]+6} =max{ 7,0+6} (2,3), (3,4), (4,5), (5,6) As w>=w4 ; m[4,5] = max{m[4-1,5] , m[4-1,5-5]+6} =max{ 7,0+6} W 0 1 2 3 4 5 i 1 2 3 4 3 4 7 5 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
(2,3), (3,4), (4,5), (5,6) W 0 1 2 3 4 5 i 1 2 3 4 3 4 7 5 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)
FPTAS Given \epsilon, let k = \epsilon P /n Let p’(a) = floor (p(a)/k) Find an optimal solution, say S’, using DP with these profits. Output S’ Claim: p(S’) >= (1-\epsilon) OPT
kp’(a) <= p(a) p’(a) = floor (p(a)/k) >= p(a)/k - 1. Thus, kp’(a) >= p(a) - k i.e. p(a) - kp’(a) <= k i.e. k p’(a) >= p(a) - k Let O be an optimal solution. The O is a solution for the problem with the new profits as well. Thus, p’(S’) >= p’(O) Thus, p(S) >= k p’(S’) >= kp’(O) >= p(O) – k|O| >= p(O) – kn = OPT - \epsilon P >= (1 - \epsilon) OPT (as OPT <= P)
Running Time O(n^2 P’) = O(n^2 floor(P/k)) = O(n^2 floor(n/\epsilon)) Which is polynomial in n and \epsilon.
Thank You! Consolidated by: Sumedha Upadhyaya( 42), Prachi Nagpal(41) (MCS '09)