Presentation is loading. Please wait.

Presentation is loading. Please wait.

Informatics Department Parahyangan Catholic University D ESIGN & A NALYSIS OF A LGORITHM 12 – G REEDY A LGORITHM.

Similar presentations


Presentation on theme: "Informatics Department Parahyangan Catholic University D ESIGN & A NALYSIS OF A LGORITHM 12 – G REEDY A LGORITHM."— Presentation transcript:

1 Informatics Department Parahyangan Catholic University D ESIGN & A NALYSIS OF A LGORITHM 12 – G REEDY A LGORITHM

2 G REEDY A LGORITHM 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 E XAMPLE : P RIM ’ 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 E XAMPLE : K RUSKAL ’ 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 E XAMPLE : H UFFMAN ’ S A LGORITHM Build a Huffman tree out of n elements Performs n-1 iterations, each merging two elements with smallest frequency

6 0/1 K NAPSACK P ROBLEM 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 F RACTIONAL K NAPSACK P ROBLEM 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 M AKING C HANGE 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 ! 1 ¢ 5 ¢ 10 ¢ 25 ¢

9 G REEDY A LGORITHM Advantages 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 E XERCISE : G AS S TATION 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 d 1, d 2,..., d n. Starting with full tank, give a greedy algorithm that minimize the number of stops for buying gas.

11 A CTIVITIES S CHEDULING Given a set S = {a 1, a 2,..., a n } of n proposed activities that wish to use a lecture hall, which can be used by only one activity at a time Each activity a i has a start time s i and a finish time f i Give a greedy algorithm that maximize the number of activities scheduled

12 A CTIVITIES S CHEDULING Choose the shortest time first ? Sort by start time ? Sort by end time ? a1a1 a2a2 a3a3 a1a1 a2a2 a3a3 a4a4 a5a5 Works ! But why ?

13 C ORRECTNESS 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 E XERCISE : V OLUNTEERS S CHEDULING 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 E XERCISE : V OLUNTEERS S CHEDULING (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 e ffi cient greedy algorithm that does always yield an optimal solution.


Download ppt "Informatics Department Parahyangan Catholic University D ESIGN & A NALYSIS OF A LGORITHM 12 – G REEDY A LGORITHM."

Similar presentations


Ads by Google