150 likes | 300 Views
HKOI 2004 Team Training. Greedy Algorithms (Intermediate Level). Overview. Is there anything for me to teach?. What is “Greedy Algorithm”?. We are greedy, aren ’ t we? Sometimes, we want more e.g. money, exam score, ... Sometimes, we want less
E N D
HKOI 2004 Team Training Greedy Algorithms (Intermediate Level)
Overview • Is there anything for me to teach?
What is “Greedy Algorithm”? • We are greedy, aren’t we? • Sometimes, we want more • e.g. money, exam score, ... • Sometimes, we want less • e.g. shortest distance or time, cost of goods, ... • With information on our hands, we may choose the best (most or least) things until we’ve achieved our goals. • Buy things in the cheapest way in a supermarket. • More examples later.
What is “Greedy Algorithm”? • From another point of view ... • Sometimes, when the problem ... • Is so straight forward, • That can be cut into sub-problems, • Until we reach a trivial base case, • Then solve the trivial case, then the not-so-trivial sub-problems, and so on, until we can solve the original big problem, • i.e. the famous Divide and Conquer !!! • Well ... Isn’t it recursion then?
What is an “Greedy Algorithm”? • Divide and conquer does NOT mean recursion! • Recursion, dynamic programming (DyP), greedy, etc ... • The approaches are different • Recursion: top-down • Cut the big problem into smaller ones, then solve them, finally solve the big one
What is an “Greedy Algorithm”? • Dynamic programming: bottom-up • Solve the small problems first, then build up the solution for larger problems, and larger, and larger, until one day, we “suddenly” discover that we have just solved the big problem! • Greedy: it depends ... • Solve the small problems first, then build up the solution for larger problems, and larger, and larger, until one day, we find no difference between Greedy and DyP?!
Greedy vs. DyP • Well, there is no difference. Greedy is DyP. Except that ... • Greedy is actually a special case of DyP. • A Greedy-solvable problem is one that can only be cut into one Greedy-solvable sub-problem. • A DyP-solvable problem often involves finding the optimal solution from N DyP-solved sub-problems. • See “Introduction to Algorithm” P.9, 10
Greedy vs. D&C • So how can you prove that a problem can be solved by D&C? • I think someone has taught you already. • So how can you proof that a problem can be solved by solving only one sub-problem? • Intuitively?!
Greedy vs. D&C • Well, I am weak in proving things, and sometimes you don’t have much time to really prove things “formally” in a competition. • So, please convince yourself by yourself. • For your information, the “Unu’s conjecture of competition” states that: • If you have a not-yet-proved “solution” to a problem, but you don’t know its correctness (i.e you can’t prove) and you can’t disprove it either, then it is correct.
Example: Fractional Knapsack • Given materials of different values per unit volume and maximum amounts, find the most valuable mix of materials which fit in a knapsack of a fixed volume. Note that we may take pieces (fractions) of materials. • Proof: • http://www-cse.uta.edu/~holder/courses/ cse2320/lectures/l15/node6.html
Example: $$$ • Why we have these HKD? • Notes: $1000, $500, $100, $50, $20, $10 • Coins: $10, $5, $2, $1, $0.5, $0.2, $0.1 • Because in that way, any amount can be solved by Greedy! • All money in the world organize in the same way. • At least I’ve seen none that is not ... • Unfortunately, stamps are not ...
Counter-example:UVA10003 – Cutting Sticks • Greedy solvable? • We need to solve many possible sub-problems first before solving the big problem, and some kind of processing is needed. • A DyP problem actually. • http://acm.uva.es/p/v100/10003.html
(Not-so-)Counter-example:UVA10037 – Bridge • Play here! • http://www.plastelina.net/games/game3.html • Greedy solvable? • Well, not that straight forward. • Base case: 1 or 2 or 3 person/people • Then consider the 2 fastest and 2 slowest persons if there are still 4 or more people.
Less straight forward example: 2014 – Activities • A greedy person has to be clever! • Several “obvious” ways to “greedy” out the answer, but may be wrong! • By first start time? • By shortest duration? • By fewest overlap? • By first end time? • By ... • Try to solve it yourself!
Exercises (Greedy or not?) • Think twice! Are they really solvable by Greedy? • ???? – Fractional Knapsack • 1014 – Stamps • 2025 – Addition • Alias 1019 using more advanced algorithms. • 2014 – Activity • 5022 – Coins (Why not Greedy solvable?) • 30037 – Bridge