Presentation is loading. Please wait.

Presentation is loading. Please wait.

Greedy Algorithms. Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab.

Similar presentations


Presentation on theme: "Greedy Algorithms. Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab."— Presentation transcript:

1 Greedy Algorithms

2 Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab you are covered since the lab attendance is out of 10. Assignment #3 is posted due 10/20/2010 Exam #2 on 10/27/2010

3 Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming Algorithms ◦ And Backtracking Algorithms These are 4 common types of algorithms used to solve problems. ◦ For many problems, it is quite likely that at least one of these methods will work.

4 Greedy Algorithms A greedy algorithm is one where you take the step that seems the best at the time while executing the algorithm. Previous Examples: ◦ Huffman coding ◦ Minimum Spanning Tree Algorithms – Kruskal’s and Prim’s ◦ Dijkstra’s Algorithm

5 Coin Changing The goal is to give change with the minimal number of coins as possible for a certain number of cents using 1 cent, 5 cent, 10 cent, and 25 cent coins. The Greedy Algorithm ◦ Keep giving as many coins of the largest denomination as possible until you have a remaining value less than the value of that denomination. ◦ Then you continue with the lower denomination and repeat until you’ve given out the correct change. If you think about it, this is the algorithm a cashier typically uses when giving out change.

6 Coin Changing How do we know this algorithm always works? ◦ Consider all combinations of giving change, ordered from highest denomination to lowest using 1-cent, 5-cent and 10-cent coins. ◦ 2 ways of making change for 25 cents are: 1)10, 10, 1,1,1,1,1, 2)10, 5, 5, 5 ◦ Since each larger denomination is divisible by each smaller one  We can always make a mapping for each coin in one list to a coin or set of coins in the other list. ◦ For example: 101011111 105 55

7 Coin Changing This argument doesn’t work for any set of coins without the divisibility rule. ◦ Consider 1,6,10 ◦ There is no way to match up these two ways of producing 30 cents: ◦ 101010 ◦66666◦66666 Consider making change for 18 cents ◦ Largest denomination first gives you 10, 6, 1, 1  However, 6, 6, 6 gives you the least amount of coins.

8 Coin Changing Problem Extension to 25 cent coin ◦ Even though a 10 doesn’t divide into 25, there are no values, multiples of 25, for which it’s advantageous to give a set of dimes over a set of quarters.

9 Single Room Scheduling Problem Given a single room to schedule, and a list of requests, the goal of this problem is to maximize the total number of events scheduled. ◦ Each request simply consists of the group, a start time and an end time during the day. The Greedy Solution: 1)Sort the requests by finish time. 2)Go through the requests in order of finish time, scheduling them in the room if the room is unoccupied at its start time.

10 Single Room Scheduling Problem Example: Say you’ve gone to a music festival ◦ There are so many shows and so little time! ◦ If each one of the shows schedule the first day are equally important for you to see, ◦ And you will only go to a show if you can stay the entire time, ◦ Maximize the number of shows you can go to. 5-7pm: Green Day 7-9pm: Weezer 7:30-9:30: Gorillaz 9-11pm: 311 9:30pm-10:30pm: Coldplay 11pm-2am: The Killers

11 Single Room Scheduling Problem Example: The Greedy Solution: 1)Sort the requests by finish time. 2)Go through the requests in order of finish time, scheduling them in the room if the room is unoccupied at its start time. 5-7pm: Green Day 7-9pm: Weezer 7:30-9:30pm: Gorillaz 9:30pm-10:30pm: Coldplay 9-11pm: 311 11pm-2am: The Killers 11pm – 2am The Killers 9:30pm-10:30pm Coldplay 7-9pm Weezer 5-7pm Green Day

12 Single Room Scheduling Proof Prove that this algorithm does indeed maximize the number of events scheduled Shown on the board

13 Multiple Room Scheduling Given a set of requests with start and end times, the goal here is to schedule all events using the minimal number of rooms Greedy Algorithm: 1)Sort all the requests by start time. 2)Schedule each event in any available empty room. If no room is available, schedule the event in a new room.

14 Multiple Room Scheduling Example Greedy Algorithm: 1)Sort all the requests by start time. 2)Schedule each event in any available empty room. If no room is available, schedule the event in a new room. Final Exam Schedule for Computer Science: 7-10am: CS1 7-10am: Intro to C 9am-12pm: OOP 10am-1pm: OS 12-3pm: CS 2 1-4pm: Comp Arch. ROOM 1 7-10am: CS 1 ROOM 2 7-10am: Intro to C ROOM 3 9am-12pm: OOP 10am-1pm: OS12pm-3pm: CS 2 1-4pm: Comp Arch

15 Multiple Room Scheduling Proof Let k be the number of rooms this algorithm uses for scheduling. When the kth room is scheduled, it MUST have been the case that all k-1 rooms before it were in use. At the exact point in time that the k room gets scheduled, we have k simultaneously running events. It's impossible for any schedule to handle this type of situation with less than k rooms. Thus, the given algorithm minimizes the total number of rooms used.

16 Fractional Knapsack Problem Your goal is to maximize the value of a knapsack that can hold at most W units worth of goods from a list of items I 1, I 2,... I n. Each item has two attributes: 1)A value/unit; let this be v i for item I i. 2)Weight available; let this be w i for item I i. The algorithm is as follows: 1) Sort the items by value/unit. 2)Take as much as you can of the most expensive item left, moving down the sorted list. You may end up taking a fractional portion of the "last" item you take.

17 Fractional Knapsack Example Say we have: ◦ 4 lbs. of I 1 available with a value of $50/lb. ◦ 25 lbs. of I 3 available with a value of $40/lb. ◦ 40 lbs. of I 2 available with a value of $30/lb. The algorithm is as follows: 1) Sort the items by value/unit. 2)Take as much as you can of the most expensive item left, moving down the sorted list. You may end up taking a fractional portion of the "last" item you take. You will do the following: 1)Take 4 lbs of I 1. 2)Take 25 lbs. of I 3. 3)Tale 21 lbs. of I 2. Value of knapsack = 4*50 + 25*40 + 21*30 = $1830.

18 Fractional Knapsack Proof Why is this maximal? ◦ Because if we were to exchange any good from the knapsack with what was left over, it is IMPOSSIBLE to make an exchange of equal weight such that the knapsack gains value. ◦ The reason for this is that all the items left have a value/lb. that is less than or equal to the value/lb. of ALL the material currently in the knapsack. ◦ At best, the trade would leave the value of the knapsack unchanged. ◦ Thus, this algorithm produces the maximal valued knapsack!

19 References Slides adapted from Arup Guha’s Computer Science II Lecture notes: http://www.cs.ucf.edu/~dmarino/ucf/cop3503/le ctures/ http://www.cs.ucf.edu/~dmarino/ucf/cop3503/le ctures/ Additional material from the textbook: Data Structures and Algorithm Analysis in Java (Second Edition) by Mark Allen Weiss Additional images: www.wikipedia.com xkcd.com


Download ppt "Greedy Algorithms. Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab."

Similar presentations


Ads by Google