This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Analysis of Algorithms  Homework II (Solutions) K. Subramani LCSEE, West Virginia University, Morgantown, WV { ksmani@csee.wvu.edu } 1 Problems 1. In the Knapsack problem, you are given a knapsack of capacity W and n objects { o 1 ,o 2 ,...o n } with respective weights { w 1 ,w 2 ,...,w n } and respective profit values { p 1 ,p 2 ,...p n } . The goal is to pack the objects into the knapsack in a manner that maximizes the profit of knapsack, without violating its capacity constraint. In class, we showed that if we are permitted to choose fractions of objects, then the problem can be solved by a greedy strategy. The / 1 Knapsack problem is a variant of the knapsack problem in which you cannot choose fractions of objects, i.e., each object is either selected or not. Argue with a counterexample that the greedy strategy does not work for the / 1 Knapsack problem. Solution: Consider the following input instance h o 1 ,o 2 ,o 3 i : (a) W = 5 . (b) h p 1 ,p 2 ,p 3 i = h 6 , 10 , 12 i . (c) h w 1 ,w 2 ,w 3 i = h 1 , 2 , 3 i . The greedy strategy for fractional knapsack would pick o 1 and o 2 , whereas the optimal solution is clearly o 1 and o 3 . 2 2. Let A = { a 1 ,a 2 ,...,a n } denote a set of positive integers that add up to N . Design an O ( n · N ) algorithm for determining whether there is a subset B of A , such that ∑ a i ∈ B a i = ∑ a i ∈ A B a i . Solution: Observe that the problem is asking whether A can be partitioned into two parts which sum up to N 2 . Without loss of generality, we assume that N is even; since if N is odd, the answer is “no”!...
View Full
Document
 Spring '10
 .
 Algorithms

Click to edit the document details