Upcoming SlideShare
×

Greedymethod

4,823
-1

Published on

Published in: Education, Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

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

Views
Total Views
4,823
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
160
0
Likes
2
Embeds 0
No embeds

No notes for slide

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>
15. 15. CONTINUED… <ul><li>Algorithm Container Loading(c,capacity,number of containers,x ) </li></ul><ul><li>//greedy algorithm for container loading </li></ul><ul><li>//set x[i]=1 iff container c[i],i>=1 is loaded. </li></ul><ul><li>{ </li></ul><ul><li>//sort into increasing order of weight </li></ul><ul><li>Sort(c,number of Containers); </li></ul><ul><li>n:=number of Containers; </li></ul><ul><li>//initialize x </li></ul><ul><li>for i:=1 to n do </li></ul><ul><li>x[i]:=0; </li></ul>
16. 16. CONTINUED… <ul><li>//select containers in order of weight </li></ul><ul><li>i :=1; </li></ul><ul><li>While (i<=n&& c[i].weight<=capacity) </li></ul><ul><li>{ </li></ul><ul><li>//enough capacity for container c[i].id </li></ul><ul><li>X[c[i].id]:=1; </li></ul><ul><li>Capacity-=c[i].weight;//remaining capacity </li></ul><ul><li>i++; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
17. 17. COMPARISON WITH DP <ul><li>Greedy and Dynamic Programming are methods for solving optimization problems. </li></ul><ul><li>Greedy algorithms are usually more efficient than DP solutions. However, often you need to use dynamic programming since the optimal solution cannot be guaranteed by a greedy algorithm. </li></ul><ul><li>DP provides efficient solutions for some problems for which a brute force approach would be very slow </li></ul>
18. 18. PROS AND CONS <ul><li>PROS: </li></ul><ul><li>They are easier to implement, </li></ul><ul><li>they require much less computing resources, </li></ul><ul><li>they are much faster to execute. </li></ul><ul><li>Greedy algorithms are used to solve optimization problems </li></ul><ul><li>CONS: </li></ul><ul><li>Their only disadvantage being that they not always reach the global optimum solution; </li></ul><ul><li>on the other hand, even when the global optimum solution is not reached, most of the times the reached sub-optimal solution is a very good solution. </li></ul>
1. A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.