Greedy Algorithms - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Greedy Algorithms

Description:

... 311 9:30pm-10:30pm: Coldplay 11pm-2am: The Killers Single Room Scheduling Problem Example: The Greedy Solution: Sort the requests by finish time. – PowerPoint PPT presentation

Number of Views:139
Avg rating:3.0/5.0
Slides: 20
Provided by: Sar273
Learn more at: http://www.cs.ucf.edu
Category:

less

Transcript and Presenter's Notes

Title: Greedy Algorithms


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 Kruskals and
    Prims
  • Dijkstras 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 youve 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 doesnt 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 doesnt divide into 25, there
    are no values, multiples of 25, for which its
    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 youve 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
  • 730-930 Gorillaz
  • 9-11pm 311
  • 930pm-1030pm 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
  • 730-930pm Gorillaz
  • 930pm-1030pm Coldplay
  • 9-11pm 311
  • 11pm-2am The Killers

11pm 2am
The Killers
930pm-1030pm
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
  • 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
  • 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.
  • 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.

ROOM 1
7-10am CS 1
ROOM 2
7-10am Intro to C
ROOM 3
9am-12pm OOP
10am-1pm OS
12pm-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 450 2540 2130 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 Guhas 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
Write a Comment
User Comments (0)
About PowerShow.com