GreedyIntro
GreedyIntro
The greedy method is perhaps the most straightforward design technique, and it can be applied
to a wide variety of problems.
Most, though not all, of these problems have n inputs and require us to obtain a subset that
satisfies some constraints. Any subset that satisfies these constraints is called a feasible solution.
We are required to find a feasible solution that either maximizes or minimizes a given objective
function. A feasible solution that does this is called an optimal solution.
The greedy method suggests that one can devise an algorithm which works in stages, considering
one input at a time. At each stage, a decision is made regarding whether or not a particular input
is in an optimal solution. This is done by considering the inputs in an order determined by some
selection procedure. If the inclusion of the next input into the partially constructed optimal
solution will result in an infeasible solution, then this input is not added to the partial solution.
The selection procedure itself is based on some optimization measure. This measure may or may
not be the objective function. In fact, several different optimization measures may be plausible for
a given problem.
The following procedure describes the greedy method abstractly, but more precisely than above.
The function SELECT selects an input from A, removes it and assigns its value to x. FEASIBLE
is a Boolean-valued function which determines if x can be included into the solution vector.
UNION actually combines x with solution and updates the objective function.
Note: Procedure GREEDY describes the essential way that a greedy based algorithm will look,
once a particular problem is chosen and the procedures SELECT, FEASIBLE and UNION are
properly implemented.
Reference:
The classroom scheduling problem:
Suppose we have a classroom and want to hold as many classes here as possible.
We get a list of classes as follows:
We can’t hold all of these classes in there, because some of them overlap:
Problem Statement: We want to hold as many classes as possible in this classroom. How do we
pick what set of classes to hold, so that we get the biggest set of classes possible?
Soln It seems like a hard problem, right? Actually, the algorithm is so easy, it might surprise you.
Here’s how it works:
1. Pick the class that ends the soonest. This is the first class you’ll hold in this classroom.
2. Now, we have to pick a class that starts after the first class. Again, pick the class that ends the
soonest. This is the second class you’ll hold.
Keep doing this, and you’ll end up with the answer!
Art ends the soonest, at 10:00 a.m., so that’s one of the classes you pick.
Now we need the next class that starts after 10:00 a.m. and ends the soonest. (English is out because
it conflicts with Art, but Math works)
This algorithm may seem to be very easy and too obvious. One may think that so it must not be
correct.
But that’s the beauty of greedy algorithms: they’re easy!
A greedy algorithm is simple: at each step, pick the optimal move.
(In this case, each time you pick a class, you pick the class that ends the soonest.)
In technical terms: at each step you pick the locally optimal solution, and in the end, you’re left
with the globally optimal solution.
Believe it or not, this simple algorithm finds the optimal solution to this scheduling problem!
Obviously, greedy algorithms don’t always work. But they’re simple to write!
The knapsack problem:
Suppose you’re a greedy thief. You’re in a store with a knapsack, and there
are all these items you can steal. But you can only take what you can fit in
your knapsack. The knapsack can hold 35 pounds.
You’re trying to maximize the value of the items you put in your knapsack.
What algorithm do you use?
For example, suppose there are following three items you can steal.
Your knapsack can hold 35 pounds of items. The stereo system is the most expensive, so you
steal that. Now you don’t have space for anything else.
You got $3,000 worth of goods.
But wait! If you’d picked the laptop and the guitar instead, you could have had $3,500 worth of
loot!
Clearly, the greedy strategy doesn’t give you the optimal solution here. But it gets you pretty close.
Using DP, we can explain how to calculate the correct solution. But if you’re a thief in a shopping
center, you don’t care about perfect. “Pretty good” is good enough.
Here’s the takeaway from this example: sometimes, perfect is the enemy of good. Sometimes all
you need is an algorithm that solves the problem pretty well. And that’s where greedy algorithms
shine, because they’re simple to write and usually get pretty close.
Reference: