Tutorial 11 Solutions

Copyright Notice: UBC retains the rights to this document. You may not distribute this document without permission.
CPSC 320: Intermediate Algorithm Design
2016 Summer Term 1
Tutorial 11 Solutions
Prof. Nick Harvey
University of British Columbia
1. (NP Completeness of 4-coloring) Graph coloring is discussed in the textbook (KleinbergTardos Section 8.7, Erickson Section 30.10). Given a graph G = (V, E), a k-coloring of G is a
function f mapping the vertices V to the integers {1, ..., k}, such that f (u) 6= f (v) for every edge
(u, v) ∈ E.
The 3-Coloring decision problem is “Given a graph G, does G have a 3-coloring?” The 4Coloring decision problem is “Given a graph G, does G have a 4-coloring?”
(a) Prove that the 4-Coloring problem is in NP.
Solution. A solution to the 4-Coloring problem is a function f mapping the vertices V to
the integers {1, ..., 4}. This function can be represented as an array of length |V |. Each array
entry requires two bits to store a number in {1, ..., 4}. So the total number of bits is 2|V |.
This is linear in the size of the graph.
Given a purported solution, we can verify in polynomial time if it indeed satisfies that property. To do so, we iterate over each edge in the graph, look up the colors of its endpoints in
the purported solution, and check that the colors are not equal.
(b) The 3-Coloring problem is known to be NP complete. (This is proven in Kleinberg-Tardos
Section 8.7 and Erickson Section 30.10.)
Prove that the 4-Coloring problem is NP complete.
Solution. We prove that the 4-Coloring problem is NP complete by a polynomial-time
reduction from the 3-Coloring problem problem. That is, supposing we had a subroutine
for solving the 4-Coloring problem, we show how to solve the 3-Coloring problem (with only
polynomial extra computations).
Suppose we are given an instance of the 3-Coloring problem: a graph G = (V, E). We then
create a new graph G0 from G by adding a new vertex v 0 and adding an edge {v, v 0 } for every
v ∈ V . We then provide the graph G0 to our subroutine for solving the 4-Coloring problem.
The output of that subroutine is single bit telling us whether or not G0 has a 4-Coloring. We
return that same bit as the solution of the 3-Coloring problem.
The correctness of this reduction is due to the following claim.
Claim 0.1. G can be 3-Colored if and only if G0 can be 4-colored.
Proof. Suppose G can be colored with 3 colors. Then G0 can be colored with 4 colors: just
assign the vertex v 0 the color ”4”. Conversely, suppose that G0 can be 4-colored. Then v 0 uses
a color that is not used by any other vertex in V . So the remaining colors are a 3-coloring of
G (possibly renumbering so that the remaining colors are {1, 2, 3}).
2. (NP Completeness of Knapsack) The Subset Sum Problem is defined as follows. (See
Kleinberg-Tardos Sections 6.4 and 8.8 or Erickson Sections 3.3, 5.6.1 and 30.12.) The input is a
list of numbers a1 , ..., an , and a target number A. The decision problem is “Does there exist a
subset of {a1 , ..., an } that adds up to exactly A?”
The Knapsack Problem is defined as follows. The input is a list of item values v1 , ..., vn , item
weights w1 , ..., wn , a target value V , and a maximum
capacity W
P
P. The decision problem is “Does
there exist a subset S of {1, ..., n} for which i∈S vi ≥ V , and i∈S wi ≤ W ?”
1
Copyright Notice: UBC retains the rights to this document. You may not distribute this document without permission.
(a) Prove that the Knapsack Problem is in NP.
Solution. A solution to the Knapsack problem is a subset S. This function can be represented as an bitvector of length n. This is linear in the size of the input.
Given a purported solution S, we can verify in polynomial time that it is a valid solution.
To do so, we iterate over each element of S, adding up their values and their weights. Then
we check if the total value is at least V and the total weight is at most W .
(b) The Subset Sum problem is proven to be NP-complete in Kleinberg-Tardos Section 8.8 and
Erickson Section 30.12. Prove that the Knapsack Problem is NP-complete.
Solution. We prove that the Knapsack problem is NP complete by a polynomial-time
reduction from the Subset Sum problem. That is, supposing we had a subroutine for solving
the Knapsack problem, we show how to solve the Subset Sum problem (with only polynomial
extra computations).
Suppose we are given an instance of the Sumset Sum problem: the list a1 , ..., an , and the
target A. We form the input to the Knapsack problem as follows: we set vi = ai , wi = ai ,
V = A and W = A. We then provide this input to the subroutine for solving the Knapsack
problem. The output of that subroutine is single bit telling us whether or not there is a
solution to the Knapsack problem. We return that same bit as the solution of the Subset
Sum problem.
The correctness of this reduction is due to the following claim.
Claim 0.2. The constructed instance of the Knapsack Problem has a solution if and only
if the given instance of the Subset Sum Problem has a solution.
Proof.
The subset sum problem has a solution precisely when there exists S such that
P
our constructed instance of the Knapsack problem, this is equivalent to
Pi∈S ai = A. In P
v
≥
V
and
i∈S i
i∈S wi ≤ W . And that is the precisely the condition that S is a solution
to the Knapsack problem.
2