150 likes | 168 Views
Review of NP-completeness concepts including Class P, Class NP, reduction, and Vertex Cover. Detailed analysis of Subset Sum problem with dynamic programming algorithm. Unveiling the complexity and challenges related to NP-complete problems.
E N D
Review of NP-completeness concepts • The ClassP • A decision problem is in P if and only • if is solvable in polynomial time (i.e. • O(nc) for input size n and constant c) • The ClassNP • A decision problem is in NP if and only • if there is a nondeterministic algorithmA • for such that, for any instance x of , • x is a yes-instance if and only if some • execution of Aoutputs yes in polynomial • time • Ausually consists of a “guessing stage” • and a “checking stage” • P NP; the key question: is P = NP? • strong belief that the answer is no • No proof of P NP is in sight
Theory of NP-completeness addresses the • question: “If P NP then can we identify • problems that are in NP but not in P?” • Polynomial-time reduction • for decision problems P and P : • PP if every instance of Pcan be • transformed, in polynomial time, to an • instance of P with the same answer • we say that Pis reducible toP • if PPandP P thenP P • P is NP-complete if • P NP • PP for some known • NP-complete problem P • if P P andP NP-completethen P = NP • so, P NP andP NP-complete P P
Pseudo-Polynomial Time Algorithms • A method for coping with NP-completenumber problems • NP-complete problem - Subset Sum (SS) • Instance: a sequence x1, . . ., xn of positive • integers, and a target positive integer K • Question: does there exist a subset S of • {1, . . ., n} such that r S xr = K ? • Example instance: • x1=7, x2=4, x3=6, x4=4, x5=9, x6=11 • If K = 28, answer is • If K = 29, answer is • Clearly SS belongs to NP • Vertex Cover SS (not proved) • SS is NP-complete • yes • e.g. pick S={2, 4, 5, 6} • (x2+x4+x5+x6 = 4+4+9+11 = 28). no
A dynamic programming approach • Parameterise: given an instance of SS, • SS(i, j) is a decision problem which asks: • does there exist a subset S of • {1,…,i} such that r S xr = j ? • (0 i n and 0 j K) • Let B(i, j) be the (Boolean) answer to • the problem SS(i, j) • To solve SS for the original instance, • we need to find B(n, K) • Dynamic programming algorithm is based • on the following recurrence relation for SS: • B(i, j) = B(i-1, j) (xi j B(i-1, j - xi)) • for i>0, subject to • B(0, 0) = • B(0, j) = j > 0 true false
Example x1=7, x2=4, x3=6, x4=4, x5=9, x6=11, K=28 i0 1 2 3 4 5 6 xi0 7 4 6 4 9 11 j 0 T T T T T T 1 F F F F F F F 2 F F F F F F F 3 F F F F F F F 4 F F T T T T T 5 F F F F F F F 6 F F F T T T T 7 F T T T T T 8 F F F F T T T 9 F F F F F T T 10 F F F T T T T 11 F F T T T T 17 F F F T 28 F F F F F F T T T T T T T
Example x1=7, x2=4, x3=6, x4=4, x5=9, x6=11, K=28 i0 1 2 3 4 5 6 xi0 7 4 6 4 9 11 j 0 T T T T T T 1 F F F F F F F 2 F F F F F F F 3 F F F F F F F 4 F F T T T T T 5 F F F F F F F 6 F F F T T T T 7 F T T T T T 8 F F F F T T T 9 F F F F F T T 10 F F F T T T T 11 F F T T T T 17 F F F T 28 F F F F F F T T T T T T T
Dynamic Programming Algorithm for SS /** Input: array of ints x[0]...x[n] * N.B. x[0] unused * target positive integer k * Output: solution to SS(n,k), i.e. * Boolean value b[n][k] */ public boolean subsetSum(int k, int[] x) { int n = x.length - 1; boolean b[][] = newboolean[n+1][k+1]; b[0][0] = true; for (int j = 1; j <= k; k++) b[0][j] = false; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; k++) b[i][j]= b[i-1][j] || (x[i]<=j && b[i-1][j-x[i]]); return b[n][k]; }
Complexity • Size of dynamic programming table is • (nK) (n+1 rows and K+1 columns) • i.e. both O(nK) and (nK) • Each entry can be computed in O(1) time • So complexity of algorithm is (nK) • Why does this not prove that SS is in P? • and prove that P = NP, since SS is • NP-complete? • Answer - nK is not in general a polynomial • function of the input size • what is the input size? • log K bits to represent K • n log K bits to represent the x's • so size of the instance is (n+1) log K • K could be polynomial in n, e.g. K=n3 • Therefore input size is 3(n+1) log n • Running time of algorithm is O(n4) in • this case - polynomial in the input size
Pseudo-Polynomial Time Algorithms • But • K could be exponential in n, e.g. K=2n • therefore input size is n(n+1)=O(n2) • so running time of algorithm is • (n.2n)in this case - exponential in • the input size • So what makes SS a hard problem is the • fact that the numbers involved can be • arbitrarily large • If the numbers involved are bounded • above in some way, SS is solvable in • polynomial time • Generally, let P be a number problem and • let A be an algorithm for P. We say that A • is a pseudo-polynomial-time algorithm if, • for any instance of x of P, the time • complexity of A is O(q(|x|,max)), where |x| • isthe size of x, max is the largest number • occurring in x and q is some polynomial • function
A second example - Knapsack Problem (KP) Instance:n items, where item i has a weight wiand a profit pi. Knapsack capacity C. Output: Maximum k such that there exists a subset S of {1,…,n} for which r S wr C andr S pr = k. That is, choose a subset of the items of maximum total profit such that the knapsack capacity is not exceeded. Example:Knapsack capacity65.5items - Weights:w1=23, w2=15, w3=15, w4=33, w5=32 Profits:p1=33, p2=23, p3=11, p4=35, p5=11 Choosing all the items gives total weight 118 Choosing items 1,4 gives total weight56 Cand total profit68 Choosing items 2,3,4 gives total weight 63 C and total profit 69 (optimal)
Trying a Greedy Algorithm for KP • Calculate the profit/weight ratios of each • of the n items • Sort the items in order of these ratios • (highest first) /** Input: array of weights w[0]...w[n] * array of profits p[0]...p[n] * N.B. w[0] and p[0] unused * p[i]/w[i]≥p[i+1]/w[i+1], 1 ≤ i ≤ n-1 * knapsack capacity c * Output: a profit obtained by choosing * a collection of the items without * exceeding the knapsack capacity */ public int greedyKP(int [] w, int [] p, int c) { int weight = 0; int profit = 0; int i = 1; int n = w.length - 1; while ( weight < c && i ≤ n) { if ( w[i] + weight ≤ c ) { weight += w[i]; profit += p[i]; } i++; } return profit; }
The Greedy Algorithm does not work • Example • Knapsack capacity 50 • Greedy algorithm chooses items 1, 2 • total profit 160 • Optimal solution comprises items 2, 3 • total profit 220 • Decision version of KP is NP-complete – • reduction from SS
A dynamic programming approach • Parameterise: given an instance of KP, • KP(i, j) is a problem which asks for: • the maximum total profit over all • subsets S of {1,…,i} such that • r S wr j • (0 i n and 0 j C) • Let S(i, j) be the solution to KP(i, j) • To solve KP for the original instance, • find S(n,C) • Dynamic programming algorithm is based • on the following recurrence relation for KP: • S(i-1, j) ,if wi>j • S(i-1,j), • maxS(i-1,j-wi)+pi, otherwise • for i>0, subject to S(0, j) = 0 (0 j C) • Implies an O(nC) algorithm S(i, j) =
Dynamic Programming Algorithm for KP /** Input: array of weights w[0]...w[n] * array of profits p[0]...p[n] * N.B. w[0] and p[0] unused * knapsack capacity c * Output: solution to KP(n,c) */ public int dynamicKP(int [] w, int [] p int c) { int n = w.length – 1; int [][] s = new int[n+1][c+1]; for (int j=0; j<=c; j++) s[0][j] = 0; for (int i=1; i<=n; i++) for (int j=0; j<=c; j++) if ( w[i] > j ) s[i][j] = s[i-1][j]; else s[i][j] = Math.max(s[i-1][j], s[i-1][j-w[i]]+p[i]); return s[n][c]; }
Complexity • Size of dynamic programming table is • O(nC) (n+1 rows and C+1 columns) • Each entry can be computed in O(1) time • So complexity of algorithm is O(nC) • So this is a pseudo-polynomial time • algorithm for KP