You are on page 1of 16

Greedy Algorithms

Introduction
Algorithms for optimization problems typically go through a sequence
of steps, with a set of choices at each step.
For many optimization problems, using dynamic programming to
determine the best choices is overkill;
Simpler, more efficient algorithms will do.
Greedy Algorithms : Definition
A greedy algorithm always makes the choice that looks best at the
moment.
That is, it makes a locally optimal choice in the hope that this choice
will lead to a globally optimal solution.
Does it provide optimal solution?
Not always!
But for some cases, it does.
0-1 Knapsack Problem : DP
Item Name Price Weight Ratio
No.
1. Algorithm Book 840 7 120
(Original copy)
2. A Ornament box 2250 15 150
20

3. DVD collection 420 6 70

4. Book of poetry 660 6 110


(
)
5. Shari Collection 840 7 120
Elements Of The Greedy Strategy
1. Determine the optimal substructure of the problem.
2. Develop a recursive solution.
3. Show that if we make the greedy choice, then only one subproblem
remains.
4. Prove that it is always safe to make the greedy choice. (Steps 3 and 4 can
occur in either order.)
5. Develop a recursive algorithm that implements the greedy strategy.
6. Convert the recursive algorithm to an iterative algorithm.
Greedy Algorithms : When does it Work?
How can we tell whether a greedy algorithm will solve a particular
optimization problem?
No way works all the time
The greedy-choice property and optimal substructure are the two
key ingredients.
Greedy-choice property
The first key ingredient.
we can assemble a globally optimal solution by making locally
optimal (greedy) choices.
In other words, when we are considering which choice to make, we
make the choice that looks best in the current problem, without
considering results from subproblems.
Optimal substructure
A problem exhibits optimal substructure if an optimal solution to the
problem contains within it optimal solutions to subproblems.
This property is a key ingredient of assessing the applicability of
dynamic programming as well as greedy algorithms.
We usually use a more direct approach regarding optimal substructure
when applying it to greedy algorithms.
Greedy versus Dynamic Programming
fractional Knapsack Problem : Greedy
Item Name Price Weight Ratio
No.
1. Rice 840 7 120

2. Beef 2250 15 150


20

3. Fish 420 6 70

4. Vegetable 660 6 110

5. Chicken 840 7 120


Greedy Versus Dynamic Programming
fractional Knapsack Problem : Greedy
Algorithm!

20
An activity-selection problem

Have to find largest subset of compatible activities.


Two activities are compatible if , [ , ) do not overlap.
{3 , 9 , 11 }
{1 , 4 , 8 , 11 }
{2 , 4 , 9 , 11 }
An activity-selection problem
0-1 Knapsack Problem : DP

You might also like