Approximate Knapsack Get as much value as you can into the knapsack Approximate Knapsack Ingredients: •Instances: The volume V of the knapsack. The volume and price of n objects <<v1,p1>,<v2,p2>,… ,<vn,pn>>. •Solutions: A set of objects that fit in the knapsack. •i.e. iS vi V •Cost of Solution: The total value of objects in set. •i.e. iS pi •Goal: Get as much value as you can into the knapsack. Approximate Knapsack •Dynamic Programming Running time = ( V × n ) = ( 2#bits in V × n ) •Poly time if size of knapsack is small •Exponential time if size is an arbitrary integer. Approximate Knapsack •Dynamic Programming Running time = ( V × n ) = ( 2#bits in V × n ) •NP-Complete •Approximate Algorithm •In poly-time (n3/), solution can be found •that is perfect in iS vi V •(1+) as good as optimal wrt iS pi •Eg, = .001, Time is 1000n3 Approximate Knapsack Subinstance: V’[0..V], i[0..n], knapsack(V’,i) = maximize iS pi subject to S {1..i} and iS vi V •Recurrence Relation knapsack(V’,i) = max( knapsack(V’,i-1), knapsack(V’-vi,i-1)+pi ) No Yes Approximate Knapsack V’[0..V], i[0..n], knapsack(V’,i) OptSol price No Yes 0 1 2 V’-vi V’ V 0 1 2 same + pi i-1 i Our price? n same Take best of best. Approximate Knapsack V’[0..V], i[0..n], knapsack(V’,i) OptSol price 0 1 2 0 1 2 i-1 i Time = O(nV) n V’ V Approximate Knapsack Ingredients: (strange version) •Instances: The price P wanted from the knapsack. The volume and price of n objects <<v1,p1>,<v2,p2>,… ,<vn,pn>>. •Solutions: A set of objects with total value P. •i.e. iS pi ≥ P •Cost of Solution: The total volume of objects in set. •i.e. iS vi •Goal: Minimize the volume needed to obtain this value P. Approximate Knapsack Subinstance: P’[0..P], i[0..n], knapsack’(P’,i) = minimize iS vi subject to S {1..i} and iS pi ≥ P •Recurrence Relation knapsack’(P’,i) = min( knapsack’(P’,i-1), knapsack’(P’-pi,i-1)+vi ) No Yes Approximate Knapsack V’[0..V], i[0..n], knapsack’(P’,i) OptSol volume No Yes 0 1 2 P’-pi P’ P 0 1 2 same + pi i-1 i Our volume?n same Take best of best. Approximate Knapsack Original problem knapsack(V,n) OptSol volume 0 1 2 P’ 0 1 2 i Time = O(nP) n P = i pi Find largest price not using more than V volume P Approximate Knapsack •Dynamic Programming Running time = ( V × n ) = ( 2#bits in V × n ) •Poly time if size of knapsack is small •Exponential time if size is an arbitrary integer. •Strange Dynamic Programming Running time = ( P × n ) = ( 2#bits in P × n ) •Poly time if prices are small •Exponential time if prices are arbitrary integers. Approximate Knapsack Approximation Algorithm: •Given V, <<v1,p1>,<v2,p2>,… ,<vn,pn>>, & •Let k be some # of bits chosen later •Let p’i = pi with low k bits zeroed eg pi = 101101101011 p’i = 1011011000002 k=4 (Lost precision in prices, but that is ok) Solve knapsack using the strange algorithm. Approximate Knapsack Original problem knapsack(V,n) OptSol volume 0 1 2 0 1 2 i P = i pi P’ n Find largest price not using more than V volume P Approximate Knapsack Original problem knapsack(V,n) OptSol volume 0 1 2 P’ 0 1 2 i Time = O(n P/2k) n Only every 2k th column matters P Approximate Knapsack Approximation Algorithm: •Let Salg be the set of items selected by our alg. •Let Sopt be the set of items in the optimal sol. •Let Palg = pi be the price returned by our alg. iSalg •Let Popt = pi be the price returned by our alg. iS opt •Need Palg ≥ Popt (1-) Approximate Knapsack Palg = pi iSalg because rounded down ≥ p’i iSalg because Salg is an optimal solution ≥ p’i for the <vi,p’i> problem iSopt k. because rounded by at most 2 ≥ (pi -2k) iSopt = Popt – n 2k Approximate Knapsack Palg ≥ Popt – n2k k n2 = 1Popt Popt Popt 2k (need) =1- Popt = n Time = O(n P/2k) = O( n2 P ) Popt Popt ≥ maxi pi ≥ P = i pi Time = O( n3) P n done