greedy algorithms. announcements exam #1 ◦ see me for 2 extra points if you got #2(a) wrong. lab...

19
Greedy Algorithms Greedy Algorithms

Upload: milo-burke

Post on 17-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Greedy AlgorithmsGreedy Algorithms

Page 2: 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

AnnouncementsAnnouncementsExam #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

Page 3: 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

Algorithm Design Algorithm Design TechniquesTechniquesWe 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.

Page 4: 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

Greedy AlgorithmsGreedy AlgorithmsA 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

Page 5: 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

Coin ChangingCoin ChangingThe 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.

Page 6: 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

Coin ChangingCoin ChangingHow 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:10 10 1111110 5 5 5

Page 7: 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

Coin ChangingCoin ChangingThis 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.

Page 8: 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

Coin Changing ProblemCoin Changing ProblemExtension 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.

Page 9: 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

Single Room Scheduling Single Room Scheduling ProblemProblemGiven 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.

Page 10: 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

Single Room Scheduling Single Room Scheduling Problem Example: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

Page 11: 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

Single Room Scheduling Problem Single Room Scheduling Problem Example: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

Page 12: 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

Single Room Scheduling Single Room Scheduling ProofProofProve that this algorithm does indeed

maximize the number of events scheduled

Shown on the board

Page 13: 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

Multiple Room Scheduling 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.

Page 14: 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

Multiple Room Scheduling Multiple Room Scheduling ExampleExample

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: CS17-10am: Intro to C9am-12pm: OOP10am-1pm: OS12-3pm: CS 21-4pm: Comp Arch.

ROOM 1

7-10am: CS 1

ROOM 2

7-10am: Intro to C

ROOM 3

9am-12pm: OOP10am-1pm: OS 12pm-3pm: CS

21-4pm: Comp Arch

Page 15: 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

Multiple Room Scheduling Multiple Room Scheduling ProofProofLet 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.

Page 16: 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

Fractional Knapsack Fractional Knapsack ProblemProblemYour 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:1) A value/unit; let this be vi for item Ii.

2) Weight available; let this be wi for item Ii.

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.

Page 17: 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

Fractional Knapsack Fractional Knapsack ExampleExampleSay 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: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 I1.2)Take 25 lbs. of I3.3)Tale 21 lbs. of I2.

Value of knapsack = 4*50 + 25*40 + 21*30 = $1830.

Page 18: 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

Fractional Knapsack ProofFractional 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!

Page 19: 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

ReferencesReferencesSlides 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 WeissAdditional images:

www.wikipedia.comxkcd.com