greedy algorithms
play

Greedy Algorithms Chapter 16 1 Optimization Problems A class of - PowerPoint PPT Presentation

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


  1. Greedy Algorithms Chapter 16 1

  2. 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

  3. Example: Bin Packing 10 2 5 6 4 3 3

  4. Example: Bin Packing 8 5 6 4 3 2 4

  5. Example: Bin Packing 4 5 6 4 3 2 5

  6. Example: Bin Packing 1 3 5 6 4 2 6

  7. Example: Bin Packing 5 1 3 6 4 5 2 7

  8. Example: Bin Packing 5 4 1 3 4 6 5 2 Number of bins = 3 8

  9. Example: Bin Packing 2 4 3 6 5 (Optimal Solution) Number of bins = 2 9

  10. 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

  11. Activity Selection a.k.a. Task Scheduling 11

  12. 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

  13. Example of Activity Selection 13

  14. A Solution 14

  15. A Better Solution 15

  16. An Optimal (Best) Solution 16

  17. Another Optimal Solution 17

  18. β€œGreedy” Strategies Longest first Shortest first Early start first Early finish first … 18

  19. 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

  20. Early Finish 20

  21. Early Finish 21

  22. Early Finish 22

  23. Early Finish 23

  24. Early Finish 24

  25. Early Finish 25

  26. Early Finish 26

  27. Early Finish 27

  28. 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

  29. 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

  30. 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

  31. Knapsack Problem 31

  32. 0-1 Knapsack Problem $4,500  15LBs $1,500  3LBs $800  2LBs $3,000 $4,000  45LBs  10LBs  20LBs 32

  33. 0-1 Knapsack Problem $4,500  15LBs $1,500  3LBs $800  2LBs $3,000 $4,000  45LBs  10LBs  20LBs 33

  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 34

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. Greedy Choice Property β€² = Οƒ 𝑦 𝑗 β€²β€² β‡’ (both are valid solutions) Οƒ 𝑦 𝑗 π‘Š β€² = Οƒ 𝑦 𝑗 β€² 𝑧 𝑗 , π‘Š β€²β€² = Οƒ 𝑦 𝑗 β€²β€² 𝑧 𝑗 β€²β€² + 𝑧 𝑙 𝑦 𝑙 β€² + 𝑧 𝑙 𝑦 𝑙 β€²β€² βˆ’ 𝑧 π‘˜ 𝑦 π‘˜ π‘Š β€²β€² βˆ’ π‘Š β€² = 𝑧 π‘˜ 𝑦 π‘˜ β€² (All other items are the same) β€²β€² βˆ’ 𝑦 π‘˜ β€² βˆ’ 𝑧 𝑙 𝑦 𝑙 β€² βˆ’ 𝑦 𝑙 π‘Š β€²β€² βˆ’ π‘Š β€² = 𝑧 π‘˜ 𝑦 π‘˜ β€²β€² β€²β€² βˆ’ 𝑦 π‘˜ β€² = 𝑦 𝑙 β€² βˆ’ 𝑦 𝑙 β€²β€² = 𝑨 But, x j π‘Š β€²β€² βˆ’ π‘Š β€² = 𝑧 π‘˜ βˆ’ 𝑧 𝑙 𝑨 β€²β€² βˆ’ 𝑦 π‘˜ > 0 Since 𝑧 π‘˜ β‰₯ 𝑧 𝑙 and 𝑨 = 𝑦 π‘˜ π‘Š β€²β€² β‰₯ π‘Š β€² , hence, π‘Œ β€²β€² is also optimal 42

  43. Greedy Choice Illustration Ordered by 𝑧 π‘˜ π‘₯ 𝑇 π‘˜ Input Optimal answer that does not have π‘˜ π‘Œβ€² β€² the greedy choice β€² 𝑦 π‘˜ 𝑦 𝑙 Optimal answer π‘˜ π‘˜ π‘Œβ€²β€² that has the β€²β€² 𝑦 𝑙 greedy choice β€²β€² = 𝑦 π‘˜ 43 𝑦 π‘˜

  44. 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

  45. Optimal substructure Ordered by 𝑧 π‘˜ π‘₯ 𝑇 π‘˜ Input π‘Œβ€² π‘˜ π‘Œ Should be an optimal answer for 𝑇′ Optimal answer for 𝑇 𝑇′ Reduced problem 45

  46. 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

  47. Huffman Codes 47

  48. 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

  49. Example: Morse Code 49

  50. 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

  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

  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