Homework-2-Greedy
Homework-2-Greedy
Greedy Algorithms
1. Knapsack problem: Given a set S of n items {a1 , a2 , . . . , an }, where each item ai has a weight wi
and value vi . Also there is a bag with capacity W .
P P
Select fractions f (ai )’s of ai ’s (1 ≤ i ≤ n) such that ai f (ai ) · wi ≤ W and the value ai f (ai ) · vi is
maximized. Note that the value of f (ai ) is between 0 and 1.
For example, item, value, and weight are given in the table and let W be 4.
a1 a2 a3
wi 2 2 3
vi 100 10 120
2
Then if we take fraction 1 of a1 and fraction 3 of a3 then the value is 100 + ( 32 · 120) = 180
This problem is called fractional knapsack problem. There is another variation called the 0-1 knapsack
problem
In 0-1 knapsack problem, f (ai ) is either 1 or 0. That means an item is either picked fully or it is not
picked.
2. Breadth-First search and Depth-First search are simple graph searching algorithms.
1
Hint: [BOOK: Introduction to Algorithms by Thomas H. Cormen Charles E. Leiserson Ronald L.
Rivest Clifford Stein] Section 16.3: Huffman Coding
4. Suppose that all edge weights in a graph are integers in the range from 1 to |V |. How fast can you
make Prim’s algorithm run? What if the edge weights are integers in the range from 1 to W for some
constant W ?
5. Professor Borden proposes a new divide-and-conquer algorithm for computing minimum spanning trees,
which goes as follows. Given a graph G(V, E), partition the set V of vertices into two sets V1 and V2
such that |V1 | and |V2 | differ by at most 1. Let E1 be the set of edges that are incident only on vertices
in V1 , and let E2 be the set of edges that are incident only on vertices in V2 . Recursively solve a
minimum-spanning-tree problem on each of the two subgraphs G1 (V1 , E1 ) and G2 (V2 , E2 ). Finally,
select the minimum-weight edge in E that crosses the cut (V1 , V2 ), and use this edge to unite the
resulting two minimum spanning trees into a single spanning tree.
Either argue that the algorithm correctly computes a minimum spanning tree of G, or provide an
example for which the algorithm fails.