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(nc) for input size n and constant c)
• The Class NP
– A decision problem is in NP if and only
if there is a nondeterministic algorithm A
for such that, for any instance x of ,
x is a yes-instance if and only if some
execution of A outputs yes in polynomial
time
– A usually 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
Part 4 # 13
• 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 and :
– if every instance of can be
transformed, in polynomial time, to an
instance of with the same answer
– we say that is reducible to
– if and P then P
• is NP-complete if
– NP
– for some known
NP-complete problem
• if P and NP-complete then P = NP
• so, P NP and NP-complete P
Part 4 # 14
Pseudo-Polynomial Time Algorithms
A method for coping with NP-complete
number 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 yes
• e.g. pick S={2, 4, 5, 6}
(x2+x4+x5+x6 = 4+4+9+11 = 28).
If K = 29, answer is no
• Clearly SS belongs to NP
• Vertex Cover SS (not proved)
• SS is NP-complete
Part 4 # 15
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) = true
B(0, j) = false
j >0
Part 4 # 16
Example
x1=7, x2=4, x3=6, x4=4, x5=9, x6=11,
i 0
xi 0
K=28
1
7
2
4
3
6
4
4
5
9
6
11
j
0
1
2
3
4
5
6
7
8
9
10
11
T
F
F
F
F
F
F
F
F
F
F
F
T
F
F
F
F
F
F
T
F
F
F
F
T
F
F
F
T
F
F
T
F
F
F
T
T
F
F
F
T
F
T
T
F
F
T
T
T
F
F
F
T
F
T
T
T
F
T
T
T
F
F
F
T
F
T
T
T
T
T
T
T
F
F
F
T
F
T
T
T
T
T
T
17
F
F
F
T
T
T
T
28
F
F
F
F
F
F
T
Part 4 # 17
Example
x1=7, x2=4, x3=6, x4=4, x5=9, x6=11,
i 0
xi 0
K=28
1
7
2
4
3
6
4
4
5
9
6
11
j
0
1
2
3
4
5
6
7
8
9
10
11
T
F
F
F
F
F
F
F
F
F
F
F
T
F
F
F
F
F
F
T
F
F
F
F
T
F
F
F
T
F
F
T
F
F
F
T
T
F
F
F
T
F
T
T
F
F
T
T
T
F
F
F
T
F
T
T
T
F
T
T
T
F
F
F
T
F
T
T
T
T
T
T
T
F
F
F
T
F
T
T
T
T
T
T
17
F
F
F
T
T
T
T
28
F
F
F
F
F
F
T
Part 4 # 18
Dynamic Programming Algorithm for SS
/** Input:
*
*
* Output:
*
array of ints x[0]...x[n]
N.B. x[0] unused
target positive integer k
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[][] = new boolean[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];
}
Part 4 # 19
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
Part 4 # 20
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|
is the size of x, max is the largest number
occurring in x and q is some polynomial
function
Part 4 # 21
A second example - Knapsack Problem (KP)
Instance: n items, where item i has a weight
wi and 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 capacity 65. 5 items 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 weight 56 C
and total profit 68
Choosing items 2,3,4 gives total weight
63 C and total profit 69 (optimal)
Part 4 # 22
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;
}
Part 4 # 23
The Greedy Algorithm does not work
• Example
item
1
2
3
weight
10
20
30
profit
60
100
120
profit/
weight
6
5
4
• 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
Part 4 # 24
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, j) =
S(i-1, j) ,
if wi >j
S(i-1, j),
max
S(i-1, j - wi) + pi , otherwise
for i>0, subject to S(0, j) = 0 (0 j C)
• Implies an O(nC) algorithm
Part 4 # 25
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];
}
Part 4 # 26
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
Part 4 # 27
© Copyright 2026 Paperzz