0% found this document useful (0 votes)
5 views18 pages

DAA - Module1 - Part3 - Recurrence Relation

The document discusses recurrence relations, which express the time complexity of algorithms recursively based on previous values, commonly used in algorithm design and analysis. It provides examples and methods for solving recurrence relations, including the substitution method, iteration method, recursion tree method, and the master method. The document illustrates these concepts with various examples and their corresponding solutions, highlighting the asymptotic bounds of different recurrences.

Uploaded by

pd7300144
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

DAA - Module1 - Part3 - Recurrence Relation

The document discusses recurrence relations, which express the time complexity of algorithms recursively based on previous values, commonly used in algorithm design and analysis. It provides examples and methods for solving recurrence relations, including the substitution method, iteration method, recursion tree method, and the master method. The document illustrates these concepts with various examples and their corresponding solutions, highlighting the asymptotic bounds of different recurrences.

Uploaded by

pd7300144
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 18
srt, 12:12 PM Untitled document - Google Docs Recurrences Relation A recurrence relation is a way of expressing the time complexity of an algorithm by recursively defining the value of a function in terms of its previous values. Recurrence relations are commonly used in algorithm design and analysis to understand the time complexity of recursive algorithms or algorithms that can be broken down into subproblems. For example in Merge Sort, to sort a given array, we divide it in two halves and recursively repeat the process for the two halves. Finally we merge the results. Time complexity of Merge Sort can be written as T(n) = 2T(w/2) + cn. There are many other algorithms like Binary Search, Tower of Hanoi, ete. There are several methods to solve recurrence relations. Here are some common methods: 1) Substitution Method: We make a guess for the solution and then we use mathematical induction to prove the guess is correct or incorrect. Examples 1. T(n) = 2T(n/2) +n ‘We guess the solution as T(n) = O(nLogn). Now we use induction to prove our guess. ‘We need to prove that T(n) <= ¢ nLogn. We can assume that it is true for values smaller than n. T(n) = 2T(/2) +n <= 2en/2Log(n/2) +n = enLogn - cnLog2 +n = enLogn-en +n <= enLogn = 0 (Logn) hitpsildocs.google.comidocumenié1-q@BAHY AR9nbnXaciZbs2-lgsuuDsINuAmUVeqhBFkledt 1. srt, 12:12 PM Untitled document - Google Docs example 2 Consider the recurrence 19-3) awe have to show that it is asymptotically bound by O(logn). Solution. For T (n) = O(log) we have to show that for some constant ¢ T(n) n=4! or islogyn 3n 90,270, , slogan Snt+—++——+... TV) Tin) sna Sts S24 2H 436" TC) ng 3% 490, 27m, glos4n gy 416° 64 n=2! or i=log, n T(n)= =o 4 Sa wie. 42 %82" 54) not ae, + n'98227(1) on fas 1 ] Sete [end 2-022 te T)=10) pi [teed ag [ig ]am $3n*,2411n S6n* +11n T(n)= O(n?) , Example 9 Solve the recurrence relation T(n)=1 for n=1 (UPTU MCA 2004-05) =2T(n-1) for ns1 SS UNUpler Ss) KEGURKENUES Bt ve Solution. T(n)=2T(n-1) =2[2T(n-2)]=4T(n-2) = 4[2T(n-3)] =8T(n-3) =2°T(n-3)=24T(n-4), Ingeneral T(n)=2'T(n-i) Put f=n—1we get T(n)=2""'T(1) 22" Example 10 Consider the recurrence T(n)=T(n-1)+1 and T(1)=0(1) Solve it Solution, T(n)=T(n-1)+1 =(T(n-2)+1)41=(T(n-3)41) 4141 =T(n-4)+4=T(n-5)+1+4 =T(n-5)+5=T(n-k)+k where k=n-1 ie, T(n-k)=T()=0() ie, T(n) = 0(1)-+(n-1)=1+n-1=n=0(n} Example 11 rm=1(2) Solution. T(n) =7(2)+ nf 2 nl? +(2) “ “@) rennet (a) (a) Ha) Let k =log, n when x slic, 3* =n 2 4 ke 3(_1 43 (1 3(_1 ina nf? +n (sis) (5) va (ae k i = (2 Sa (as) i=0 ar 4 # 102 Cn é 2 ey (a4? —> en(Ga)en oft bat ude ve |i ny \T=2, CoB" CF (OF (a0)? (gh at > | eae NIN ININ er) BW Chapter 9: RECURRENCES 55 pxample 14 Solve the following recurrence by using the recursion tree method : Ton=@r( 2) 2 Solution. The recursion tree for the above recurrence is (nz? “etimes (nia Tin?2): Tina) Thnly + Tina) Thad) Tin'a) 8 : as UN ‘sites UN > siniaf = an? (nia Sv (nia? (nig)? -(ni4y® ——> 8 nlaye = 2? - a 0 § i > ane caer © @(n) + T(n)=n? 42n? +270? +. AB = O(n?) Example 15 Solve the following recurrences : (UPTU B.Tech, 2003-04) T(n)=T(a.n+T-a)ntn On aw ae oN 09 iq ———™ SS me Forat= 0(0 109") a(i-an a(t-a)n (1-a)*n >? fs ALGORITHMS DESIGN AND ANALYSIS BI 56 cnt -«(é) Example 16 Consider the following recurrence T(n) & ([3 | obtain the asymptotic bound using recursion tee method. recursion tree for the above recurrence Solution. The mia) —Tinl2) ‘ n 2102) eo togn | a(n) aN wn aN ee Total = O(n) We have n+2n+4n+...log, n times =n(1+2+4+..log, ntimes) eA Chal -1) _n(n-1) =n —n=0(n?) @-). 4 T(n)=0(n?) Example 17 Consider the following recurrence Top=T(2\+ 1(2t)+n = 3) Obtain . ‘asymptotic bound using recursion tree method olution. The given recurrence has the following recursion tr. ee, Tn’8) Tn) ae Oe 7 © 2n3 in i 7 \ 4 NX oe 4n9 —»n Total = @(n 1og n) "3 Chapter 3: RECURRENCES = 57 When we add the values across the levels of the jion tree, eee Tre Tosgest balk treae te levela ofthe recursion tee, we get a value of nfo every 4392) n $n (2) ned ~~ Since @) n=1when i =loga/. 1. Thus the height of the tree is logy. 1. f T(t) sme. logy, 1 times. = 0(nlogn) Example 18 Solve the following recursion : noe) (ny (2 je7()e7( 2) en by reson tee methods | sy seam Solution. The given recurrence has the following recursion tree : T(2) Tin/4)—T(nl8) 1 => Te vo 2 2G n/32 neh =| ——> (78) 0 IMINIK.IKININ INNIS n 1 1 ' 1 2 rons neZne(Z) Thee eisZe(3) +] <8n T(n)=0(n). ALGORITHMS DESIGN AND ANALYSIS ®& 58 3.4 Master/Method The master method is used for solvin; ro =«7(2)+ f(x) with azland b>1 ize tt ubproblems, each of size ; where a and b are positive g the following type of recurrence : In this, problem is divided into ‘a’ s constants. The cost of dividing the problem and combining the results of the subproblems is described by the function f (1) The master method depends on the following theorem. Master Theorem (UPTU MCA 2012-13) Let T(n) be defined on the non-negative integers by the recurrence. Ton=er( 2) (0) where #21 and b>1 be constants and f(n) be a function and # can be interpreted as [z| or [ | : Then T(n) can be Bound asymptotically as follows : 1. IF f(n) = O(H84>*) for some constant e >0, then T(n)=0(n!98") ~ 2. IE f(n) =0(nF0* ) then T(n) = 0(n!°8" Ign) 3. If f(n)=2(n1®86***) for some constant ¢>0 and if a 1(2)se f(n) for some c<1 and all sufficiently Inge n: then T(n)=0( f(n)) a (se ‘f(n) is called the “regularity” condition, . NOTE x 7 para to note that the tree cases do not cover all the possiblities for f(n) There fen cases 1 and 2, When f(n)is smaller than n!°86¢ but not polynomiall oe a ae $8. gap between cases 2 and 3 when F(n)is larger than. ‘ 0 te nat plyonialy age. 7 cay (0) falls into one of these gaps, or if the replay old, the master method cannot be used to solve the aun Example 19 Solve the recurrence using master method, T(n) or x |. T(n) =97( 2) 4» :) Solution. Compare, the given recurrence with the (UPTU B.Tech,, 2005-06) Ton) =at( 2) Fn) We get a=9, b=3, f(ri)=n (Chapters: RECURRENCES 59 Now, P84 a0 on? 2 9(y2y S(n)enan'esa9 . 421 Hence we can apply case 1 and the solution is O(ni"®9®) a(n, Bxample 20 Solve T(n) =1(32)41 by master method, & solution Here a=1, v8, F(t z Bb 4 gs 9!83/21 2 0 F(n)=n'8", ence case 2 applies, Thus solution is T(n)=0(n'°85" Jpn) = (gn) Example 24 Solve the recurrence T(n) =3r(2)+ nalgn by master method. Solution. Herea=3, b=4 f(n)=nign. mB an'843 = O(497%) Ff (n)=O(n'843*#) where e=02 Case 3 applies, now for regularity condition i.e, o(2}sesoy a ioe( (3) tere f(r) for e=3, 4) Pa) 14 ee Therefore, the solution is T(n)=0(nlgn) Example 22 Solve Teop=16( 2) 0° Solution. By master theorem a=l6 b=4 f(nj=n® PB 4 a BAIS wy? fone? enti a? gat Now regularity condition /.e., ® 16 /(2) sefin) 4 16 son? for od | So regularity condition holds. Hence by case.3, the solution is T(n) =0( (1) =0(0°) gxample 25 Solve the following recurrence using Master method : T(n)=4T(n/3)+n2 Solution. T(n)=4T(n/3)+n? Here a=4 b=3 f(n)=n? (UPTU B.Tech 05, 11 ; MCA 13 ; AKTU B.Tech. 17-18) lb = pylogg4 _ 4126 n? =Q(n'%834*) for e>0 Now check 4. f(n/3)0. = O(n?) T(n)=0(n!%8*)=0(n?) Example 27 The solution to the recurrence (UPTU B.Tech. 2003-0 T(n)=100T (n/99)+log(n!) is T(x) = 0(nlogn) Solution. T(n)=100T (1/99) +log(n!) Here a=100 b=99 f(n)=log(n!) Now niBb* = 11899100 = nh002187 F (a) = log) = (n87 °°) Now we check, n 100 tog( 2!) |+0ea) Ratan toy nae, 2 2 sy Srp 3.) KEGUKKENCES. Bl “ (i) T(n)=T (Vn) +1 Put m= logn er => TR")=T(2")41 Now, Renaming S(m) = S(m/2)+1 [- S(m=TR"), Compare with Tn) = aT (n/b)+ f(n) Herea=1, b=2, f(n)=1 > mlog, @= ,,log, 1 =m? =1 » 8@) By case 2, S(mr) = (1 . logm) = 6(log(logn)) > T(n)=TQ")= S(mn) = @(loglogn) T(n) =O (loglogn) n Example 32 Solve T(n) -21(2)+ a a Solution. Toy =27(2)+ 2)" Ign <27 2. nlgn. G a T(n) =21(2)-2¢tem “This recurrence cannot be solved by master method, so using the iteration method, we get et ar(2\= p22" log, 2*) fa s 22! St ei =2! eH) «i 7 As k=logn = O(log? 1) So T(n) = O(nlog? n)-0(nlgn) =O(nlog? n) Because the term O(nlog? n) is greater than @(nlgn Example 33 Solve the recurrence T@=1 Ten=3( +2 ae n 15 Solution. We have T(r) -31(2) +2" Divide by 2, we get ; 1 =37(2}+ nf 2a Na Let oe and S| a=} = Solve the recurrence equation (3) with the help of master theorem Herea=3, b=2 f(n)=n'> HBO 823 9h F (ni) = n98b4-* =(7159-09) fe = 09 By case 1: S(n) =0(n') = O(n'°823) Hence T(n)=2 S()=O(n!*823) T(n)=O(n*823) * Example 34 Solve the following recurrences using suitable methods : @ T= 5T(n/2 +16) +n? @ Te)=TWh)4n Solution, © Tey=sr(z #16)? =5[on(z +8116) (2 wa) oe =5 r(2 *1648)s5( +16) na +16+8)=5 (2 s7(z B46) sary J + =s1(2 BTS844) s(n Now +1648)" ~(A) (aKTU 201415) HRLURRENUES il 73 Put this value, we get 2 | n 2 2 T(n)=5 [s1(g+16s8+4)(teress) |ss(g8) +n 2 2 2. =5°1(J16+8+4)+5°(241648) +8(2+16) $n 2 2 =5°7( 2fn n 5 1(Be16s8+4)+5 (21648) +5(Z+16) +n? Similarly we get 2 2 2 Tiop=st7( 116484442). 59( 3 164844) +5°(Zes1648) +3(Z+16) +n Multipliers of n? are ron) «(8) +(3) + ee |s[etsle" T(n)=0(n?). (i) T(n)=TWn)+n Let m=log, n n=2™ 2 T@™)=72@"7)+2" Let S(m)=T(2") S(1m) = S(m/2)+2" Using master theorem Herea=1, b=2, f(m)=2" m!98b4 = 821 = yy =1 mb" < f(m) So case 3 applies at(2) 5 om 1.2! <¢2™ condition satisfied. . By Master’s theorem $(m)=6(2") T2™)=02") > T(n)=0(n)

You might also like