Greedy Algorithms Chapter 16 1
Optimization Problems A class of problems in which we are asked to find a set (or a sequence ) of β items β that satisfy some constraints and simultaneously optimize (i.e., maximize or minimize ) some objective function 2
Example: Bin Packing 10 2 5 6 4 3 3
Example: Bin Packing 8 5 6 4 3 2 4
Example: Bin Packing 4 5 6 4 3 2 5
Example: Bin Packing 1 3 5 6 4 2 6
Example: Bin Packing 5 1 3 6 4 5 2 7
Example: Bin Packing 5 4 1 3 4 6 5 2 Number of bins = 3 8
Example: Bin Packing 2 4 3 6 5 (Optimal Solution) Number of bins = 2 9
The Greedy Method Applied to optimization problems Adds items to the solution one-by-one Builds up towards the final solution No backtracking Not necessarily optimal! 10
Activity Selection a.k.a. Task Scheduling 11
Activity Selection Problem Given a set of activities π = {π 1 , π 2 , β¦ , π π } where each activity π has a start time π‘ π and a finish time π π , where 0 β€ π‘ π < π π < β . An activity π π happens in the half-open time interval [π‘ π , π π ) . Two activities are said to be compatible if they do not overlap . The problem is to find a maximum-size compatible subset , i.e., a one with the maximum number of activities. 12
Example of Activity Selection 13
A Solution 14
A Better Solution 15
An Optimal (Best) Solution 16
Another Optimal Solution 17
βGreedyβ Strategies Longest first Shortest first Early start first Early finish first β¦ 18
Early Finish Greedy Strategy 1. Sort activities by finish time 2. Schedule the first activity 3. Remove all incompatible activities 4. If there are more activities, repeat 2 19
Early Finish 20
Early Finish 21
Early Finish 22
Early Finish 23
Early Finish 24
Early Finish 25
Early Finish 26
Early Finish 27
Optimality of the Greedy Choice To prove optimality of the greedy choice, we have to prove the following two properties 1. Greedy Choice: The greedy choice is part of the answer 2. Optimal Substructure: The optimal solution to the big problem contains the optimal solution to the sub-problem 28
Greedy Choice Let π΅ β π be an optimal solution. Let π π be the first element in π΅ and π π be the first element in π . We want to prove that π π is part of an optimal solution. If π π = π π then we are done Otherwise, we prove that there is another optimal solution π΅` = π΅ β π π βͺ π π Is π΅` a solution? yes Is π΅` optimal? yes 29
Optimal Substructure We want to prove that, if π΅ β π is an optimal solution to π , then π΅ β π π is an optimal solution to S` = π π : π π β π β§ π‘ π > π π Proof by contradiction Assume that π΅ β π π is not optimal Then, there is another solution πΆ to π` such that πΆ > π΅ β π π β |πΆ| β₯ |π΅| If this is the case, then the subset π΅` = π π βͺ πΆ is also a solution to π π΅` > πΆ β |π΅`| > π΅ , which means that π΅ is not optimal which is a contradiction 30
Knapsack Problem 31
0-1 Knapsack Problem $4,500 ο 15LBs $1,500 ο 3LBs $800 ο 2LBs $3,000 $4,000 ο 45LBs ο 10LBs ο 20LBs 32
0-1 Knapsack Problem $4,500 ο 15LBs $1,500 ο 3LBs $800 ο 2LBs $3,000 $4,000 ο 45LBs ο 10LBs ο 20LBs 33
0-1 Knapsack Problem $4,500 ο 15LBs $300/LB $1,500 ο 3LBs $800 $500/LB ο 2LBs $400/LB $3,000 $4,000 ο 45LBs ο 10LBs ο 20LBs $300/LB $200/LB 34
0-1 Knapsack Problem $4,500 ο 15LBs $300/LB $1,500 ο 3LBs $800 $500/LB ο 2LBs $400/LB $3,000 $4,000 ο 45LBs ο 10LBs ο 20LBs $300/LB $200/LB Total value = $9,800 35
0-1 Knapsack Problem $4,500 ο 15LBs $300/LB $1,500 ο 3LBs There isnβt a known solution to $800 the 0-1 Knapsack problem using $500/LB ο 2LBs a greedy algorithm $400/LB $3,000 $4,000 ο 45LBs ο 10LBs ο 20LBs $300/LB $200/LB Total value = $11,500 36
Fractional Knapsack Problem $4,500 ο 15 oz $300/oz $1,500 ο 3 oz $800 $500/oz ο 2 oz $400/oz $3,000 $4,000 ο 45 oz ο 10oz ο 20oz $300/oz $200/oz 37
Problem Formulation Given a set π[1. . π] of items where each item π has a weight π₯ π and a value π€ π , we would like to find the amount π¦ π of each item that we can take to maximize the total value π π¦ π π = ΰ· π€ π π₯ π π=1 Under the following two constraints 0 β€ π¦ π β€ π₯ π π Ο π=1 π¦ π β€ π 38
Pseudo-code Fractional-Knapsack π€ π Compute the value-per-weight π§ π = π₯ π for all items Sort items by π§ π Set π = 0 While ( π < π ) Select the item π with the highest π§ π Set π¦ π = πππ π β π, π₯ π Remove item π Set π = π + π¦ π Set all remaining π¦ π βs to 0 39
Greedy-choice Property Given the set π ordered by the value-per- weight ( π§ ), taking as much as possible π¦ π from the item π with the highest value-per- weight will lead to an optimal solution Assume there is an optimal solution πβ² where we take less amount of item π , say π¦ π β² < π¦ π . We prove that there is another solution πβ²β² where we take π¦ π of item π and get a similar or a higher total value π 40
Greedy-choice Property Since π¦ π β² < π¦ π , there must be another item π which was taken in an amount that accounts for the difference, i.e., π¦β² π We create another solution πβ²β² by doing the following changes in πβ² Reduce the amount of item π by a value π¨ β²β² = π¦ π β² β π¨ π¦ π Increase the amount of item π by a value π¨ β²β² = π¦ π β² + π¨ = π¦ π ( π¦ π is the greedy choice) π¦ π 41
Greedy Choice Property β² = Ο π¦ π β²β² β (both are valid solutions) Ο π¦ π π β² = Ο π¦ π β² π§ π , π β²β² = Ο π¦ π β²β² π§ π β²β² + π§ π π¦ π β² + π§ π π¦ π β²β² β π§ π π¦ π π β²β² β π β² = π§ π π¦ π β² (All other items are the same) β²β² β π¦ π β² β π§ π π¦ π β² β π¦ π π β²β² β π β² = π§ π π¦ π β²β² β²β² β π¦ π β² = π¦ π β² β π¦ π β²β² = π¨ But, x j π β²β² β π β² = π§ π β π§ π π¨ β²β² β π¦ π > 0 Since π§ π β₯ π§ π and π¨ = π¦ π π β²β² β₯ π β² , hence, π β²β² is also optimal 42
Greedy Choice Illustration Ordered by π§ π π₯ π π Input Optimal answer that does not have π πβ² β² the greedy choice β² π¦ π π¦ π Optimal answer π π πβ²β² that has the β²β² π¦ π greedy choice β²β² = π¦ π 43 π¦ π
Optimal Sub-structure Given the problem π with an optimal solution π , we want to prove that the solution π` = π β π¦ π is optimal to the problem π` after removing the item π and updating the capacity π` = π β π¦ π Proof by contradiction Assume that π` is not optimal to π` There is another solution π`` to π` that has a higher total value π`` > π` This means we can have a better solution to the problem π which is impossible because π is optimal 44
Optimal substructure Ordered by π§ π π₯ π π Input πβ² π π Should be an optimal answer for πβ² Optimal answer for π πβ² Reduced problem 45
Optimal Substructure Input problem π with an optimal answer π of total value π π` = π β π¦ π , π` = π β π¦ π π§ π Assume π`` is an optimal solution to π` π`` = Οπ¦`` π β€ π β π¦ π and π`` > π` π`` + π¦ π is a valid solution to π because π`` + π¦ π β€ π π`` = π`` + π¦ π π§ π > π β² + π¦ π π§ π > V This is a contradiction with the assumption that π is an optimal solution for π 46
Huffman Codes 47
Encoding How data is represented? Fixed-size codes, e.g., ASCII A: 1000001 B: 1000010 Variable-size codes, e.g., Morse Codes A: ββ¬ B: β¬βββ E: β T: β¬ 48
Example: Morse Code 49
Prefix Codes No code is allowed to be a prefix of another code To encode, simply concatenate all the codes Decoding does not entail any ambiguity Example: Message βJAVAβ a = β0β, j = β11β, v = β10β Encoded message β110100β Decoding β 110100 β 51
Trie We can use a trie to find prefix codes the characters are stored at the external nodes a left child (edge) means 0 a right child (edge) means 1 A = 010 0 1 B = 11 0 1 0 1 C = 00 D B C D = 10 0 1 A R R = 011 52
Example of Decoding encoded text: 01011011010000101001011011010 text: ABRACADABRA ASCII: 88 bits Our encoding: 29 bits A = 010 0 1 B = 11 0 1 0 1 C = 00 D B C D = 10 0 1 A R R = 011 53
Recommend
More recommend