Sie sind auf Seite 1von 3

# GREEDY METHOD ALGORITHM

THEORY Sometimes using a dynamic programming approach to solve/determine the best choices for optimization problems takes a large amount of time and is complex. There are some more efficient and simpler algorithms to solve these problems. Using the name greedy one can imagine about having the best possible thing at a time ithout thinking about the future. The same happens in the case of greedy algorithm. !ere e pick up the best suitable choice at a particular instance ithout thinking about the future" even though that choice takes us to the orst solution in the future ork. #reedy algorithms do not al ays yield optimal solutions" but for many problems they do. So the basic steps that are involved in solving a problem thro gh greed! method are as "ollo#s\$ %& 'ast the optimi(ation problem as one in #hich #e ma)e a choice and are le"t #ith one s b*problem to solve& +That means a"ter ever! instance !o #ill be le"t #ith onl! one s b*problem&, -& .rove that there is al#a!s an optimal sol tion to the original problem that ma)es the greed! choice/ so that the greed! choice is al#a!s sa"e +It is one o" the most important steps o" the greed! method,& 0& Demonstrate that/ having made the greed! choice/ #hat remains is a s b*problem #ith the propert! that i" #e combine an optimal sol tion to the s b*problem #ith the greed! choice #e have made/ #e arrive at an optimal sol tion to the original problem&

1o# ho# does the greed! algorithm di""er "rom the d!namic one2

\$n dynamic programming" e make a choice at each step" but the choice usually depends on the solutions to sub%problems. &onse'uently" e typically solve dynamic%programming problems in a bottom%up manner" progressing from smaller sub%problems to larger sub%problems. \$n a greedy algorithm" e make hatever choice seems best at the moment and then solve the sub%problem arising after the choice is made.

Optimal s b*str ct re ( problem exhibits optimal sub%structure if an optimal solution to the problem contains ithin it optimal solutions to sub%problems. This property is a key ingredient of assessing the applicability of dynamic programming as ell as greedy algorithms. The one thing that e make very clear over here is that both greedy and dynamic methods are very similar in their ork" but e have to point out t hich method is best suitable to hich problem. )ne might be tempted to form a greedy algorithm for a dynamically solvable problem so e have to take care for that. So no " let*s move on to some very interesting examples of the greedy method in the next section. .

rv .content.section.icon.text

## Greedy Method Example

GREEDY METHOD E3AM.LES

O.TIMAL STORAGE O1 TA.ES There are *n* programs that are to be stored on a computer tape of length *l*.

(ssociated ith each other i is the length li" +,-i,-n. (ll the programs can only be ritten on the tape if the sum of all the lengths of the program is at most l. (ssumption. that henever a program is to be retrieved from the tape " the tape is positioned at the front. /o if the programs are stored in the order as \$-i+"i 0"i1"i2........."in" then the time t3 needed to retrieve program 4i"35 is proportional to 6 +7k73 lik. So the problem is that e have to store them in the tape in such an order that the 8.9.T 4mean retrieval time5 is minimum. ( greedy approach to build such an algorithm re'uires such a permutation that chooses the next program on the basis of some optimization measure. \$n this e take the optimization measure to be *d* 4 hich is the current length of the tape that is ritten ith the program5 So no every time hen e rite onto the disk" e keep in mind that the increment in the length of *d* is minimum. So this greedy method re'uires us to 3ust sort the lengths of the programs or to assign them in a non%decreasing order" so that they can be directly ritten into disk on their length basis. So it in turns takes the complexity e'ual to )4n logn5 3ust to sort the length of the programs. !ere*s a sample pseudo code for the program if e have multiple disks on hich e have to rite the data .se do code tapes+n/m, 44 here n is the n mbers o" programs and m is the n mbers o" tapes 5 6789 "or+i7% to n do, 5 #rite +:append:/ i :to the perm tation o" the tape:/ 6, 67+6;%, mod m < <

9:;:9:/&: <))= +.:llis !oro itz and Sarta3 Sahni" Fundamentals of Computer Algorithms" &omputer Science >ress" 8aryland" +?@A" B0B pages