Greedy Algorithm
Greedy Algorithm
Greedy Algorithm
G r e e d y ALGORITHM
1
Introduction
As the name suggests, they are short-sighted in their approach,
taking decisions on the basis of information immediately at hand
without worrying about the effect these decisions may have in
the future.
Thus they are easy to invent, easy to implement, and-when they
work-efficient.
Greedy algorithms are typically used to solve optimization
problems.
Examples
2
Change Making Problem
Making c h a n g e (1)
Suppose we live in a country where the following coins
areavailable: dollars (100 cents),
quarters (25 cents),
dimes (10 cents),
nickels (5 cents)
and pennies (1 cent).
Our problem is to devise an algorithm for paying a given
amount to a customer using the smallest possible number of
coins.
For instance,
if we must pay $2.89 (289 cents), than what is solution
of above problem?
5
Making c h a n g e (1)
Suppose we live in a country where the following coins
areavailable: dollars (100 cents),
quarters (25 cents),
dimes (10 cents),
nickels (5 cents)
and pennies (1 cent).
Our problem is to devise an algorithm for paying a given
amount to a customer using the smallest possible number of
coins.
For instance, if we must pay $2.89 (289 cents), the best solution is
to give the customer 10 coins: 2 dollars, 3 quarters, 1 dime and 4
pennies.
Most of us solve this kind of problem every day without thinking
twice, unconsciously using an obvious greedy algorithm: starting
with nothing, at every stage we add to the coins already chosen a
coin of the largest value available that does not take us past the
amount to be paid. 4
Algorithm
A Set of candidates :
We have some problem to solve in an optimal way. To construct
the solution of our problem, we have a set (or list) of
candidates: the coins that are available, the edges of a graph that
may be used to build a path, the set of jobs to be scheduled, or
whatever.
Isfeasible function
A second function checks whether a set of candidates isfeasible,
that is, whether or not it is possible to complete the set by adding
further candidates so as to obtain at least one solution to our
problem. Here too, we are not for the time being concerned with
optimality. We usually expect the problem to have at least one
solution that can be obtained using candidates from the set
initially available.
9
Continue…
Selection function :
Yet another function, the selection function, indicates at any time which
of the remaining candidates, that have neither been chosen nor rejected,
is the most promising.
Analysis:
1) solution by PRIM’S algorithm
Steps {u,v} B
Initialization ------------- {a}
{a,b} {a,b}
{a,c} {a,b,c}
{c,e} {a,b,c,e}
{e,d} {a,b,c,d,e}
{d,z} { a , b , c, d , e , z }
Example -
2
2) By kruskal’s algorithm
Steps Edge Connected Component
considered
Initialization ------------- {A} , {B} , {C} , {D} , {E} , {F}
{B,D } {A} , {B , D} , {C} , {E} , {F}
{B,C} {A} , {B , C , D} , {E} , {F}
{C ,D } Rejected
{B,F} {A} , {B , C , D , F} , {E}
{F,D } Rejected
{A,B} {A , B , C , D , F} , {E}
{E,D } {A , B , C , D , E , F}