0% found this document useful (0 votes)
4 views

Homework-2-Greedy

Uploaded by

Devang Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Homework-2-Greedy

Uploaded by

Devang Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Homework 2

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.

(a) Design an algorithm for the fractional knapsack problem.


(b) What is the time complexity for your algorithm. Give step-by-step analysis.
(c) Run your algorithm on an example.
(d) Implement your algorithm in your preferred language.
(e) Does your algorithm works for the 0-1 knapsack problem.

2. Breadth-First search and Depth-First search are simple graph searching algorithms.

(a) Design algorithm for Breadth-First search


(b) Design algorithm for Depth-First search
(c) Find their time complexities.
(d) Run each algorithm on some examples.
(e) Implement your algorithms in your preferred language.

Hint: [BOOK: Introduction to Algorithms by Thomas H. Cormen Charles E. Leiserson Ronald L.


Rivest Clifford Stein] See chapter 22: Elementary Graph Algorithms
Section 22.1: Representations of graphs
Section 22.2: Breadth-first search
Section 22.3: Depth-first search
3. The Huffman Coding Problem
(a) Design a greedy algorithm for the Huffman coding problem.
(b) What is the time complexity for your algorithm. Give step-by-step analysis.
(c) Run your algorithm on an example.
(d) Implement your algorithm in your preferred language.

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.

You might also like