1 / 15

Review of NP-completeness concepts The Class P A decision problem  is in P if and only

Review of NP-completeness concepts The Class P A decision problem  is in P if and only if  is solvable in polynomial time (i.e. O(n c ) for input size n and constant c ) The Class NP A decision problem  is in NP if and only

mayton
Download Presentation

Review of NP-completeness concepts The Class P A decision problem  is in P if and only

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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 : • PP 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 PPandP P thenP P • P is NP-complete if • P NP • PP for some known • NP-complete problem P • if P P andP NP-completethen P = NP • so, P  NP andP NP-complete  P  P

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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]; }

  8. 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

  9. 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

  10. 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 andr  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)

  11. 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; }

  12. 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

  13. 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) =

  14. 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]; }

  15. 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

More Related