Successfully reported this slideshow.
Upcoming SlideShare
×

# Greedymethod

8,484 views

Published on

Published in: Education, Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Greedymethod

1. 1. GREEDY METHOD
2. 2. OUTLINE <ul><li>Greedy introduction </li></ul><ul><li>Characteristics and features </li></ul><ul><li>Optimization Problem </li></ul><ul><li>Pseudo code for greedy algorithm </li></ul><ul><li>Greedy approach </li></ul><ul><ul><li>The coin changing problem </li></ul></ul><ul><li>Container overloading </li></ul><ul><li>Comparison with DP </li></ul><ul><li>Pros and Cons </li></ul>
3. 3. GREEDY INTRODUCTION <ul><li>Greedy algorithms are simple and straightforward. </li></ul><ul><li>They are shortsighted in their approach </li></ul><ul><li>A greedy algorithm is similar to a dynamic programming algorithm, but the difference is that solutions to the sub problems do not have to be known at each stage, </li></ul><ul><li>instead a &quot;greedy&quot; choice can be made of what looks best for the moment. </li></ul>
4. 4. CHARACTERISTICS AND FEATURES <ul><li>To construct the solution in an optimal way. Algorithm Maintains two sets, </li></ul><ul><li>-One contains chosen items and </li></ul><ul><li>-The other contains rejected items. </li></ul><ul><li>Greedy algorithms make good local choices in the hope that They result in, </li></ul><ul><li>-An optimal solution. </li></ul><ul><li>-Feasible solutions. </li></ul>
5. 5. CONTINUED… <ul><li>The greedy algorithm consists of four (4) function. </li></ul><ul><li>A function that checks whether chosen set of items provide a solution. </li></ul><ul><li>A function that checks the feasibility of a set. </li></ul><ul><li>The selection function tells which of the items is the most promising. </li></ul><ul><li>An objective function, which does not appear explicitly, gives the value of a solution. </li></ul>
6. 6. OPTIMIZATION PROBLEMS <ul><li>An optimization problem: </li></ul><ul><li>Given a problem instance, a set of constraints and an objective function . </li></ul><ul><li>Find a feasible solution for the given instance for which the objective function has an optimal value. </li></ul><ul><li>Either maximum or minimum depending on the problem being solved.A feasible solution that does this is called optimal solution . </li></ul>
7. 7. Continued… <ul><li>Feasible : </li></ul><ul><li>A feasible solution satisfies the problem’s constraints </li></ul><ul><li>Constraints : </li></ul><ul><li>The constraints specify the limitations on the required solutions. </li></ul>
8. 8. GREEDY PROPERTY <ul><li>It consists of two property, </li></ul><ul><li>1. &quot; greedy-choice property &quot; ->It says that a globally optimal solution can be arrived at by making a locally optimal choice. </li></ul><ul><li>2. &quot; optimal substructure &quot; ->A problem exhibits optimal substructure if an optimal solution to the problem contains optimal solutions to the sub-problems. </li></ul><ul><li>are two ingredients in the problem that lend to a greedy strategy. </li></ul>
9. 9. Pseudo-code for Greedy Algorithm <ul><li>Algorithm Greedy (a,n) </li></ul><ul><li>//a[1:n]contains the n inputs. </li></ul><ul><li>{ </li></ul><ul><li>solution:=0;//initialize the solution. </li></ul><ul><li>for i:=1 to n do </li></ul><ul><li>{ </li></ul><ul><li>x:=Select(a); </li></ul><ul><li> if Feasible( solution, x) then </li></ul><ul><li>solution:=Union(solution,x); </li></ul><ul><li>} </li></ul><ul><li>return solution; </li></ul><ul><li>} </li></ul>
10. 10. CONTINUED… <ul><li>Select () selects an input from a[] and removes it. </li></ul><ul><li>the selected input value is assigned to x. </li></ul><ul><li>Feasible () is a boolean-valued function that determines whether x can be included into the solution vector(no constraints are violated). </li></ul><ul><li>Union() combines x with the solution and updates the objective function. </li></ul>
11. 11. GREEDY APPROACH <ul><li>Greedy Algorithm works by making the decision that seems most promising at any moment; it never reconsiders this decision, whatever situation may arise later. </li></ul><ul><li>As an example consider the problem of &quot; Making Change&quot;. </li></ul><ul><li>Coins available are: </li></ul><ul><ul><li>100 cents </li></ul></ul><ul><ul><li>25 cents </li></ul></ul><ul><ul><li>10 cents </li></ul></ul><ul><ul><li>5 cents </li></ul></ul><ul><ul><li>1 cent </li></ul></ul>
12. 12. CONTINUED… <ul><li>Problem: </li></ul><ul><li> Make a change of a given amount using the smallest possible number of coins. </li></ul><ul><li>Solution: </li></ul><ul><li>The coin is selected using greedy criterion: at each stage increases the total amount of change constructed as much as possible. </li></ul><ul><li>To assure feasibility i.e., the change given exactly equals the desired amount of the solution </li></ul>
13. 13. Coin changing problem <ul><li>Algorithm: </li></ul><ul><li>Make change for n units using the least possible number of coins. </li></ul><ul><li>MAKE-CHANGE (n)         </li></ul><ul><li>C ← {100, 25, 10, 5, 1}     // constant. S← {};                  // set that will hold the solution set.  </li></ul><ul><li>  Sum ← 0 sum of item in solution set   WHILE sum not = n      x = largest item in set C such that sum + x ≤ n       IF no such item THEN           RETURN     &quot;No Solution&quot;      S ← S {value of x}      sum ← sum + x    RETURN S </li></ul>