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.
An optimization problem is one in which you want to find, not just a solution but best solution Search technique look at many possible solutions eg dynamic programming or backtrack search A “greedy algorithm” sometimes works well for optimization problems
A greedy algorithm works in a phases At each phase: You take the best you can get right now without regard for future You hope that by choosing a local optimum at each step, you will end up at a global optimum For some problems, greedy approch always gets optimum
For others, greedy finds good, but not always best. If so it is called greedy heuristic, or opproximation For still other greedy approch can do very poorly
Construct optimum solution piece by piece Generic Step Having constructed some part of optimum solution extend this by identifing the next part
Comparison: Dynamic ProgrammingGreedy Algorithms At each step, the choice is determined based on solutions of subproblems. At each step, we quickly make a choice that currently looks best. --A local optimal (greedy) choice. Bottom-up approachTop-down approach Sub-problems are solved first.Greedy choice can be made first before solving further sub-problems. Can be slower, more complexUsually faster, simpler
A thief robbing a store finds n items. i th item: worth v i dollars w i pounds W, w i, v i are integers. He can carry at most W pounds. 0-1 Knapsack Problem: Which items should I take?
A thief robbing a store finds n items. i th item: worth v i dollars w i pounds W, w i, v i are integers. He can carry at most W pounds. He can take fractions of items. Fractional Knapsack Problem: ?
Both problems are similar. But Fractional Knapsack Problem can be solved in a greedy strategy. Step 1.Compute the value per pound for each item E.g. gold dust: $10000 per pound (most expensive) Silver dust: $2000 per pound Copper dust: $500 per pound Step 2.Take as much as possible of the most expensive (ie. Gold dust) Step 3. If the supply of that item is exhausted (ie. no more gold) and he can still carry more, he takes as much as possible of the item that is next most expensive and so forth until he can’t carry any more.
We can solve the Fractional Knapsack Problem by a greedy algorithm: Always makes the choice that looks best at the moment. i.e., A locally optimal Choice
You have to run nine jobs, with running times of 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes You have three processors on which you can run these jobs You decide to do the longest-running jobs first, on whatever processor is available Time to completion: = 35 minutes This solution isn’t bad, but we might be able to do better P1 P2 P3
What would be the result if you ran the shortest job first? Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes That wasn’t such a good idea; time to completion is now = 40 minutes Note, however, that the greedy algorithm itself is fast All we had to do at each stage was pick the minimum or maximum P1 P2 P3
Better solutions do exist: This solution is clearly optimal (why?) Clearly, there are other optimal solutions (why?) How do we find such a solution? One way: Try all possible assignments of jobs to processors Unfortunately, this approach can take exponential time P1 P2 P3