greedy algorithms
play

Greedy Algorithms Pedro Ribeiro DCC/FCUP 2018/2019 Pedro Ribeiro - PowerPoint PPT Presentation

Greedy Algorithms Pedro Ribeiro DCC/FCUP 2018/2019 Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 1 / 47 Greedy Algorithms A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal


  1. Greedy Algorithms Pedro Ribeiro DCC/FCUP 2018/2019 Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 1 / 47

  2. Greedy Algorithms A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. Greedy Algorithm At each step choose the ”best” local choice Never look ”behind” or change any decisions already made Never look to the ”future” to check if our decision has negative consequences Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 2 / 47

  3. Greedy Algorithms A first example Coin Change Problem (Cashier’s Problem) Input: a set of coins S and a quantity K we want to create with the coins Output: the minimum number of coins to make the quantity K (we can repeat coins) Input/Output Example Input: S = { 1 , 2 , 5 , 10 , 20 , 50 , 100 , 200 } (we have an infinite supply of each coin) K = 42 Output: 3 coins (20 + 20 + 2) Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 3 / 47

  4. Coin Change Problem A greedy algorithm for the coin change problem In each step choose the largest coin that we will not take us past quantity K Examples (with S = { 1 , 2 , 5 , 10 , 20 , 50 , 100 , 200 } ): K = 35 ◮ 20 (total: 20) + 10 (total: 30) + 5 (total: 35)[3 coins] K = 38 ◮ 20 + 10 + 5 + 2 + 1 [5 coins] K = 144 ◮ 100 + 20 + 20 + 2 + 2 [5 coins] K = 211 ◮ 200 + 10 + 1 [3 coins] Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 4 / 47

  5. Coin Change Problem Does this algorithm always give the minimum amount of coins? For the common money systems (ex: euro, dollar)... yes! For a general coin set... no! Examples: S = { 1 , 2 , 5 , 10 , 20 , 25 } , K = 40 ◮ Greedy gives 3 coins (25 + 10 + 5), but it is possible to use 2 (20 + 20) S = { 1 , 5 , 8 , 10 } , K = 13 ◮ Greedy gives 4 coins (10 + 1 + 1 + 1), but it is possible to use 2 (5 + 8) (Will it be enough that a single coin is larger than the double of the previous coin?) S = { 1 , 10 , 25 } , K = 40 ◮ Greedy gives 7 coins (25 + 10 + 1 + 1 + 1 + 1 + 1), but is is possible to only use 4 coins (10 + 10 + 10 + 10) Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 5 / 47

  6. Greedy Algorithms ”Simple” idea, but it does not always work ◮ Depending on the problem, it may or may note give an optimal answer Normally, the running time is very low (ex: linear or linearithmic) The hard part is to prove optimality Typically is it applied in optimization problems ◮ Find the ”best” solution among all possible solutions, according to a given criteria (goal function) ◮ Generally it involves finding a minimum or a maximum A very common pre-processing step is... sorting ! Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 6 / 47

  7. Properties needed for a greedy approach to work Optimal Substructure When the optimal solution of a problem contains in itself solutions for subproblems of the same type Example Let min ( K ) be the minimum amount of coins to make quantity K . If that solution uses a coin of value v , then the remaining coins to use are given precisely by min ( K − v ). If a problem presents this characteristic, we say it respects the optimality principle . Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 7 / 47

  8. Properties needed for a greedy approach to work Greedy Choice Property An optimal solution is consistent with the greedy choice of the algorithm. Example In the case of euro coins, there is an optimal solution using the largest coin which is still smaller or equal than the quantity we need to make. Proving this property is normally the ”hardest” part Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 8 / 47

  9. Cashier’s Problem: Proof Let H = { h 1 , h 2 , h 5 , h 20 , h 50 , h 100 , h 200 } be an optimal solution with h v coins of each value v If h 100 > 1, H would not be optimal (we could just substitute two 100 coins by one of 200). Therefore, h 100 ≤ 1 Using the same reasoning, h 50 ≤ 1, h 10 ≤ 1, h 5 ≤ 1 and h 1 ≤ 1 If h 20 > 2, H would not be optimal (we could just substitute three 20 coins by one of 50 and one of 10). Therefore, h 20 ≤ 2 (and h 2 ≤ 2) h 2 = 2 and h 1 = 1 can’t happen at the same time (we could just use a 5 coin instead). Therefore, 2 h 2 + h 1 ≤ 4 (and 20 h 20 + 10 h 10 ≤ 40) Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 9 / 47

  10. Cashier’s Problem: Proof We have: ◮ h 1 ≤ 1 ◮ h 2 ≤ 2 (and 2 h 2 + h 1 ≤ 4) ◮ h 5 ≤ 1 ◮ h 10 ≤ 1 ◮ h 20 ≤ 2 (and 20 h 20 + 10 h 10 ≤ 40) ◮ h 50 ≤ 1 ◮ h 100 ≤ 1 Combining what was said before: ◮ 5 h 5 + 2 h 2 + h 1 ≤ 9 ◮ 10 h 10 + 5 h 5 + 2 h 2 + h 1 ≤ 19 ◮ 20 h 20 + 10 h 10 + 5 h 5 + 2 h 2 + h 1 ≤ 49 ◮ 50 h 50 + 20 h 20 + 10 h 10 + 5 h 5 + 2 h 2 + h 1 ≤ 99 ◮ 100 h 100 + 50 h 50 + 20 h 20 + 10 h 10 + 5 h 5 + 2 h 2 + h 1 ≤ 199 Let V = { 1 , 2 , 5 , 10 , 20 , 50 , 100 } . We have that � k i =1 v i h i < v k +1 . Therefore, H has the same number of coins as our greedy solution! Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 10 / 47

  11. Fractional Knapsack Fractional Knapsack Problem Input: A backpack of capacity C A set of n materials, each one with weight w i and value v i Output: The allocation of materials to the backpack that maximizes the transported value. The materials can be ”broken” in smaller pieces, that is, we can decide to take only quantity x i of object i , with 0 ≤ x i ≤ 1. What we want is therefore to obey the following constraints The materials fit in the backpack ( � x i w i ≤ C ) i The value transported is the maximum possible (maximize � x i v i ) i Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 11 / 47

  12. Fractional Knapsack Input Example Input: 5 objects and C = 100 i 1 2 3 4 5 w i 10 20 30 40 50 v i 20 30 66 40 60 What is the optimal answer in this case? Always choose the material with the largest value: i 1 2 3 4 5 x i 0 0 1 0.5 1 This would give a total weight of 100 and a total value of 146. Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 12 / 47

  13. Fractional Knapsack Input Example Input: 5 objects e C = 100 i 1 2 3 4 5 w i 10 20 30 40 50 v i 20 30 66 40 60 What is the optimal answer in this case? Always choose the material with the smallest weight: i 1 2 3 4 5 x i 1 1 1 1 0 This would give a total weight of 100 and a total value of 156. Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 13 / 47

  14. Fractional Knapsack nput Example Input: 5 objects e C = 100 i 1 2 3 4 5 w i 10 20 30 40 50 v i 20 30 66 40 60 What is the optimal answer in this case? Always choose the material with the largest value / weigth ratio: i 1 2 3 4 5 v i / w i 2 1.5 2.2 1.0 1.2 x i 1 1 1 0 0.8 This would give a total weight of 100 and a total value of 164. Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 14 / 47

  15. Fractional Knapsack Theorem Always choosing the largest possible quantity of the material with the largest value / weigth ratio is a strategy leading to an optimal total value. 1) Optimal Substructure Consider an optimal solution and its material m with the best ratio. If we remove it from the backpack, then the remaining objects must contain the optimal solution for the materials othen than m and for a backpack with capacity C − x m w m If that is not the case, then the initial solution was also not optimal! Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 15 / 47

  16. Fractional Knapsack Theorem Always choosing the largest possible quantity of the material with best value / weigth ratio is a strategy that gives an optimal value 2) Greedy Choice Property We want to prove that the largest possible quantity of the material m with the best ratio ( v m / w m ) should be included in the backpack. The value of the backpack: value = � x i v i . i Let q i = x i w i be the quantity of material i : value = � q i v i / w i i If we still have some material m available, then swapping any other material i with m will give origin to a better total value: q m v m / w m ≥ q i v i / w i (by definition of m ) Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 16 / 47

  17. Fractional Knapsack Greedy Algorithm for Fractional Knapsack Sort the materials by decreasing order of value / weigth ratio Process the next material in the sorted list: ◮ If it fits entirely on the backpack, include it all and continue to the next material ◮ If it does not fit entirely, include the largest possible quantity and terminate Temporal Complexity: Sorting: O ( n log n ) Processing: O ( n ) Total: O ( n log n ) Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 17 / 47

  18. Interval Scheduling Interval Scheduling Problem Input: A set of n activities, each one starting on time s i and finishing on time f i . Output: Largest possible quantity of activities without overlapping Two intervals i and j overlap if there is a time k where both are active. Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 18 / 47

  19. Interval Scheduling Input Example Input: 5 activities: i 1 2 3 4 5 s i 1 2 4 4 5 f i 7 5 6 9 10 Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 19 / 47

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend