

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Algorithm Design and Analysis 3/27/
Instructor: Mike Kowalczyk
The knapsack problem consists of input: a bunch of items, each with a weight and a value, and also a knapsack which has a maximum weight capacity. The challenge is to output a subset of these items that maximizes value of the stuff in the knasack, without exceeding the carrying capacity of the knapsack. This problem is NP-complete, so we will try to solve a variant of it instead.
The fractional knapsack problem is the same as knapsack, except youāre allowed to select fractional items. The text draws the analogy to having items like gold dust and sand. We can solve this variant of the problem in polynomial time. Our greedy choice is to keeping selecting the item with the greatest value per pound. If we canāt fit the whole quantity of an item in the knapsack, then we just take as much as we can, filling the pack to capacity. To prove correctness, we will use the same technique as we did for the scheduling problem (i.e. suppose there is some other choice that leads to an optimal solution and show that ours must also lead to an optimal solution). First note that after adding an item to the knapsack, what remains is effectively another instance of knapsack (except that the knapsack capacity has changed and one of the items is removed). Thus, if we can prove that our first greedy choice leads to some optimal solution, then continually following that choice indeed leads to an optimal solution. Let the value-per-pound of the items be v 1 , v 2 ,... , vn, where v 1 ā„ v 2 ā„ Ā· Ā· Ā· vn. Then item 1 with its v 1 value-per-pound is choosen by our algorithm, and we select some number of pounds p of it (p is either the full quantity, or some partial amount, depending on whether the knapsack got filled to capacity in this step). So the total value added in this step is pv 1 , and it added p pounds of weight. Suppose there is an optimal solution that contains s pounds of item 1, where s < p. Then we can assume without loss of generality that this optimal solution doesnāt contain any of item 1 (otherwise, we can compare both solutions reduced by s pounds of item 1 instead). Then the first p pounds added in the optimal solution has value
ān i=2 aivi^ where^
ān i=2 ai^ =^ p^ and each^ ai^ ā„^ 0. So
ān i=2 aivi^ ā¤^
ān i=2 aiv^1 =^ v^1
ān i=2 ai^ =^ v^1 p, meaning that our greedy choice admits an optimal solution.
Recall that a graph is a set of vertices and edges between the vertices. A graph is 2-colorable if there is an assignment of 2 colors to the vertices of the graph such that every pair of adjacent vertices have different colors (see visual aid). The graph 2-coloring problem is to determine if the graph is 2-colorable.
Algorithm sketch: Start with a vertex, color it blue. Color its neighbors red, color its neighborās neighborās red, etc. If thereās a problem, we will find a vertex which has the āwrong colorā when we try to color it. We repeat this process for every connected component of the graph.