class: center, title-slide
## CSCI-UA 480: APS ## Algorithmic Problem Solving
## Greedy Algorithms .author[ Instructor: Joanna Klukowska
] .license[ Copyright 2020 Joanna Klukowska. Unless noted otherwise all content is released under a
[Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/).
Background image by Stewart Weiss
] --- layout:true template: default name: section class: inverse, middle, center --- layout:true template: default name: challenge class: challenge --- layout:true template: default name: poll class: inverse, full-height, center, middle --- layout:true template: default name: breakout class: breakout --- layout:true template:default name:slide class: slide .bottom-left[© Joanna Klukowska. CC-BY-SA.] --- ## Greedy - solving problems by making locally (at each step) optimal choices with the hope of obtaining optimal solution overall - when does it work - problem has optimal sub-structure: optimal solution for the problem contains optimal solutions for sub-problems - problem has a greedy property: optimal choices made at the moment lead to otpimal overall solution (no need to go back and explore an alternative) - examples --- class:challenge ## Challenge: Making Change __Task__ Given an amount V (in cents) and a list of denominations of n coins find the way to make the change with the minimum number of coins. Assume that you have unlimited supply of each coin (for each given denomination). --- class:challenge ## Challenge: Making Change __Example__ - n = 4 - denominations = {25, 10, 5, 1} cents - desired amount V = 47 cents solution: - start with the largest denomination and use as many coins as possible
47 - 25 = 22 - use the next highest denomination
22 - 10 = 12
12 - 10 = 2
- the remaining amount is too small for the denomination of 5, so skip it - use the last denomination
2 - 1 = 1
2 - 1 = 0
so we need 5 coins: 25, 10, 10, 1, 1 --- ## Challenge: Making Change This problem posseses both characteristics that make the greedy solution applicable here: - the solutions to its subproblems are optimal, for example - for 22 cents, the otpimal solution would be 4 coins: {10, 10, 1, 1} - for 12 cents, the optimal solution would be 3 coins: {10, 1, 1} - the solution has a greedy property: we use the largest possible coin first, if we used a smaller one instead, we would have to replace that coin with more than one coin in the alternative solution. -- _Note_
This approach does not work for all denominations. It does work for American coins of {25, 10, 5, 1} cents. --- class:challenge ## Computing Center Load Balancing You are an operator of a super computing center and in control of M nodes. One day, a research institute from Light Kingdom submitted N computational tasks. Given the computational power needed for each task, you are to distribute the tasks among the available nodes. Restriction: every node can process up to two tasks. You also want to distribute the workload as evenly as possible, i.e. minimize the following imbalance value $$Imbalance = \sum_{i=0}^{M-1}{|Avg-Load_i|} $$ where $Avg$ is average load per node and $Load_i$ is the load of the i-th node. __Input__: The first line of the input contains two integers M (1 <= M <= 5) and N (1 <= N <= 2M), indicating the number of nodes you control and the number of tasks, respectively. The second line contains N integers, each of which represents the computational power required for a task. Numbers on this line are between 1 and 1000. __Output__: Print one line `IMBALANCE = I` where `I` is the minimum imbalance value rounded to 5 decimal places. --- ## Computing Center Load Balancing You are an operator of a super computing center and in control of M nodes. One day, a research institute from Light Kingdom submitted N computational tasks. Given the computational power needed for each task, you are to distribute the tasks among the available nodes. Restriction: every node can process up to two tasks. You also want to distribute the workload as evenly as possible, i.e. minimize the following imbalance value $$Imbalance = \sum_{i=0}^{M-1}{|Avg-Load_i|} $$ where $Avg$ is average load per node and $Load_i$ is the load of the i-th node. __Example 1__ ``` Input: 2 3 6 3 8 Output: IMBALANCE = 1.00000 ``` __Example 2__ ``` Input: 3 5 51 19 27 14 33 Output: IMBALANCE = 6.00000 ``` __Example 3__ ``` Input: 5 9 1 2 3 5 7 11 13 17 19 Output: IMBALANCE = 11.60000 ``` --- __Observation 1__: If there is an empty computing node in the final solution, than it is always beneficial (in the sense that the new solution is not worse than the original) to move one task from a computing node with two tasks to the empty one. Otherwise, the empty computing node contributes more to the imbalance than the alternative assignment. __Observation 2__: If N > M, then N-M tasks must be scheduled on computing nodes that already contain other tasks.