1 / 19

Greedy Algorithms

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 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.

mills
Download Presentation

Greedy Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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: • 10, 10, 1,1,1,1,1, • 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: 10 10 11111 10 5 5 5

  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: • 10 10 10 • 6 6 6 6 6 • 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: • Sort the requests by finish time. • 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: • Sort the requests by finish time. • 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

  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: • Sort all the requests by start time. • 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: • Sort all the requests by start time. • 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.

  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 I1, I2, ... In. Each item has two attributes: • A value/unit; let this be vi for item Ii. • Weight available; let this be wi for item Ii. • The algorithm is as follows: • Sort the items by value/unit. • 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 I1 available with a value of $50/lb. • 25 lbs. of I3 available with a value of $40/lb. • 40 lbs. of I2 available with a value of $30/lb. • The algorithm is as follows: • Sort the items by value/unit. • 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: • Take 4 lbs of I1. • Take 25 lbs. of I3. • Tale 21 lbs. of I2. • 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/lectures/ • 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

More Related