















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















Instance : A set of items I = {i 1 ,... in} where each item ij has value vj and weight wj (values and weights are positive integers); and a target value t and a weight limit l. Question: Is there a subset T of I with total value at least t and total weight at most l.
Theorem KNAPSACK is NP -complete.
Step 1. The problem is in NP : a subset T is the certificate. Step 2. To show completeness, reduce SUBSET SUM to KNAPSACK. We will use the method of “restriction”.
KNAPSACK can be solved in O(nl) time using dynamic programming
Create a (l + 1 ) × (n + 1 ) matrix M Set M(w, 0 ) = M( 0 , i) to 0 for all w ∈ { 1 , 2 ,... , l} and all i ∈ { 1 , 2 ,... , n} For i = 0 , 1 ,... , n − 1, set entry M(w, i + 1 ) as follows
M(w, i + 1 ) = max{M(w, i), M(w − wi+ 1 , i) + vi+ 1 }
(M(w, i) is the largest total value obtainable by selecting from the first i items with weight limit w) Answer yes if any entry ≥ t, otherwise no
Let n = 4; values 1, 3 , 4 , 2; weights 1, 1 , 3 , 2; target t = 7 and limit l = 5.
weight max total value from first i items limit w i = 0 i = 1 i = 2 i = 3 i = 4 0 0 1 0 3 2 3 4 4 5 X
Let n = 4; values 1, 3 , 4 , 2; weights 1, 1 , 3 , 2; target t = 7 and limit l = 5.
weight max total value from first i items limit w i = 0 i = 1 i = 2 i = 3 i = 4 0 0 0 0 0 0 1 0 X 3 2 0 3 0 4 0 4 5 0
Let n = 4; values 1, 3 , 4 , 2; weights 1, 1 , 3 , 2; target t = 7 and limit l = 5.
weight max total value from first i items limit w i = 0 i = 1 i = 2 i = 3 i = 4 0 0 0 0 0 0 1 0 X 3 2 0 3 0 4 0 4 5 0
Let n = 4; values 1, 3 , 4 , 2; weights 1, 1 , 3 , 2; target t = 7 and limit l = 5.
weight max total value from first i items limit w i = 0 i = 1 i = 2 i = 3 i = 4 0 0 0 0 0 0 1 0 1 3 2 0 3 0 4 0 4 5 0
Let n = 4; values 1, 3 , 4 , 2; weights 1, 1 , 3 , 2; target t = 7 and limit l = 5.
weight max total value from first i items limit w i = 0 i = 1 i = 2 i = 3 i = 4 0 0 0 0 0 0 1 0 1 3 3 3 2 0 1 4 4 4 3 0 1 4 4 5 4 0 1 4 X 5 0 1 4
Let n = 4; values 1, 3 , 4 , 2; weights 1, 1 , 3 , 2; target t = 7 and limit l = 5.
weight max total value from first i items limit w i = 0 i = 1 i = 2 i = 3 i = 4 0 0 0 0 0 0 1 0 1 3 3 3 2 0 1 4 4 4 3 0 1 4 4 5 4 0 1 4 X 5 0 1 4
Let n = 4; values 1, 3 , 4 , 2; weights 1, 1 , 3 , 2; target t = 7 and limit l = 5.
weight max total value from first i items limit w i = 0 i = 1 i = 2 i = 3 i = 4 0 0 0 0 0 0 1 0 1 3 3 3 2 0 1 4 4 4 3 0 1 4 4 5 4 0 1 4 7 5 0 1 4
This algorithm is not sufficient to show that KNAPSACK is in P!
The length of the input to KNAPSACK is in O(n log l) - so nl is not bounded by a polynomial function of the input length. (This is a typical source of confusion in amateur “proofs” that P = NP : numbers must be encoded in binary, not in unary!).
However, if we restrict KNAPSACK to instances such that l ≤ p(n) for some polynomial p, then we obtain a problem in P.
An algorithm which is polynomial in the size of the numbers in the input is called pseudo-polynomial NP-complete problems which remain NP-complete when all numbers in the input are bounded by some polynomial in the length of the input are called strongly NP-complete
Any NP -complete problem without numerical data is strongly NP -complete: e.g., SATISFIABILITY, HAMILTONIAN CIRCUIT, 3-COLOURABILITY. TSP (D) is strongly NP -complete, since it is NP -complete even if all distances are 1 and 2 (see reduction from HAM CIRCUIT). SUBSET SUM is not strongly NP -complete, it is a subproblem of KNAPSACK.
Instance : A graph G Question: Is it true that G has no Hamiltonian circuit?
Instance : A CNF-formula f Question: Is it true that f is a tautology, that is, it is satisfied by all assignments?
(^1) Sometimes called VALIDITY
Definition A language L′^ ∈ coNP is said to be coNP -complete if, for any language L ∈ NP , we have L ≤ L′. Theorem A language L′^ is NP -complete if and only if L′^ is coNP -complete.
Proof: We will show one direction, the other is similar. Let L′^ be NP -complete and choose any L ∈ coNP. Then L is in NP. So there is a polynomial-time reduction R from L to L′. Then R is also a reduction from L to L′^ : x ∈ L ⇔ x 6 ∈ L ⇔ R(x) 6 ∈ L′^ ⇔ R(x) ∈ L′. Corollary The problems NO HAM CIRCUIT and TAUTOLOGY are coNP -complete.