The Making Change Problem (MCP) A “greedy” algorithm Greedy Algorithms // Input: a , an amount // Output: a list of coins giving DPV Chapter 5, Part 1 The MCP // a solution to the MCP for a C ← { 1.00, 0.50, 0.25, 0.10, 0.05, 0.01 } Given: an amount a ans ← the empty list Jim Royer while a > 0 do Find: the smallest collection of coins if ( a < min ( C )) that is worth amount a . then return the empty list EECS Example: For a = 0.28 the (unique) // Indicates no Solution answer is 0.25, 0.01, 0.01, 0.01. else c ← max ( { c ∈ C : c ≤ a } ) February 28, 2019 add c to the ans list a ← a − c return ans (Unless otherwise credited, all images are from DPV.) Fact: US coins have denominations: 1.00, 0.50, 0.25, 0.10, 0.05, and 0.01. Royer Greedy Algorithms Royer Greedy Algorithms Making Change Making Change Fiction: Martian coins have denoms: 1.00, 0.50, 0.25, 0.14, and 0.01. Example: For a = 0.28 the (unique) answer is 0.14, and 0.14. A greedy “algorithm” // Input: a , an amount // Output: a list of coins that may or may not be a solution to the MCP for a C ← { 1.00, 0.50, 0.25, 0.14, 0.01 } ans ← the empty list while a > 0 do if ( a < min ( C )) then return the empty list else c ← max ( { c ∈ C : c ≤ a } ) add c to the ans list a ← a − c return ans Problem: The above returns the wrong answer for a = 28. Problem: How do we know the previous version is correct? Diagram from http://en.wikipedia.org/wiki/Greedy_algorithm Royer Greedy Algorithms Royer Greedy Algorithms
Standard Features of Greedy Algorithms Event Scheduling, 1 Greedy algorithms generally (but not always) have the following features: The Meeting Scheduling Problem (MSP) Given: ( s 1 , f 1 ) , . . . , ( s k , f k ) where, for each i , ( s i , f i ) represents a meeting ◮ The goal is to build an optimal C ← { 1.00, 0.50, 0.25, 0.14, 0.01 } ; ans ← [ ] starting at time s i and finishing at time f i with 0 ≤ s i < f i for each i . solution to a problem by selecting while a > 0 do items from a candidate set. Goal: Schedule as many of non-overlapping meetings as possible. if ( a < min ( C )) then return [ ] ◮ A feasibility function is used to else c ← max ( { c ∈ C : c ≤ a } ) Definition check whether a candidate can be ans ← cons ( c , ans ) ; a ← a − c return ans used in the solution Two meetings overlap when their intervals intersect, i.e., ( s i , f i ) ∩ ( s j , f j ) � = ∅ . ◮ candidate set = C ◮ A greedy choice function picks a ? “best” candidate to be added to ◮ feasibility ≡ [coin value ≤ a ] the solution. ◮ greedy choice ≡ c ← max ( { c ∈ C : c ≤ a } ) ◮ An objective function assigns a ◮ objective function ≡ the number of value to partial solutions. coins used (minimize) ◮ A solution function tests whether ◮ solution function ≡ [ a ? = 0 ] A maximal conflict-free schedule for a set of classes. we have a complete solution yet. Image from: http://www.cs.uiuc.edu/class/fa05/cs473g/lectures/06-greedy.pdf Royer Greedy Algorithms Royer Greedy Algorithms Event Scheduling, 2 Event Scheduling, 3 function greedySch( L : a list of events) The Meeting Scheduling Problem (MSP) Sort L so that f 1 ≤ · · · ≤ f k S ← ∅ ; last ← 0 Given: ( s 1 , f 1 ) , . . . , ( s k , f k ) where, for each i , ( s i , f i ) represents a meeting for i ← 1 to k do starting at time s i and finishing at time f i with 0 ≤ s i < f i for each i . if last ≤ s i then S ← S ∪ { ( s i , f i ) } ; last ← f i Goal: Schedule as many of non-overlapping meetings as possible. return S Definition Two meetings overlap when their intervals intersect, i.e., ( s i , f i ) ∩ ( s j , f j ) � = ∅ . ◮ candidate set = { ( s 1 , f 1 ) , . . . , ( s k , f k ) } . ◮ S is a partial solution ≡ S is a set of non-overlapping meetings ◮ ( s i , f i ) is feasible for S ⇐ ⇒ ( s i , f i ) does not overlap with anything in S . ◮ solution ≡ no more meetings can be added to S ◮ objective function ≡ the size of S (to be maximized) ◮ greedy choice ≡ shortest meeting?, earliest starting time?, latest The same classes sorted by finish times and the greedy schedule. finishing time?, fewest overlaps?, earliest finishing time! Image from: http://www.cs.uiuc.edu/class/fa05/cs473g/lectures/06-greedy.pdf Royer Greedy Algorithms Royer Greedy Algorithms
Event Scheduling, 4 Event Scheduling, 5 Strategy: Turn S ′′ into S ′ . function greedySch( L : a list of events) function greedySch( L : a list of events) Suppose f ′ 1 ≤ · · · ≤ f ′ m and f ′′ 1 ≤ · · · ≤ f ′′ n . Sort L so that f 1 ≤ · · · ≤ f k Sort L so that f 1 ≤ · · · ≤ f k Strategy: Turn S ′′ into S ′ . For ℓ = 1, . . . , m , let S ′′ ℓ = S ← ∅ ; last ← 0 S ← ∅ ; last ← 0 { ( s ′ 1 , f ′ 1 ) , . . . , ( s ′ ℓ , f ′ ℓ ) , ( s ′′ ℓ + 1 , f ′′ ℓ + 1 ) , . . . , ( s ′′ m , f ′′ m ) } . for i ← 1 to k do Suppose f ′ 1 ≤ · · · ≤ f ′ m and f ′′ 1 ≤ · · · ≤ f ′′ for i ← 1 to k do n . We want to show each S ′′ if last ≤ s i then if last ≤ s i then ℓ is feasible. For ℓ = 1, . . . , m , let S ′′ ℓ = S ← S ∪ { ( s i , f i ) } ; last ← f i S ← S ∪ { ( s i , f i ) } ; last ← f i { ( s ′ 1 , f ′ 1 ) , . . . , ( s ′ ℓ , f ′ ℓ ) , ( s ′′ ℓ + 1 , f ′′ ℓ + 1 ) , . . . , ( s ′′ m , f ′′ m ) } . Induction step Case return S // Runtime: ?? return S // Runtime: ?? We want to show each S ′′ ℓ is feasible. IH: S ′′ ℓ − 1 is feasible, ( 1 <ℓ ≤ m ) . Lemma Base Case Lemma Convention: s ′′ n + 1 = ∞ . greedySch returns a schedule with as Claim 1: f ′ 1 ≤ f ′′ greedySch returns a schedule with as Claim 3: f ′ ℓ ≤ f ′′ 1 . ℓ . Proof: By greedySch ’s choice, f ′ Proof: Since S ′′ ℓ − 1 is consistent, s ′′ ℓ ≥ f ′ many events as possible. many events as possible. 1 = the ℓ − 1 . By smallest finishing time. Hence, f ′ 1 ≤ f ′′ 1 . greeySch ’s greedy choice, f ′ ℓ is the smallest Proof. Suppose Proof. Suppose finishing time with a corresponding starting Claim 2: S ′′ 1 is feasible. ◮ S ′ = { ( s ′ ◮ S ′ = { ( s ′ 1 , f ′ 1 ) , . . . , ( s ′ m , f ′ 1 , f ′ 1 ) , . . . , ( s ′ m , f ′ time ≥ f ′ ℓ − 1 . Hence, f ′ ℓ ≤ f ′′ m ) } is the m ) } is the ℓ . Proof: Since f ′ 1 ≤ f ′′ 1 (Claim 1) and f ′′ 1 ≤ s ′′ 2 schedule greedySch returns. ( S ′′ has no overlaps), f ′ 1 ≤ s ′′ 2 . Hence, S ′′ schedule greedySch returns. 1 has Claim 4: S ′′ ℓ is feasible. ◮ S ′′ = { ( s ′′ ◮ S ′′ = { ( s ′′ 1 , f ′′ 1 ) , . . . , ( s ′′ n , f ′′ no overlaps. 1 , f ′′ 1 ) , . . . , ( s ′′ n , f ′′ n ) } is n ) } is Proof: Since f ′ ℓ ≤ f ′′ ℓ (Claim 3) & f ′′ ℓ ≤ s ′′ ℓ + 1 an optimal schedule. (So m ≤ n .) an optimal schedule. (So m ≤ n .) ( S ′′ ℓ has no overlaps), f ′ ℓ ≤ s ′′ ℓ + 1 . Hence, S ′′ Goal: Show m = n . ℓ + 1 has no overlaps. Goal: Show m = n . Royer Greedy Algorithms Royer Greedy Algorithms Event Scheduling, 6 Knapsack, 1 function greedySch( L : a list of events) The Knapsack Problem (KP) Strategy: Turn S ′′ into S ′ . Sort L so that f 1 ≤ · · · ≤ f k Suppose f ′ 1 ≤ · · · ≤ f ′ m and f ′′ 1 ≤ · · · ≤ f ′′ S ← ∅ ; last ← 0 n . Given: for i ← 1 to k do For ℓ = 1, . . . , m , let S ′′ ℓ = ◮ A knapsack with capacity W kgs. if last ≤ s i then { ( s ′ 1 , f ′ 1 ) , . . . , ( s ′ ℓ , f ′ ℓ ) , ( s ′′ ℓ + 1 , f ′′ ℓ + 1 ) , . . . , ( s ′′ m , f ′′ m ) } . ◮ Items 1, . . . , n S ← S ∪ { ( s i , f i ) } ; last ← f i We proved: S ′′ return S // Runtime: ?? ◮ Item i has weight w i and value v i . m is feasible. Find: S ⊆ { 1, . . . , n } so that Final step Lemma ◮ ∑ i ∈ S w i ≤ W and Suppose by way of contradiction that m < n . greedySch returns a schedule with as ◮ ∑ i ∈ S v i is maximized. Since S ′′ m is feasible, ( s ′′ m + 1 , f ′′ many events as possible. m + 1 ) does not overlap with any of ( s ′ 1 , f ′ 1 ) , . . . , ( s ′ m , f ′ m ) . Proof. Suppose Greedy Heuristic But then by greedySch ’s greedy choice, the ◮ S ′ = { ( s ′ 1 , f ′ 1 ) , . . . , ( s ′ m , f ′ m ) } is the algorithm would have chosen at least one Order items so that v 1 / w i ≥ · · · ≥ v n / w n schedule greedySch returns. more event to add to S , a contradiction. cap ← W ; S ← ∅ ◮ S ′′ = { ( s ′′ 1 , f ′′ 1 ) , . . . , ( s ′′ n , f ′′ for i ← 1 to n do n ) } is Therefore, m = n and S ′ (= S ′′ m ) is optimal. if w i ≤ cap then S ← S ∪ { i } Image from: an optimal schedule. (So m ≤ n .) ✷ http://commons.wikimedia.org/wiki/File:Knapsack.svg cap ← cap − w i Goal: Show m = n . return S Royer Greedy Algorithms Royer Greedy Algorithms
Recommend
More recommend