Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design & Analysis of Algorithm Greedy Algorithm

Similar presentations


Presentation on theme: "Design & Analysis of Algorithm Greedy Algorithm"— Presentation transcript:

1 Design & Analysis of Algorithm Greedy Algorithm
Informatics Department Parahyangan Catholic University

2 Greedy Algorithm Greedy algorithm builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit Example : Chess VS Scrabble

3 Example : Prim’s MST Build a minimum spanning tree from a graph of n vertices Start with an tree T with one vertex “root” Performs n-1 iterations, each adding one vertex to T with the smallest edge weight

4 Example : Kruskal’s MST
Build a minimum spanning tree from a graph of n vertices Start with an empty tree T Performs n-1 iterations, each adding the smallest weighted edge to T that doesn’t produce a cycle

5 Example : Huffman’s Algorithm
Build a Huffman tree out of n elements Performs n-1 iterations, each merging two elements with smallest frequency

6 0/1 Knapsack Problem Given a set S of n items, each having size and value, and a knapsack of size C Find a subset of S which maximize the value and total size must not exceed C Can we solve 0/1 Knapsack problem with greedy approach ?

7 Fractional Knapsack Problem
Given a set S of n items, each having size and value, and a knapsack of size C Find a subset of S which maximize the value and total size must not exceed C We are allowed to take fractions of the items Can we solve this problem using greedy approach ? Why is it correct ?

8 Making Change 25 ¢ 10 ¢ 5 ¢ 1 ¢ How to make a change of n ¢ using as few coins as possible ? Does this work if denominations of coins are 1 ¢, 10 ¢, 12 ¢ ? Try making 30 ¢ change !

9 Greedy Algorithm Advantages Disadvantages Work fast when they work
Simple and easy to implement Disadvantages Does not always produce an optimal solution to every problem Correctness of greedy algorithm is hard to prove

10 Exercise : Gas Station A traveler needs to drive along a highway to reach the next town he wants to visit. His car can go up to 200 km on full tank. There are gas stations at d1, d2, ..., dn. Starting with full tank, give a greedy algorithm that minimize the number of stops for buying gas.

11 Activities Scheduling
Given a set S = {a1, a2, , an} of n proposed activities that wish to use a lecture hall, which can be used by only one activity at a time Each activity ai has a start time si and a finish time fi Give a greedy algorithm that maximize the number of activities scheduled

12 Activities Scheduling
Choose the shortest time first ? Sort by start time ? Sort by end time ? a1 a2 a3 a1 a2 a3 a4 a5 Works ! But why ?

13 Correctness It is easy to show that an algorithm is incorrect  give a counter example However, it is harder to show that an algorithm is correct  need formal proof

14 Exercise : Volunteers Scheduling
Jaan is scheduling volunteers to work at an information booth. Each volunteer sends Jaan a time interval that he or she is willing to work. The interval is specified by a starting time and a finishing time. For example, one volunteer might say that he is willing to work at the information booth from 10:13 a.m. until 11:42 a.m. Then Jaan must choose a set of volunteers so that there is at least one person at the information booth at all times between 8:00 a.m. and 10:00 p.m. Jaan wants to use the smallest possible number of volunteers to achieve this.

15 Exercise : Volunteers Scheduling
(a) One possible greedy algorithm for this problem would be to sort the volunteers so that the lengths of their intervals are in non-increasing order, and then go through them one-by- one, scheduling anyone who is willing to work at some time that is not already covered by other scheduled volunteers. Show that this strategy does not always yield an optimal solution. (b) Design a simple and efficient greedy algorithm that does always yield an optimal solution.


Download ppt "Design & Analysis of Algorithm Greedy Algorithm"

Similar presentations


Ads by Google