Vous êtes sur la page 1sur 32

Design and Analysis of Algorithm ( www.jntuworld.

com )

UNIT - II
DIVIDE AND CONQUER: General method: Given a function to compute on n inputs the divide-and-con uer strategy suggests splitting the inputs into ! distinct su"sets# $%!%&n# yielding ! su" pro"lems. 'hese su" pro"lems must "e solved# and then a method must "e found to com"ine su" solutions into a solution of the whole. (f the su" pro"lems are still relatively large# then the divide-and-con uer strategy can possi"ly "e reapplied. )ften the su" pro"lems resulting from a divide-and-con uer design are of the same type as the original pro"lem. *or those cases the re application of the divide-and-con uer principle is naturally e+pressed "y a recursive algorithm. D And ,(Algorithm) is initially invo!ed as D and ,(-)# where p is the pro"lem to "e solved. .mall(-) is a /oolean-valued function that determines whether the i0p si1e is small enough that the answer can "e computed without splitting. (f this so# the function . is invo!ed. )therwise# the pro"lem - is divided into smaller su" pro"lems. 'hese su" pro"lems -$# -2 3-! are solved "y recursive application of D And ,. ,om"ine is a function that determines the solution to p using the solutions to the ! su" pro"lems. (f the si1e of p is n and the si1es of the ! su" pro"lems are n$# n2 3.n!# respectively# then the computing time of D And , is descri"ed "y the recurrence relation. '(n)& 4 g(n) n small '(n$)5'(n2)5333335'(n!)5f(n)6 otherwise. $

Design and Analysis of Algorithm ( www.jntuworld.com )

7here '(n) is the time for D And , on any (0p of si1e n. g(n) is the time of compute the answer directly for small (0ps. f(n) is the time for dividing - 8 com"ining the solution to su" pro"lems. $. 2. 9. :. ;. <. Algorithm D And ,(-) 4 if small(-) then return .(-)6 else 4 divide - into smaller instances -$# -23 -!# !=&$6 >. Apply D And , to each of these su" pro"lems6 ?. return com"ine (D And ,(-$)# D And ,(-2)#33.#D And ,(-!))6 @. A $B. A 'he comple+ity of many divide-and-con uer algorithms is given "y recurrences of the form '(n) & 4 '($) n&$ A'(n0")5f(n) n=$ 7here a 8 " are !nown constants. 7e assume that '($) is !nown 8 n is a power of "(i.e.# n&"C!) )ne of the methods for solving any such recurrence relation is called the su"stitution method. 'his method repeatedly ma!es su"stitution for each occurrence of the function. ' is the Dight-hand side until all such occurrences disappear. E+ampleF $) ,onsider the case in which a&2 and "&2. Get '($)&2 8 f(n)&n. 7e have# '(n) & 2'(n02)5n & 2H2'(n0202)5n02I5n & H:'(n0:)5nI5n & :'(n0:)52n & :H2'(n0:02)5n0:I52n & :H2'(n0?)5n0:I52n & ?'(n0?)5n52n & ?'(n0?)59n J J J 2

Design and Analysis of Algorithm ( www.jntuworld.com )

(n general# we see that '(n)&2Ci'(n02Ci )5in.# for any log n =&(=&$.

'(n) &2Clog n '(n02Clog n) 5 n log n ,orresponding to the choice of i&log n

'hus# '(n) & 2Clog n '(n02Clog n) 5 n log n & n. '(n0n) 5 n log n & n. '($) 5 n log n & 2n 5 n log n Hsince# log $&B# 2CB&$I

BINARY SEARCH
$. Algorithm /in search(a#n#+) 2. 00 Given an array aH$FnI of elements in non-decreasing 9. 00order# n=&B#determine whether + is present and :. 00 if so# return j such that +&aHjI6 else return B. ;. 4 <. lowF&$6 highF&n6 >. while (low%&high) do ?. 4 @. mid: !"lo#$hi%h&'()* $B. if (+%aHmidI) then high6 $$. else if(+=aHmidI) then low&mid5$6 $2. else return mid6 $9. A $:. return B6 $;. A Algorithm# descri"es this "inary search method# where /insrch has :(0ps aHI# ( # l 8 +. (t is initially invo!ed as /insrch (a#$#n#+) A non-recursive version of /insrch is given "elow. 'his /insearch has 9 i0ps a#n# 8 +. 'he while loop continues processing as long as there are more elements left to chec!. At the conclusion of the procedure B is returned if + is not present# or j is returned# such that aHjI&+. 7e o"serve that low 8 high are integer Karia"les such that each time through the loop either + is found or low is increased "y at least one or high is decreased at least one. 9

Design and Analysis of Algorithm ( www.jntuworld.com )

'hus we have 2 se uences of integers approaching each other and eventually low "ecomes = than high 8 causes termination in a finite no. of steps if + is not present.

E+ampleF $) Get us select the $: entries. -$;#-<#B#>#@#29#;:#?2#$B$#$$2#$2;#$9$#$:2#$;$. -lace them in aH$F$:I# and simulate the steps /insearch goes through as it searches for different values of +. )nly the varia"les# low# high 8 mid need to "e traced as we simulate the algorithm. 7e try the following values for +F $;$# -$: and @. for 2 successful searches 8 $ unsuccessful search. 'a"le. .hows the traces of /in search on these 9 steps. low $ ? $2 $: low $ $ $ 2 2 low $ $ : high $: $: $: $: high $: < 2 2 $ high $: < < mid > $$ $9 $: *ound mid > 9 $ 2 Mot found mid > 9 ; *ound

L&$;$

+&-$:

+&@

Theorem:

Algorithm /insearch(a#n#+) wor!s correctly.

+roo,: 7e assume that all statements wor! as e+pected and that comparisons such as +=aHmidI are appropriately carried out. (nitially low &$# high& n#n=&B# and aH$I%&aH2I%&33..%&aHnI. (f n&B# the while loop is not entered and is returned. :

Design and Analysis of Algorithm ( www.jntuworld.com )

)therwise we o"serve that each time thro the loop the possi"le elements to "e chec!ed of or e uality with + and aHlowI# aHlow5$I#33..#aHmidI#33aHhighI. (f +&aHmidI# then the algorithm terminates successfully. )therwise# the range is narrowed "y either increasing low to (mid5$) or decreasing high to (mid-$). ,learly# this narrowing of the range does not affect the outcome of the search. (f low "ecomes = than high# then + is not present 8 hence the loop is e+ited.

-a.im/m and -inim/m:


Get us consider another simple pro"lem that can "e solved "y the divide-andcon uer techni ue. 'he pro"lem is to find the ma+imum and minimum items in a set of n elements. (n analy1ing the time comple+ity of this algorithm# we once again concentrate on the no. of element comparisons. Nore importantly# when the elements in aH$FnI are polynomials# vectors# very large num"ers# or strings of character# the cost of an element comparison is much higher than the cost of the other operations. Oence# the time is determined mainly "y the total cost of the element comparison.

$. Algorithm straight Na+Nin(a#n#ma+#min) 2. 00 set ma+ to the ma+imum 8 min to the minimum of aH$FnI 9. 4 :. ma+F&minF&aH$I6 ;. for (F&2 to n do <. 4 >. if(aH(I=ma+) then ma+F&aH(I6 ?. if(aH(I%min) then minF&aH(I6 @. A $B. A Al%orithm: .traight forward Na+imum 8 Ninimum .traight Na+Nin re uires 2(n-$) element comparison in the "est# average 8 worst cases.

Design and Analysis of Algorithm ( www.jntuworld.com )

An immediate improvement is possi"le "y reali1ing that the comparison aH(I%min is necessary only when aH(I=ma+ is false. Oence we can replace the contents of the for loop "y# (f(aH(I=ma+) then ma+F&aH(I6 Else if (aH(I%min) then minF&aH(I6 Mow the "est case occurs when the elements are in increasing order. 'he no. of element comparison is (n-$). 'he worst case occurs when the elements are in decreasing order. 'he no. of elements comparison is 2(n-$) 'he average no. of element comparison is % than 2(n-$) )n the average aH(I is = than ma+ half the time# and so# the avg. no. of comparison is 9n02-$. A divide- and con uer algorithm for this pro"lem would proceed as followsF Get -&(n# aH(I #33#aHjI) denote an ar"itrary instance of the pro"lem. Oere n is the no. of elements in the list (aH(I#3.#aHjI) and we are interested in finding the ma+imum and minimum of the list.

(f the list has more than 2 elements# - has to "e divided into smaller instances. *or e+ample # we might divide - into the 2 instances# -$&(Hn02I#aH$I# 33..aHn02I) 8 -2& (n-Hn02I#aHHn02I5$I#3..#aHnI) After having divided - into 2 smaller su" pro"lems# we can solve them "y recursively invo!ing the same divide-and-con uer algorithm.

Al%orithm: Decursively *inding the Na+imum 8 Ninimum $. 2. 9. :. ;. <. >. ?. @. Algorithm Na+Nin ((#j#ma+#min) 00aH$FnI is a glo"al array# parameters ( 8 j 00are integers# $%&(%&j%&n.'he effect is to 00set ma+ 8 min to the largest 8 smallest value 00in aH(FjI# respectively. 4 if((&j) then ma+F& minF& aH(I6 else if ((&j-$) then 00 Another case of small(p) 4 <

Design and Analysis of Algorithm ( www.jntuworld.com )

$B. if (aH(I%aHjI) then $$. 4 $2. ma+F&aHjI6 $9. minF&aH(I6 $:. A $;. else $<. 4 $>. ma+F&aH(I6 $?. minF&aHjI6 $@. A 2B. A 2$. else 22. 4 29. 00 if - is not small# divide - into su"pro"lems. 2:. 00 find where to split the set mid: !"I$0&'()* 2;. 00solve the su"pro"lems 2<. Na+Nin((#mid#ma+.min)6 2>. Na+Nin(mid5$#j#ma+$#min$)6 2?. 00com"ine the solution 2@. if (ma+%ma+$) then ma+&ma+$6 9B. if(min=min$) then min & min$6 9$. A 92. A 'he procedure is initially invo!ed "y the statement# Na+Nin($#n#+#y) .uppose we simulate Na+Nin on the following @ elements

AF H$I H2I H9I H:I H;I H<I H>I H?I H@I 22 $9 -; -? $; <B $> 9$ :> A good way of !eeping trac! of recursive calls is to "uild a tree "y adding a node each time a new call is made. *or this Algorithm# each node has : items of informationF (# j# ma+ 8 imin. E+amining figF we see that the root node contains $ 8 @ as the values of ( 8j corresponding to the initial call to Na+Nin. 'his e+ecution produces 2 new calls to Na+Nin# where ( 8 j have the values $# ; 8 <# @ respectively 8 thus split the set into 2 su"sets of appro+imately the same si1e. *rom the tree# we can immediately see the ma+imum depth of recursion is :. (including the $st call) 'he include no.s in the upper left corner of each node represent the order in which ma+ 8 min are assigned values. Mo. of element ,omparisonF >

Design and Analysis of Algorithm ( www.jntuworld.com )

(f '(n) represents this no.# then the resulting recurrence relations is '(n)&4 '(Hn02I5'Hn02I52 n=2 $ n&2 B n&$

7hen n is a power of 2# n&2C! for some 5ve integer !# then '(n) & 2'(n02) 52 & 2(2'(n0:)52)52 & :'(n0:)5:52 J J & 2C!-$'(2)5 & 2C!-$52C!-2 & 2C!0252C!-2 & n025n-2 & (n52n)02)-2 T"n& "1n'(&-( JMote that (9n09)-9 is the "est-average# and worst-case no. of comparisons when n is a power of 2.

-ERGE SORT
As another e+ample divide-and-con uer# we investigate a sorting algorithm that has the nice property that is the worst case its comple+ity is )(n log n) 'his algorithm is called merge sort 7e assume throughout that the elements are to "e sorted in non-decreasing order. Given a se uence of n elements aH$I#3#aHnI the general idea is to imagine then split into 2 sets aH$I#3..#aHn02I and aHHn02I5$I#3.aHnI. Each set is individually sorted# and the resulting sorted se uences are merged to produce a single sorted se uence of n elements. 'hus# we have another ideal e+ample of the divide-and-con uer strategy in which the splitting is into 2 e ual-si1ed sets 8 the com"ining operation is the merging of 2 sorted sets into one.

Al%orithm 2or -er%e Sort: $. 2. 9. :. ;. <. Algorithm Nerge.ort(low#high) 00aHlowFhighI is a glo"al array to "e sorted 00.mall(-) is true if there is only one element 00to sort. (n this case the list is already sorted. 4 if (low%high) then 00if there are more than one element ?

Design and Analysis of Algorithm ( www.jntuworld.com )

>. 4 ?. 00Divide - into su"pro"lems @. 00find where to split the set 345 mid !"lo#$hi%h&'()* $$. 00solve the su"pro"lems. $2. mergesort (low#mid)6 $9. mergesort(mid5$#high)6 $:. 00com"ine the solutions . $;. merge(low#mid#high)6 $<. A $>. A Al%orithm: Nerging 2 sorted su"arrays using au+iliary storage. $. Algorithm merge(low#mid#high) 2. 00aHlowFhighI is a glo"al array containing 9. 00two sorted su"sets in aHlowFmidI :. 00and in aHmid5$FhighI.'he goal is to merge these 2 sets into ;. 00a single set residing in aHlowFhighI."HI is an au+iliary glo"al array. <. 4 >. h&low6 (&low6 j&mid5$6 ?. while ((h%&mid) and (j%&high)) do @. 4 $B. if (aHhI%&aHjI) then $$. 4 $2. "H(I&aHhI6 $9. h & h5$6 $:. A $;. else $<. 4 $>. "H(I& aHjI6 $?. j&j5$6 $@. A 2B. (&(5$6 2$. A 22. if (h=mid) then 29. for !&j to high do 2:. 4 2;. "H(I&aH!I6 2<. (&(5$6 2>. A 2?. else 2@. for !&h to mid do 9B. 4 9$. "H(I&aH!I6 @

Design and Analysis of Algorithm ( www.jntuworld.com )

92. (&(5$6 99. A 9:. for !&low to high do aH!I & "H!I6 9;. A ,onsider the array of $B elements aH$F$BI &(9$B# 2?;# $>@# <;2# 9;$# :29# ?<$# 2;:# :;B# ;2B) Algorithm Nergesort "egins "y splitting aHI into 2 su" arrays each of si1e five (aH$F;I and aH<F$BI). 'he elements in aH$F;I are then split into 2 su" arrays of si1e 9 (aH$F9I ) and 2(aH:F;I) 'hen the items in a aH$F9I are split into su" arrays of si1e 2 aH$F2I 8 one(aH9F9I) 'he 2 values in aH$F2A are split to find time into one-element su" arrays# and now the merging "egins. (9$BP 2?;P $>@P <;2# 9;$P :29# ?<$# 2;:# :;B# ;2B) 7here vertical "ars indicate the "oundaries of su" arrays. Elements aH(I and aH2I are merged to yield# (2?;# 9$BP$>@P<;2# 9;$P :29# ?<$# 2;:# :;B# ;2B) 'hen aH9I is merged with aH$F2I and ($>@# 2?;# 9$BP <;2# 9;$P :29# ?<$# 2;:# :;B# ;2B) Me+t# elements aH:I 8 aH;I are merged. ($>@# 2?;# 9$BP 9;$# <;2 P :29# ?<$# 2;:# :;B# ;2B) And then aH$F9I 8 aH:F;I ($>@# 2?;# 9$B# 9;$# <;2P :29# ?<$# 2;:# :;B# ;2B) Depeated recursive calls are invo!ed producing the following su" arrays. ($>@# 2?;# 9$B# 9;$# <;2P :29P ?<$P 2;:P :;B# ;2B) Elements aH<I 8aH>I are merged. 'hen aH?I is merged with aH<F>I ($>@# 2?;# 9$B# 9;$# <;2P 2;:#:29# ?<$P :;B# ;2B) Me+t aH@I 8aH$BI are merged# and then aH<F?I 8 aH@F$BI ($>@# 2?;# 9$B# 9;$# <;2P 2;:# :29# :;B# ;2B# ?<$ )

$B

Design and Analysis of Algorithm ( www.jntuworld.com )

At this point there are 2 sorted su" arrays 8 the final merge produces the fully sorted result. ($>@# 2;:# 2?;# 9$B# 9;$# :29# :;B# ;2B# <;2# ?<$) (f the time for the merging operations is proportional to n# then the computing time for merge sort is descri"ed "y the recurrence relation. '(n) & 4 a 2'(n02)5cn n&$#a a constant n=$#c a constant.

7hen n is a power of 2# n& 2C!# we can solve this e uation "y successive su"stitution. '(n) &2(2'(n0:) 5cn02) 5cn & :'(n0:)52cn & :(2'(n0?)5cn0:)52cn J J & 2C! '($)5!,n. & an 5 cn log n. (t is easy to see that if sC!%n%&2C!5$# then '(n)%&'(2C!5$). 'herefore# T"n& O"n lo% n&

QUIC6 SORT
'he divide-and-con uer approach can "e used to arrive at an efficient sorting method different from merge sort. (n merge sort# the file aH$FnI was divided at its midpoint into su" arrays which were independently sorted 8 later merged. (n Quic! sort# the division into 2 su" arrays is made so that the sorted su" arrays do not need to "e merged later. 'his is accomplished "y rearranging the elements in aH$FnI such that aH(I%&aHjI for all ( "etween $ 8 n and all j "etween (m5$) 8 n for some m# $%&m%&n. 'hus the elements in aH$FmI 8 aHm5$FnI can "e independently sorted. Mo merge is needed. 'his rearranging is referred to as partitioning.

$$

Design and Analysis of Algorithm ( www.jntuworld.com )

*unction partition of Algorithm accomplishes an in-place partitioning of the elements of aHmFp-$I (t is assumed that aHpI=&aHmI and that aHmI is the partitioning element. (f m&$ 8 p-$&n# then aHn5$I must "e defined and must "e greater than or e ual to all elements in aH$FnI 'he assumption that aHmI is the partition element is merely for convenience# other choices for the partitioning element than the first item in the set are "etter in practice. 'he function interchange (a#(#j) e+changes aH(I with aHjI.

Al%orithmF -artition the array aHmFp-$I a"out aHmI $. Algorithm -artition(a#m#p) 2. 00within aHmI#aHm5$I#3..#aHp-$I the elements 9. 00 are rearranged in such a manner that if :. 00initially t&aHmI#then after completion ;. 00aH I&t for some "etween m and <. 00p-$#aH!I%&t for m%&!% # and >. 00aH!I=&t for %!%p. is returned ?. 00.et aHpI&infinite. @. 4 $B. v&aHmI6(&m6j&p6 $$. repeat $2. 4 $9. repeat $:. (&(5$6 $;. until(aH(I=&v)6 $<. repeat $>. j&j-$6 $?. until(aHjI%&v)6 $@. if ((%j) then interchange(a#i.j)6 2B. Auntil((=&j)6 2$. aHmI&aHjI6 aHjI&v6 22. retun j6 29. A $. 2. 9. :. ;. <. Algorithm (nterchange(a#(#j) 00E+change aH(I with aHjI 4 p&aH(I6 aH(I&aHjI6 aHjI&p6 $2

Design and Analysis of Algorithm ( www.jntuworld.com )

>. A Al%orithm: .orting "y -artitioning $. Algorithm Quic!sort(p# ) 2. 00.ort the elements aHpI#3.aH I which resides 9. 00is the glo"al array aH$FnI into ascending :. 00order6 aHn5$I is considered to "e defined ;. 00 and must "e =& all the elements in aH$FnI <. 4 >. if(p% ) then 00 (f there are more than one element ?. 4 @. 00 divide p into 2 su"pro"lems $B. j&partition(a#p# 5$)6 $$. 00j is the position of the partitioning element. $2. 00solve the su"pro"lems. $9. uic!sort(p#j-$)6 $:. uic!sort(j5$# )6 $;. 00'here is no need for com"ining solution. $<. A $>. A Decord -rogramF Quic! .ort Rinclude %stdio.h= Rinclude %conio.h= int aH2BI6 main() 4 int n#(6 clrscr()6 printf(SQT(,U .)D'V)6 printf(SWn Enter the no. of elements S)6 scanf(SXdV#8n)6 printf(SWnEnter the array elementsV)6 for((&B6(%n6(55) scanf(SXdV#8aH(I)6 uic!sort(B#n-$)6 printf(SWn'he array elements areV)6 for((&B6(%n6(55) printf(SWnXdV#aH(I)6 getch()6 A uic!sort(int p# int ) 4 int j6 $9

Design and Analysis of Algorithm ( www.jntuworld.com )

if(p# ) 4 j&partition(p# 5$)6 uic!sort(p#j-$)6 uic!sort(j5$# )6 A A -artition(int m# int p) 4 int v#(#j6 v&aHmI6 i&m6 j&p6 do 4 do i&i5$6 while(aHiI%v)6 if (i%j) interchange((#j)6 A while ((%j)6 aHmI&aHjI6 aHjI&v6 return j6 A (nterchange(int (# int j) 4 int p6 p& aH(I6 aH(I&aHjI6 aHjI&p6 A )utputF Enter the no. of elements ; Enter the array elements 9 ? $ ; 2 'he sorted elements are# $ $:

Design and Analysis of Algorithm ( www.jntuworld.com )

2 9 ; ?

STRASSON7S -ATRI8 -U9TI+9ICAION


Get A and / "e the 2 nJn Natri+. 'he product matri+ ,&A/ is calculated "y using the formula# , (i #j )& A(i#!) /(!#j) for all i and and j "etween $ and n. 'he time comple+ity for the matri+ Nultiplication is )(nC9). Divide and con uer method suggest another way to compute the product of nJn matri+. 7e assume that M is a power of 2 .(n the case M is not a power of 2 #then enough rows and columns of 1ero can "e added to "oth A and / ..) that the resulting dimension are the powers of two. (f n&2 then the following formula as a computed using a matri+ multiplication operation for the elements of A 8 /. (f n=2#'hen the elements are partitioned into su" matri+ n02Jn02..since n is a power of 2 these product can "e recursively computed using the same formula .'his Algorithm will continue applying itself to smaller su" matri+ until MV "ecome suita"le small(n&2) so that the product is computed directly . 'he formula are A$2 J A2$ A2$ /2$ /22 /$$ /$2 ,$$ & ,2$ ,$2 ,22

A$$

,$$ & A$$ /$$ 5 A$2 /2$ ,$2 & A$$ /$2 5 A$2 /22 ,2$ & A2$ /$$ 5 A22 /2$ ,22 & A2$ /$2 5 A22 /22 $;

Design and Analysis of Algorithm ( www.jntuworld.com )

*or ELF :J:& 2222 2222 2222 2222 J $ $ $ $ $ $$ $$ $ $$ $ $$ $

'he Divide and con uer method

2 2 2 2

2 2 2 2

2 2 2 2

2 2 2 2

$ $ $ $

$ $ $ $

$ $ $ $

$ $ $ $

&

: : : :

: : : :

: : : :

: : : :

'o compute A/ using the e uation we need to perform ? multiplication of n02Jn02 matri+ and from : addition of n02Jn02 matri+. ,i#j are computed using the formula in e uation : As can "e sum -# Q# D# .# '# T# and K can "e computed using > Natri+ multiplication and $B addition or su"traction. 'he ,ij are re uired addition ? addition or su"traction. " >'(n02)5anC2 n%&2 a 8" are n=2 constant

'(n)&

*inally we get '(n) &)( n Clog2>) E+ample : : J : : : : -&(:J:)5(:5:)&<: Q&(:5:):&92 D&:(:-:)&B .&:(:-:)&B '&(:5:):&92 T&(:-:)(:5:)&B K&(:-:)(:5:)&B ,$$&(<:5B-925B)&92 $< : :

Design and Analysis of Algorithm ( www.jntuworld.com )

,$2&B592&92 ,2$&925B&92 ,22&<:5B-925B&92 .o the answer c(i#j) is 92 92

92 92 since n02n02 8matri+ can "e can "e added in ,n for some constant ,# 'he overall computing time '(n) of the resulting divide and con uer algorithm is given "y the se uence. '(n)& " ?'(n02)5cnC2 n%&2 a 8" are n=2 constant

'hat is '(n)&)(nC9) J Natri+ multiplication are more e+pensive then the matri+ addition )(nC9).7e can attempt to reformulate the e uation for ,ij so as to have fewer multiplication and possi"ly more addition . .tressen has discovered a way to compute the ,ij of e uation (2) using only > multiplication and $? addition or su"traction. .trassens formula are

-& (A$$5A$2)(/$$5/22) Q& (A$25A22)/$$ D& A$$(/$2-/22) .& A22(/2$-/$$) '& (A$$5A$2)/22 T& (A2$-A$$)(/$$5/$2) K& (A$2-A22)(/2$5/22) ,$$&-5.-'5K ,Y2&D5t ,2$&Q5' ,22&-5D-Q5K

GREEDY -ETHOD
Greedy method is the most straightforward designed techni ue. As the name suggest they are short sighted in their approach ta!ing decision on the "asis of the information immediately at the hand without worrying a"out the effect these decision may have in the future. $>

Design and Analysis of Algorithm ( www.jntuworld.com )

DEFINITION: A pro"lem with M inputs will have some constraints .any su"sets that satisfy these constraints are called a feasi"le solution. A feasi"le solution that either ma+imi1e can minimi1e a given o"jectives function is called an optimal solution.

Control al%orithm ,or Greed: -ethod: $.Algorithm Greedy (a#n) 2.00aH$FnI contain the n inputs 9. 4 :.solution &B600(nitialise the solution. ;.*or i&$ to n do <.4 >.+&select(a)6 ?.if(feasi"le(solution#+))then @.solution&union(solution#+)6 $B.A $$.return solution6 $2.A J 'he function select an input from aHI and removes it. 'he select input value is assigned to L. *easi"le is a /oolean value function that determines whether L can "e included into the solution vector. 'he function Tnion com"ines L with 'he solution and updates the o"jective function. 'he function Greedy descri"es the essential way that a greedy algorithm will once a particular pro"lem is chosen ands the function su"set# feasi"le 8 union are properly implemented.

E+ample .uppose we have in a country the following coins are availa"le F Dollars($BB cents) Quarters(2; cents) Dimes( $B cents) Mic!el(; ,ents) -ennies($ cent) )ur aim is paying a given amount to a customer using the smallest possi"le num"er of coins. $?

Design and Analysis of Algorithm ( www.jntuworld.com )

*or e+ample if we must pay 2>< cents possi"le solution then#

$ doll5> 5 $ pen@ coins 2 doll 59Q 5$ pen< coins 2 doll5>dim5$ nic 5$ pen$$ coins.

6NA+SAC6 +ROB9E we are given n o"jects and !napsac! or "ag with capacity N o"ject ( has a weight 7i where ( varies from $ to M. 'he pro"lem is we have to fill the "ag with the help of M o"jects and the resulting profit has to "e ma+imum. *ormally the pro"lem can "e stated as Na+imi1e +ipi su"ject to Li7i%&N 7here Li is the fraction of o"ject and it lies "etween B to $. 'here are so many ways to solve this pro"lem# which will give many feasi"le solution for which we have to find the optimal solution. /ut in this algorithm# it will generate only one solution which is going to "e feasi"le as well as optimal. *irst# we find the profit 8 weight rates of each and every o"ject and sort it according to the descending order of the ratios. .elect an o"ject with highest p0w ratio and chec! whether its height is lesser than the capacity of the "ag. (f so place $ unit of the first o"ject and decrement .the capacity of the "ag "y the weight of the o"ject you have placed. Depeat the a"ove steps until the capacity of the "ag "ecomes less than the weight of the o"ject you have selected .in this case place a fraction of the o"ject and come out of the loop. 7henever you selected.

ALGORITHM: $.Algorityhm Greedy !napsac! (m#n) $@

Design and Analysis of Algorithm ( www.jntuworld.com )

200-H$FnI and the wH$FnIcontain the profit 9.00 8 weight res.of the n o"ject ordered. :.00such that pHiI0wHiI =&pHi5$I07Hi5$I ;.00n is the Unapsac! si1e and +H$FnI is the solution verte+. <.4 >.for (&$ to n do aH(I&B.B6 ?.T&n6 @.*or (&$ to n do $B.4 $$.if (wHiI=u)then "rea!6 $9.+HiI&$.B6T&T-wHiI $:.A $;.if(i%&n)then +HiI&T0wHiI6 $<.A E.am;le: ,apacity&2B M&9 #N&2B 7i&$?#$;#$B -i&2;#2:#$; -i07i&2;0$?&$.9<#2:0$;&$.<#$;0$B&$.; Descending )rder -i07i$.< $.; $.9< -i & 2: $; 2; 7i & $; $B $? Li & $ ;0$B B -iLi&$J2:5B.;J$;9$.; 'he optimal solution is 9$.; X1 X2 X3 Z $09 [ $ 20; B B 209 $ B $ Z WiXi $<.< 2B 2B 2B PiXi 2:.2; $?.2 9$ 9$.;

)f these feasi"le solution .olution : yield the Na+ profit .As we shall soon see this solution is optimal for the given pro"lem instance. <OB SCHEDU9ING =ITH DEAD 9INES

2B

Design and Analysis of Algorithm ( www.jntuworld.com )

'he pro"lem is the num"er of jo"s# their profit and deadlines will "e given and we have to find a se uence of jo"# which will "e completed within its deadlines# and it should yield a ma+imum profit. +oint> To remem?er: 'o complete a jo"# one has to process the jo" or a action for one unit of time. )nly one machine is availa"le for processing jo"s. A feasi"le solution for this pro"lem is a su"set of j of jo"s such that each jo" in this su"ject can "e completed "y this deadline. (f we select a jo" at that time # .ince one jo" can "e processed in a single m0c. 'he other jo" has to "e in its waiting state until the jo" is completed and the machine "ecomes free. .o the waiting time and the processing time should "e less than or e ual to the dead line of the jo". A9GORITH-: Algorithm \.(d#j#n) 00'he jo" are ordered such that pH$I=pH2I3=pHnI 00jHiI is the ith jo" in the optimal solution 00 Also at terminal d H \H iI%&dH \ 4i5$I#$%i%! 4 dHBI& \HBI&B6 \H$I&$6 U&$6 *or ( &$ to n do 4 00 consider jo"s in non increasing order of -H(I6find the position for ( and chec! feasi"ility insertion r&!6 while((dH\HrII=dHiI )and (dH\HrII & r)do r &r-$6 if (dH\HrII%dH(I)and (dH(I=r))then 4 for &! to (r5$) step ]$ do \ H 5$I&jH I \Hr5$I&i6 U&!5$6 A A return !6 A E.am;le : 2$

Design and Analysis of Algorithm ( www.jntuworld.com )

$. n&; (-$#-2#3-;)&(2B#$;#$B#;#$) (d$#d23.d9)&(2#2#$#9#9) Feasible solution ($) (2) (9) (:) (;) ($#2) ($#9) ($#:) ($#;) (2#9) (2#:) (2#;) ($#2#9) ($#2#:) 'he .olution $9 is optimal 2. n&: (-$#-2#3-:)&($BB#$B#$;#2>) (d$#d23.d:)&(2#$#2#$) P o!essin" #e$uen!e (2#$) ($#9) (:#$) (@#9) (:#2) (:#9) ($) (2) (9) (:) %alue $$B $$; $2> 2; 9> :2 $BB $B $; 2> P o!essin" #e$uen!e ($) (2) (9) (:) (;) (2#$) (9#$) ($#:) ($#;) (9#2) (2#:) (2#;) (9#2#$) ($#2#:) %alue 2B $; $B ; $ 9; 9B 2; 2$ 2; 2B $< :; :B

Feasible solution ($#2) ($#9) ($#:) (2#9) (2#:) (9#:) ($) (2) (9) (:)

'he solution 9 is optimal.

-INI-U- S+ANNING TREE


22

Design and Analysis of Algorithm ( www.jntuworld.com )

Get G(K#E) "e an undirected connected graph with vertices v and edge E. A su"-graph t&(K#E) of the G is a .panning tree of G iff t is a tree.9 'he pro"lem is to generate a graph G& (K#E) where E is the su"set of E#G is a Ninimum spanning tree. Each and every edge will contain the given non-negative length .connect all the nodes with edge present in set E and weight has to "e minimum.

NOTE: 7e have to visit all the nodes. 'he su"set tree (i.e) any connected graph with M vertices must have at least M-$ edges and also it does not form a cycle. De&inition: A spanning tree of a graph is an undirected tree consisting of only those edge that are necessary to connect all the vertices in the original graph. A .panning tree has a property that for any pair of vertices there e+ist only one path "etween them and the insertion of an edge to a spanning tree form a uni ue cycle. A;;li@ation o, the >;annin% tree: $. Analysis of electrical circuit. 2. .hortest route pro"lems. -inim/m @o>t >;annin% tree: 'he cost of a spanning tree is the sum of cost of the edges in that trees. 'here are 2 method to determine a minimum cost spanning tree are $. Urus!als Algorithm 2. -roms Algorithm. 6RUS6A97S A9GORITH-: (n !rus!al^s algorithm the selection function chooses edges in increasing order of length without worrying too much a"out their connection to previously chosen edges# e+cept that never to form a cycle. 'he result is a forest of trees that grows until all the trees in a forest (all the components) merge in a single tree. (n this algorithm# a minimum cost-spanning tree ' is "uilt edge "y edge. Edge are considered for inclusion in ' in increasing order of their cost. An edge is included in ' if it doesnt form a cycle with edge already in '. 29

Design and Analysis of Algorithm ( www.jntuworld.com )

'o find the minimum cost spanning tree the edge are inserted to tree in increasing order of their cost

Al"o it'(: Algorithm !rus!al(E#cost#n#t) 00Eset of edges in G has n vertices. 00costHu#vIcost of edge (u#v).tset of edge in minimum cost spanning tree 00 the first cost is returned. 4 for i&$ to n do parentH(I&-$6 (&B6mincost&B.B6 7hile(((%n-$)and (heap not empty)) do 4 j&find(n)6 !&find(v)6 if(j not e ual !) than 4 i&i5$ tHi#$I&u6 tHi#2I&v6 mincost&mincost5costHu#vI6 union(j#!)6 A A if(i note ual n-$) then write(SMo spanning treeV) else return minimum cost6 A Analysis 'he time comple+ity of minimum cost spanning tree algorithm in worst case is )(PEPlogPEP)# where E is the edge set of G. E.am;le: Ste; ?: Ste; o;eration o, 6/r>Aal al%orithm5 .tep $. (n the graph# the Edge(g# h) is shortest. Either verte+ g or verte+ h could "e representative. Gets choose verte+ g ar"itrarily.

2:

Design and Analysis of Algorithm ( www.jntuworld.com )

.tep 2. 'he edge (c# i) creates the second tree. ,hoose verte+ c as representative for second tree.

.tep 9. Edge (g# g) is the ne+t shortest edge. Add this edge and choose verte+ g as representative.

.tep :. Edge (a# ") creates a third tree.

.tep ;. Add edge (c# f) and merge two trees. Kerte+ c is chosen as the representative.

2;

Design and Analysis of Algorithm ( www.jntuworld.com )

.tep <. Edge (g# i) is the ne+t ne+t cheapest# "ut if we add this edge a cycle would "e created. Kerte+ c is the representative of "oth.

.tep >. (nstead# add edge (c# d).

.tep ?. (f we add edge (h# i)# edge(h# i) would ma!e a cycle.

.tep @. (nstead of adding edge (h# i) add edge (a# h).

2<

Design and Analysis of Algorithm ( www.jntuworld.com )

.tep $B. Again# if we add edge ("# c)# it would create a cycle. Add edge (d# e) instead to complete the spanning tree. (n this spanning tree all trees joined and verte+ c is a sole representative.

+RI-BS A9GORITH.tart from an ar"itrary verte+ (root). At each stage# add a new "ranch (edge) to the tree already constructed6 the algorithm halts when all the vertices in the graph have "een reached.

Al%orithm prims(e#cost#n#t) 4 Get (!#l) "e an edge of minimum cost in E6 Nincost F&costH!#lI6 'H$#$IF&!6 tH$#2IF&l6 *or (F&$ to n do (f (costHi#lI%costHi#!I) then nearHiIF&l6 Else nearHiIF&!6 2>

Design and Analysis of Algorithm ( www.jntuworld.com )

MearH!IF&nearHlIF&B6 *or iF&2 to n-$ do 4 Get j "e an inde+ such that nearHjI_B and ,ostHj#nearHjII is minimum6 'Hi#$IF&j6 tHi#2IF&nearHjI6 NincostF&mincost5 ,ostHj#nearHjII6 MearHjIF&B6 *or !F&B to n do (f near((nearH!I_B) and (,ostH!#nearH!II=costH!#jI)) then MearH!IF&j6 A Deturn mincost6 A 'he prims algorithm will start with a tree that includes only a minimum cost edge of G. 'hen# edges are added to the tree one "y one. the ne+t edge (i#j) to "e added in such that ( is a verte+ included in the tree# j is a verte+ not yet included# and cost of (i#j)# costHi#jI is minimum among all the edges. 'he wor!ing of prims will "e e+plained "y following diagram

Ste; 3:

Ste; (:

Ste; 1:

Ste; C:

2?

Design and Analysis of Algorithm ( www.jntuworld.com )

Ste; D:

Ste; E:

SING9E SOURCE SHORTEST +ATH


Sin%le->o/r@e >horte>t ;ath: Graphs can "e used to represent the highway structure of a state or country with vertices representing cities and edges representing sections of highway. 'he edges can then "e assigned weights which may "e either the distance "etween the two cities connected "y the edge or the average time to drive along that section of highway. A motorist wishing to drive from city A to / would "e interested in answers to the following uestionsF $. (s there a path from A to /` 2. (f there is more than one path from A to /` 7hich is the shortest path`

2@

Design and Analysis of Algorithm ( www.jntuworld.com )

'he pro"lems defined "y these uestions are special case of the path pro"lem we study in this section. 'he length of a path is now defined to "e the sum of the weights of the edges on that path. 'he starting verte+ of the path is referred to as the source and the last verte+ the destination. 'he graphs are digraphs representing streets. ,onsider a digraph G&(K#E)# with the distance to "e traveled as weights on the edges. 'he pro"lem is to determine the shortest path from vB to all the remaining vertices of G. (t is assumed that all the weights associated with the edges are positive. 'he shortest path "etween vB and some other node v is an ordering among a su"set of the edges. Oence this pro"lem fits the ordering paradigm. E+ampleF ,onsider the digraph of fig >-$. Get the num"ers on the edges "e the costs of travelling along that route. (f a person is interested travel from v$ to v2# then he encounters many paths. .ome of them are $. 2. 9. :. v$ v2 & ;B units v$ v9 v: v2 & $B5$;52B&:; units v$ v; v: v2 & :;59B52B& @; units v$ v9 v: v; v: v2 & $B5$;59;59B52B&$$B units 'he cheapest path among these is the path along v$ v9 v: v2. 'he cost of the path is $B5$;52B & :; units. Even though there are three edges on this path# it is cheaper than travelling along the path connecting v$ and v2 directly i.e.# the path v$ v2 that costs ;B units. )ne can also notice that# it is not possi"le to travel to v< from any other node. 'o formulate a greedy "ased algorithm to generate the cheapest paths# we must conceive a multistage solution to the pro"lem and also of an optimi1ation measure. )ne possi"ility is to "uild the shortest paths one "y one. As an optimi1ation measure we can use the sum of the lengths of all paths so far generated. *or this measure to "e minimi1ed# each individual path must "e of minimum length. (f we have already constructed i shortest paths# then using this optimi1ation measure# the ne+t path to "e constructed should "e the ne+t shortest minimum length path. 'he greedy way to generate these paths in nondecreasing order of path length. *irst# a shortest path to the nearest verte+ is generated. 'hen a shortest path to the second nearest verte+ is generated# and so on. A much simpler method would "e to solve it using matri+ representation. 'he steps that should "e followed is as follows# .tep $F find the adjacency matri+ for the given graph. 'he adjacency matri+ for fig >.$ is given "elow

9B

Design and Analysis of Algorithm ( www.jntuworld.com )

K$ K$ K2 K9 K: K; K< (nf 2B (nf (nf (nf

K2 ;B (nf 2B (nf (nf

K9 $B $; (nf (nf (nf

K: (nf (nf $; 9B 9

K; :; $B inf 9; (nf

K< (nf (nf (nf (nf (nf -

.tep 2F consider v$ to "e the source and choose the minimum entry in the row v$. (n the a"ove ta"le the minimum in row v$ is $B. .tep 9F find out the column in which the minimum is present# for the a"ove e+ample it is column v9. Oence# this is the node that has to "e ne+t visited. .tep :F compute a matri+ "y eliminating v$ and v9 columns. (nitially retain only row v$. 'he second row is computed "y adding $B to all values of row v9. 'he resulting matri+ is K2 K$ Kw K$ K9 Kw Ninimum ;B $B5inf ;B K: (nf $B5$; 2; K; :; $B5inf :; K< (nf $B5inf inf

.tep ;F find the minimum in each column. Mow select the minimum from the resulting row. (n the a"ove e+ample the minimum is 2;. Depeat step 9 followed "y step : till all vertices are covered or single column is left. 'he solution for the fig >.$ can "e continued as follows K2 K$ Kw ;B K; :; K< (nf

9$

Design and Analysis of Algorithm ( www.jntuworld.com )

K$ Kw

K9

K: 2;52B :;

2;59; :;

2;5inf inf

Ninimum

K; K$ Kw :;

K< (nf :;5inf inf K<

K$ K9 K: K2 :;5$B Kw Ninimum :;

K$ Kw

(nf

K$ K9 K: K2 K; :;5inf Kw Ninimum inf

*inally the cheapest path from v$ to all other vertices is given "y K$ K9 K: K2 K;.

92

Vous aimerez peut-être aussi