110 likes | 266 Views
Greedy Algorithm & Assignment 3 Discussion CSCI3160 tutorial (7 t h week) Office: SHB 913 Office Hour: (Mon) 10:00 -12:00 Email: jjye@cse.cuhk.edu.hk. Ye Junjie. Outline. Greedy Algorithm Coin Changing Bin Packing Assignment 3 Discussion . Coin Changing.
E N D
Greedy Algorithm & Assignment 3 DiscussionCSCI3160 tutorial (7th week)Office: SHB 913 Office Hour: (Mon) 10:00 -12:00 Email: jjye@cse.cuhk.edu.hk Ye Junjie
Outline Greedy Algorithm Coin Changing Bin Packing Assignment 3 Discussion
Coin Changing Suppose that in a certain country, the coin dominations consist of: $1, $2, $5, $10 You want to design an algorithm such that you can make change of any x dollars using the fewest number of coins.
Coin Changing Greedy algorithm: 1. Create an empty bag 2. while (x > 0) { Find the largest coin c at most x; Put c in the bag; Set x = x –c ; } 3. Return coins in the bag
Bin Packing Bin Packing: In the bin packing problem, objects of different volumes must be packed into a finite number of bins or containers each of volume V in a way that minimizes the number of bins used. There are many variations of this problem, such as 2D packing, linear packing, packing by weight, packing by cost, and so on. http://en.wikipedia.org/wiki/Bin_packing_problem
Bin Packing First-fit Algorithm: The algorithm processes the items in arbitrary order. For each item, it attempts to place the item in the first bin that can accommodate the item. If no bin is found, it opens a new bin and puts the item within the new bin. How good is the greedy algorithm comparing the optimal solution?
Bin Packing First-fit Algorithm: It is impossible for 2 bins to be at most half full. Otherwise, it means at some point one bin is at most half full and we open a new bin to accommodate an item of size at most half capacity. Thus if we have B bins, at least B − 1 bins are more than half full. We have B ≤ 2OPT. Other Algorithms: Best-fit and etc…
Assignment 3 Discussion 4.9: Consider a directed graph in which the only negative edges are those that leave s; all other edges are positive. Can Dijkstra’s algorithm apply? Use induction on set S to prove the correctness. 5.9(b): If G has a cycle with a unique heaviest edge e, then e can not be part of any MST. Replace e with other edge
Assignment 3 Discussion For the Greedy-MST algorithm, prove that the colourinvariant holds after an application of the red rule: We need to prove that after applying a red rule, there is still a MST containing all blue edges. Bonus(1-spanner): Prove that for a MST M, T contains all edges of M.
Thank you! Q&A