Docsity

Greedy Algorithms: Knapsack Problem and Graph Coloring - Prof. Michael R. Kowalczyk, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Algorithm Design and Analysis 3/27/2008
Lecture 28: More Greedy Algorithms
Instructor: Mike Kowalczyk
1 More Greedy Algorithms
1.1 Knapsack
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.
1.2 Fractional Knapsack
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 v1,v2, . . . , vn, where v1≄v2≄ Ā· Ā· Ā· vn. Then item 1 with its
v1value-per-pound is choosen by our algorithm, and we select some number of pounds pof 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 pv1, and it added ppounds of
weight. Suppose there is an optimal solution that contains spounds 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 spounds of item 1 instead). Then the first
ppounds added in the optimal solution has value Pn
i=2 aiviwhere Pn
i=2 ai=pand each ai≄0.
So Pn
i=2 aivi≤Pn
i=2 aiv1=v1Pn
i=2 ai=v1p, meaning that our greedy choice admits an optimal
solution.
1.3 Graph Coloring
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.
1
pf2

Partial preview of the text

Download Greedy Algorithms: Knapsack Problem and Graph Coloring - Prof. Michael R. Kowalczyk and more Study notes Algorithms and Programming in PDF only on Docsity!

Algorithm Design and Analysis 3/27/

Lecture 28: More Greedy Algorithms

Instructor: Mike Kowalczyk

1 More Greedy Algorithms

1.1 Knapsack

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.

1.2 Fractional Knapsack

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.

1.3 Graph Coloring

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.