Docsity

Optimizing Resource Allocation: Knapsack & Change Making Problems, Assignments of Algorithms and Programming

Toggle sidebar
bg1
CS 3510: Design and Analysis of Algorithms
Problem Set 6 Solutions
Problem 1
In the 0-1 knapsack problem, one is given a collection of nitems where the i-th item is worth vi
dollars and weighs wipounds, and a knapsack that can hold Wpounds, and the goal is to find a
subcollection of items with the maximum value that can fit in the knapsack, i.e. whose total weight
does not exceed W. Design a O(nW ) time algorithm for the 0-1 knapsack problem.
Solution: The solution is based on the following optimal substructure property: Let item ibe the
highest-numbered item in an optimal solution Sfor Wpounds and items 1, . . . , n. Then S0=S−{i}
is an optimal solution for W−wipounds and items 1, . . . , i −1, and the value of the solution S
is that of S0plus vi. Define c[i, w] to be the value of the optimal solution for items 1, . . . , i and
maximum weight w. Then
c[i, w] =
0 if i= 0 or w= 0
c[i−1, w]wi> w
max {vi+c[i−1, w −wi], c[i−1, w ]}if i > 0 and w≥wi.
The last equality says that if an optimal solution for the first iitems and weight wincludes item
i, then its value equals that of viplus the value of an optimal solution for the first i−1 items and
weight w−wi; otherwise it equals that of an optimal solution for the first i−1 items and weight
w. The better of these two choices should be made.
The above relation yields a dynamic programming algorithm that given as input the maximum
weight W, the number of items n, values (v1, . . . , vn) and weights (w1, . . . , wn) of the individual
items, computes and stores the values c[i, w] in a table c[0 . . . n, 0. . . W ], as follows:
•for w= 0 to W:
c[0, w]←0
•for i= 1 to n:
c[i, 0] ←0
for w= 1 to W:
if wi≤w
then c[i, w]←max {vi+c[i−1, w −wi], c[i−1, w]}
else
c[i, w]←c[i−1, w]
Thus, c[n, W ] is the value of an optimal solution. The set of items to include can be deduced from
the table by starting at c[n, W ] and tracing as follows: if c[i, w ] = c[i−1, w], then item iis not
included, and continue tracing with c[i−1, w]; otherwise item iis included, and continue tracing
with c[i−1, w −wi].
The algorithm has (n+ 1)(W+ 1) table entries to fill in, each taking O(1) time. Tracing an
optimal solution takes O(n) time. Thus the algorithm takes O(nW ) time.
1
pf3
pf4

Partial preview of the text

Download Optimizing Resource Allocation: Knapsack & Change Making Problems and more Assignments Algorithms and Programming in PDF only on Docsity!

CS 3510: Design and Analysis of Algorithms

Problem Set 6 Solutions

Problem 1

In the 0-1 knapsack problem, one is given a collection of n items where the i-th item is worth vi dollars and weighs wi pounds, and a knapsack that can hold W pounds, and the goal is to find a subcollection of items with the maximum value that can fit in the knapsack, i.e. whose total weight does not exceed W. Design a O(nW ) time algorithm for the 0-1 knapsack problem.

Solution: The solution is based on the following optimal substructure property: Let item i be the highest-numbered item in an optimal solution S for W pounds and items 1,... , n. Then S′^ = S−{i} is an optimal solution for W − wi pounds and items 1,... , i − 1, and the value of the solution S is that of S′^ plus vi. Define c[i, w] to be the value of the optimal solution for items 1,... , i and maximum weight w. Then

c[i, w] =

0 if i = 0 or w = 0 c[i − 1 , w] wi > w max {vi + c[i − 1 , w − wi], c[i − 1 , w]} if i > 0 and w ≥ wi.

The last equality says that if an optimal solution for the first i items and weight w includes item i, then its value equals that of vi plus the value of an optimal solution for the first i − 1 items and weight w − wi; otherwise it equals that of an optimal solution for the first i − 1 items and weight w. The better of these two choices should be made. The above relation yields a dynamic programming algorithm that given as input the maximum weight W , the number of items n, values (v 1 ,... , vn) and weights (w 1 ,... , wn) of the individual items, computes and stores the values c[i, w] in a table c[0... n, 0... W ], as follows:

  • for w = 0 to W : c[0, w] ← 0
  • for i = 1 to n: c[i, 0] ← 0 for w = 1 to W : if wi ≤ w then c[i, w] ← max {vi + c[i − 1 , w − wi], c[i − 1 , w]} else c[i, w] ← c[i − 1 , w]

Thus, c[n, W ] is the value of an optimal solution. The set of items to include can be deduced from the table by starting at c[n, W ] and tracing as follows: if c[i, w] = c[i − 1 , w], then item i is not included, and continue tracing with c[i − 1 , w]; otherwise item i is included, and continue tracing with c[i − 1 , w − wi]. The algorithm has (n + 1)(W + 1) table entries to fill in, each taking O(1) time. Tracing an optimal solution takes O(n) time. Thus the algorithm takes O(nW ) time. 

Problem 2

Consider the problem of making change for n cents using the smallest number of coins.

  1. Give a greedy algorithm that makes change using quarters, dimes, nickels, and pennis. Prove that your algorithm yields an optimal solution. Solution: A greedy algorithm makes change for n cents as follows:

(a) First give q = bn/ 25 c quarters. After this nq = n mod 25 cents remain. (b) Then give d = bnq/ 10 c dimes. After this nd = nq mod 10 cents remain. (c) Then give k = bnd/ 5 c nickels. After this nk = nd mod 5 cents remain. (d) Finally give p = nk pennies.

We prove the correctness of the algorithm by induction on n. It can be verified that the algorithm yields an optimal solution for each n ≤ 25. Suppose that the algorithm yields an optimal solution for each n < k, where k > 25. We show that the algorithm yields an optimal solution for n = k. We first observe that if k > 25, then an optimal solution for k cents must use at least one quarter. Suppose a solution uses only dimes, nickels and pennies. If it contains three dimes, then replacing them by one quarter and one nickel results in a solution with one fewer coin. If it contains at most two dimes, then some subset of dimes, nickels and pennies add up to 25 cents, which can replaced by one quarter to yield a solution with fewer coins. Thus an optimal solution for k cents must use at least one quarter. By the induction hypothesis, the greedy yields an optimal solution for k − 25 cents. Thus it also yields an optimal solution for k cents. 

  1. Give a set of coin denominations for which the greedy algorithm above does not yield an optimal solution. Solution: Suppose that { 1 , 10 , 25 } is the set of coin denominations. For n = 30 cents, the solution returned by the greedy algorithm includes one quarter and five pennies, whereas the optimal solution uses three dimes. 
  2. Give a O(nk) time algorithm that makes change using any set of k coin denominations of which one is worth one cent. Solution: Let {d 1 ,... , dk} be an arbitrary set of coin denominations. For each j ∈ N, define c[j] to be the minimum number of coins for j cents. It follows that

c[j] =

0 if j ≤ 0 1 + min 1 ≤i≤k {c[j − di]} if j > 0.

One can compute c[1],... , c[n], as well as information for yielding an optimal solution, using dynamic programming as follows:

  • for j = 1 to n: c[j] ← ∞ for i = 1 to k: if j ≥ di and 1 + c[j − di] < c[j], then c[j] ← 1 + c[j − di] denom[j] ← di

〈ϕ′〉 ∈ Double-Set. Conversely, if 〈ϕ〉 6 ∈ SAT, then no assignment to the variables of ϕ satisfies ϕ; since the new variable y does not appear in ϕ, no assignment can satisfy ϕ ∧ (y ∨ y) = ϕ′. Therefore ϕ′^ has no satisfying assignment, hence 〈ϕ′〉 6 ∈ Double-Set.

We therefore conclude that Double-SAT is NP-Complete. 

Problem 4

Let U be a set. Let S = {S 1 ,... , Sm} be a collection of subsets of U 1 such that U =

⋃m i=1 Si. A subcollection of subsets {Si 1 ,... , Sik } in S is a set cover of size k if U =

⋃k j=1 Sij^. Define Set-Cover = {〈S, k〉 : S has a set cover of size k}.

By reduction from Vertex-Cover, prove that Set-Cover is NP-Complete.

Solution: It is clear that Set-Cover ∈ NP. If S has a set cover of size k, then such a set cover is a proof of this fact. A verifier V , on input 〈U, S, k〉 and a supposed proof π, simply verifies that

  1. π encodes a collection C of k subsets of U ;
  2. C ⊂ S (that is, the collection C encoded by π is a subcollection of S);
  3. C covers U , i.e. U =

A∈C A;

and accepts if and only if all the above conditions are satisfied. It is clear that

  1. For each 〈U, S, k〉 ∈ Set-Cover, there is a subcollection C ⊂ S such that V (〈U, S, k〉, 〈C〉) = 1.
  2. For each 〈U, S, k〉 6 ∈ Set-Cover, for every string π, V (U, 〈S, k〉, π) = 0.
  3. The verifier V runs in time polynomial in |〈U, S, k〉|.

Therefore Set-Cover ∈ NP.

To show that Set-Cover is NP-Hard, we give a polynomial-time reduction from Vertex-Cover to Set-Cover, as follows:

On input a Vertex-Cover instance 〈G = (V, E), k〉:

  1. Let U = E, that is, the universe U is the set of edges in G.
  2. For each vertex v ∈ V in G, define Sv = {(u, v) : (u, v) ∈ E}. That is, Sv is the set of all edges incident with v.
  3. Let S = {Sv : v ∈ V }. That is, the collection S consists of Sv for every vertex v ∈ V.
  4. Output 〈E, S, k〉.

Clearly the reduction takes polynomial time. We now show that the reduction is correct, that is, 〈G, k〉 ∈ Vertex-Cover if and only if 〈E, S, k〉 ∈ Set-Cover. If {v 1 ,... , vk} is a vertex cover in G, then ∪ki=1Svi = E = U , and thus {Sv 1 ,... , Svk } is a set cover in S = {Sv : v ∈ V }. Conversely, if {Sv 1 ,... , Svk } is a set cover in S, then ∪ki=1Svi = E = U , and thus {v 1 ,... , vk} is a vertex cover in G.

We therefore conclude that Vertex-Cover is NP-Complete.  (^1) That is, each Si is a subset of U