DAA Unit3 Greedy Methods
DAA Unit3 Greedy Methods
OF ALGORITHMS
Unit III
Greedy Method
Greedy Algorithms
• Greedy is an algorithmic paradigm that builds
up a solution piece by piece, always choosing
the next piece that offers the most obvious and
immediate benefit.
• Typically used to solve optimization problems.
• So the problems where choosing locally
optimal also leads to global solution are best fit
for Greedy.
• There is no need to evaluate alternative
Example
• Devise an algo. for paying given amount using smallest
possible number of coins
• Available coins:
– Dollars(100 cents)
– Quarters (25 cents)
– Dimes(10 cents)
– Nickles(5 cents)
– Pennies(1 cents)
• If we have to pay 289 cents
• Best solution:-
– 10 coins(2 dollar, 3 quarter,1 dime and 4 pennies)
General characteristics of Greedy
• A candidate set, from which a solution is created
• A selection function, which chooses the best
candidate to be added to the solution
• A feasibility function, that is used to determine if a
candidate can be used to contribute to a solution
• An objective function, which assigns a value to a
solution, or a partial solution, and
• A solution function, which will indicate when we
have discovered a complete solution
Function greedy
Elements of greedy technique
• 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. This chapter explores optimization
problems that are solvable by greedy
algorithms.
• Greedy algorithms do not always yield optimal
solutions, but for many problems they
To develop a greedy algorithm was a bit more involved than is
typical. We went through the following steps:
• 1. Determine the optimal substructure of the problem.
• 2. Develop a recursive solution.
• 3. Prove that at any stage of the recursion, one of the optimal
choices is the greedy choice. Thus, it is always safe to make the
greedy choice.
• 4. Show that all but one of the subproblems induced by having
made the greedy choice are empty.
• 5. Develop a recursive algorithm that implements the greedy
strategy.
• 6. Convert the recursive algorithm to an iterative algorith
An activity-selection problem
• scheduling several competing activities that require
exclusive use of a common resource, with a goal of selecting
a maximum-size set of mutually compatible activities.
• Suppose we have a set S = {a1, a2, ..., an} of n proposed
activities that wish to use a resource, such as a lecture hall,
which can be used by only one activity at a time.
• Each activity ai has a start time si and a finish time fi, where
0 ≤ si < fi < ∞. If selected, activity ai takes place during the
half-open time interval [si, fi). Activities ai and aj are
compatible if the intervals [si, fi) and [sj fj) do not overlap
(i.e., ai and aj are compatible if si ≥ fj or sj ≥ fi)
activity-selection problem
The final activity schedule is (A1,A3,A4,A6,A7,A9,A10)
example
• Given 10 activities along with their start and
finish time as
Activity 1 2 3 4 5 6 7 8 9 10
Start 1 2 3 4 5 6 7 8 9 10
time
Finish 5 3 4 6 7 8 11 10 12 13
time