CS4102 Algorithms Fall 2018 Warm up Given access to unlimited quantities of pennies, nickels dimes, and quarters, (worth value 1, 5, 10, 25 respectively), provide an algorithm which gives change for a given value π¦ using the fewest number of coins. 1
Change Making 43 cents 2
Change Making Algorithm β’ Given: target value π¦ , list of coins π· = [π 1 , β¦ , π π ] (in this case π· = [1,5,10,25] ) β’ Repeatedly select the largest coin less than the remaining target value: while( π¦ > 0 ) let π = max(π π β {π 1 , β¦ , π π } | π π β€ π¦) print π π¦ = π¦ β π 3
Why does this always work? β’ If π¦ < 5 , then pennies only β 5 pennies can be exchanged for a nickel Only case Greedy uses pennies! β’ If 5 β€ π¦ < 10 we must have a nickel β 2 nickels can be exchanged for a dime Only case Greedy uses nickels! β’ If 10 β€ π¦ < 25 we must have at least 1 dime β 3 dimes can be exchanged for a quarter and a Only case Greedy uses dimes! nickel β’ If π¦ β₯ 25 we must have at least 1 quarter 4 Only case Greedy uses quarters!
Warm up 2 Given access to unlimited quantities of pennies, nickels dimes, kims, and quarters, (worth value 1, 5, 10, 11, 25 respectively), give 90 cents change using the fewest number of coins. 5
Greedy solution 90 cents 6
Greedy solution 90 cents 7
Todayβs Keywords β’ Greedy Algorithms β’ Choice Function β’ Change Making β’ Interval Scheduling β’ Exchange Argument 8
CLRS Readings β’ Chapter 16 9
Homework β’ Hw6 Due Friday November 9, 11pm β Dynamic Programming and Greedy β Written assignment 10
Greedy vs DP β’ Dynamic Programming: β Require Optimal Substructure β Several choices for which small subproblem β’ Greedy: β Require Optimal Substructure β Must only consider one choice for small subproblem 11
Greedy Algorithms β’ Require Optimal Substructure β Solution to larger problem contains the solution to a smaller one β Only one subproblem to consider! β’ Idea: 1. Identify a greedy choice property β’ How to make a choice guaranteed to be included in some optimal solution 2. Repeatedly apply the choice property until no subproblems remain 12
Change Making Choice Property β’ Largest coin less than or equal to target value must be part of some optimal solution (for standard U.S. coins) 13
Interval Scheduling β’ Input: List of events with their start and end times (sorted by end time) β’ Output: largest set of non-conflicting events (start time of each event is after the end time of all preceding events) [2, 3.25] CS4102 [1, 4] Corn Maze Run [3, 4] CHS Prom [3.5, 4.75] DMB concert [4, 5.25] Bingo [4.5, 6] SCUBA lessons [5, 6.5] Roller Derby [7, 8] Pumpkin Carving 14
Interval Scheduling DP πΆππ‘π’(π’) = max # events that can be scheduled before time π’ πΆππ‘π’ s n + 1 Include event π πΆππ‘π’ e n = max πΆππ‘π’ e nβ1 Exclude event π π‘ π π πβ1 π‘ 1 π‘ 2 π‘ 3 π 2 π π 15
Greedy Interval Scheduling β’ Step 1: Identify a greedy choice property β Options: β’ Shortest interval β’ Fewest conflicts β’ Earliest start β’ Earliest end Prove using Exchange Argument 16
Interval Scheduling Algorithm Find event ending earliest, add to solution, Remove it and all conflicting events, Repeat until all events removed, return solution 17
Interval Scheduling Algorithm Find event ending earliest, add to solution, Remove it and all conflicting events, Repeat until all events removed, return solution 18
Interval Scheduling Algorithm Find event ending earliest, add to solution, Remove it and all conflicting events, Repeat until all events removed, return solution 19
Interval Scheduling Algorithm Find event ending earliest, add to solution, Remove it and all conflicting events, Repeat until all events removed, return solution 20
Interval Scheduling Run Time Find event ending earliest, add to solution, Remove it and all conflicting events, Repeat until all events removed, return solution Equivalent way StartTime = 0 For each interval (in order of finish time): π(π) if end of interval < Start Time: π(1) do nothing else: add interval to solution π(1) StartTime = end of interval 21
Exchange argument β’ Shows correctness of a greedy algorithm β’ Idea: β Show exchanging an item from an arbitrary optimal solution with your greedy choice makes the new solution no worse β How to show my sandwich is at least as good as yours: β’ Show: βI can remove any item from your sandwich, and it would be no worse by replacing it with the same item from my sandwichβ 22
Exchange Argument for Earliest End Time β’ Claim: earliest ending interval is always part of some optimal solution β’ Let πππ π,π be an optimal solution for time range [π, π] β’ Let π β be the first interval in [π, π] to finish overall β’ If π β β πππ π,π then claim holds β’ Else if π β β πππ π,π , let π be the first interval to end in πππ π,π β By definition π β ends before π , and therefore does not conflict with any other events in πππ π,π π,π β {π} + {π β } is also an optimal solution β Therefore πππ β Thus claim holds 23
Next Time: Caching Problem β’ Why is using too much memory a bad thing? 24
Von Neumann Bottleneck β’ Named for John von Neumann β’ Inventor of modern computer architecture β’ Other notable influences include: β Mathematics β Physics β Economics β Computer Science 25
Von Neumann Bottleneck β’ Reading from memory is VERY slow β’ Big memory = slow memory β’ Solution: hierarchical memory β’ Takeaway for Algorithms: Memory is time, more memory is a lot more time Hope itβs not here If not look here Hopefully your data in here CPU, Disc Cache registers Access time: Access time: Access time: 1 cycle 1,000,000 cycles 26 10 cycles
Exchange Argument for (U.S. coin) Change Making β’ Claim: largest coin is always part of some optimal solution β’ Consider that we had an optimal solution for target value π¦ , call this πππ π¦ β’ Let π β be the largest coin in πππ π¦ β’ If π β β πππ π¦ then claim holds β’ Else if π β β πππ π¦ , let π be the largest coin in πππ π¦ β Using slide 4, we could exchange π for a combination of coins including π β to have a solution that is no worse 27
Recommend
More recommend