Greedy Algorithms
Pedro Ribeiro
DCC/FCUP
2018/2019
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 1 / 47
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
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 1 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 2 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 3 / 47
◮ 20 (total: 20) + 10 (total: 30) + 5 (total: 35)[3 coins]
◮ 20 + 10 + 5 + 2 + 1 [5 coins]
◮ 100 + 20 + 20 + 2 + 2 [5 coins]
◮ 200 + 10 + 1 [3 coins] Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 4 / 47
◮ Greedy gives 3 coins (25 + 10 + 5), but it is possible to use 2 (20 + 20)
◮ Greedy gives 4 coins (10 + 1 + 1 + 1), but it is possible to use 2 (5 + 8)
◮ Greedy gives 7 coins (25 + 10 + 1 + 1 + 1 + 1 + 1),
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 5 / 47
◮ Depending on the problem, it may or may note give an optimal answer
◮ Find the ”best” solution among all possible solutions, according to a
◮ Generally it involves finding a minimum or a maximum
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 6 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 7 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 8 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 9 / 47
◮ h1 ≤ 1 ◮ h2 ≤ 2 (and 2h2 + h1 ≤ 4) ◮ h5 ≤ 1 ◮ h10 ≤ 1 ◮ h20 ≤ 2 (and 20h20 + 10h10 ≤ 40) ◮ h50 ≤ 1 ◮ h100 ≤ 1
◮ 5h5 + 2h2 + h1 ≤ 9 ◮ 10h10 + 5h5 + 2h2 + h1 ≤ 19 ◮ 20h20 + 10h10 + 5h5 + 2h2 + h1 ≤ 49 ◮ 50h50 + 20h20 + 10h10 + 5h5 + 2h2 + h1 ≤ 99 ◮ 100h100 + 50h50 + 20h20 + 10h10 + 5h5 + 2h2 + h1 ≤ 199
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 10 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 11 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 12 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 13 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 14 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 15 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 16 / 47
◮ If it fits entirely on the backpack, include it all and continue to the
◮ If it does not fit entirely, include the largest possible quantity and
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 17 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 18 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 19 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 20 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 21 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 22 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 23 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 24 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 25 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 26 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 27 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 28 / 47
◮ If it has less edges, it does not connect the graph ◮ If it has more edges, it forms a cycle Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 29 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 30 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 31 / 47
◮ For example, if the weights are all equal, all spanning trees are MSTs!
◮ When we want to connect computers in a networks using the minimum
◮ When we want to connect houses to the electrical network using the
◮ There is an exponential number of spanning trees ◮ Finding all possible spanning trees and choosing the best is not
◮ How to do better? Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 32 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 33 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 34 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 35 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 36 / 47
◮ Let T = {T1, . . . , Tn−1} be the spanning tree produced by Prim ◮ Let H = {H1, . . . , Hn−1} be any minimum spanning tree
◮ Let V be the nodes connected the moment before e is added ◮ Suppose that e connects nodes ue and we where ue is the parent of we
◮ Because H is a spanning tree, there is a path P between ue and we ◮ There must be an edge f in this path P with one node in V and
◮ When we were constructing T, we could have added f , but we didn’t.
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 37 / 47
◮ H2 is still connected because all paths that required f can now use e ◮ H2 is acyclic as H2 still has n − 1 edges ◮ The cost of H2 is smaller or equal than H since weight(e) ≤ weight(f )
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 38 / 47
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 39 / 47
◮ GET-MIN will be called |V | times ◮ Each edge will be considered two times (one for each if its endpoints)
◮ Therefore the complexity is O(|E| + |V | × cost(GET-MIN))
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 40 / 47
◮ max-heaps: the priority element is the maximum ◮ min-heaps: the priority element is the minimum
◮ This guarantees that the maximum height of the tree with n nodes is
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 41 / 47
◮ The children of a node (i) are the nodes in positions (i ∗ 2) and
◮ The parent of a node (i) is the node in position (i/2).
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 42 / 47
◮ In a min-heap the root is the globally smallest element ◮ In a max-heap the root is the globally largest element
◮ We take the last element and we put it at the root position ◮ That element ”goes down” (down-heap), swapping with the hight
◮ At most we do O(log n) swaps, because the tree is balanced! Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 43 / 47
◮ Start by putting it on the last position ◮ The element ”goes up” (up-heap), swapping with the parent, until the
◮ At most we do O(log n) swaps, because the tree is balanced, because
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 44 / 47
◮ Insert each element in the heap in n × O(log n) ◮ Call the remove operation n times. The element will be removed in
◮ The entire process will therefore take O(n log n)
◮ Assume we just put all elements an the array in the beginning ◮ We could then just call down-heap on all elements from positions n/2
◮ We can show that this would have a total cost of O(n): we can build
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 45 / 47
◮ Each GET-MIN will cost O(log |V |) (just call the remove operation of
◮ Each update will also cost O(log |V |) (because an update can only
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 46 / 47
◮ Optimality is not guaranteed because we are not exploring completely
◮ Generally it is easier to prove non-optimality (counter-example) ◮ A simple way of analysing is to think about a case where there is a tie
◮ We have shown examples of some possible proof techniques for greedy
⋆ ”Exchange argument”: show that you can iteratively transform any
⋆ ”Stay-Ahead”: find a measure by which your greedy algorithm stays
Pedro Ribeiro (DCC/FCUP) Greedy Algorithms 2018/2019 47 / 47