You are on page 1of 4

CS3110Lecture20

Recursiontreesandmastermethodforrecurrence
relations
Note:thispageusesthefollowingspecialcharacters:Greekcapitallettertheta:(),Greekcapitalletter
omega(),minussign().Ifthesecharactersdonotappearcorrectly,yourbrowserisnotabletofullyhandle
HTML4.0,andsomeofthefollowingtextwilllikelynothavethecorrectappearance.

Recursiontrees
Arecursiontreeisusefulforvisualizingwhathappenswhenarecurrenceisiterated.It
diagramsthetreeofrecursivecalls,andtheamountofworkdoneateachcall.
Forinstanceconsidertherecurrence
T(n)=2T(n/2)+n2.
Therecursiontreeforthisrecurrenceisofthefollowingform:
|n2
|/\
|(n/2)2(n/2)2
height=|/\/\
lgn|(n/4)2(n/4)2(n/4)2(n/4)2
|/\/\/\/\
|.
|.
|.

Generallyitisstraightforwardtosumacrosseachrowofthetree,toobtainthetotalwork
doneatagivenlevel:
|n2n2
|/\
|(n/2)2(n/2)2(1/2)n2
height=|/\/\
lgn|(n/4)2(n/4)2(n/4)2(n/4)2(1/4)n2
|/\/\/\/\
|.
|.
|.

Thisisageometricseries,andthusinthelimitthesumisO(n2).Inotherwordsthedepthof
thetreeinthiscasedoesnotreallymatter,theamountofworkateachlevelisdecreasingso
quicklythatthetotalisonlyaconstantfactormorethantheroot.
Recursiontreescanbeusefulforgainingintuitionintotheclosedformofarecurrence,but
arenotaproof(andinfactitiseasytogetthewronganswerwitharecursiontree,asisthe
casewithanymethodthatincludes''...''kindsofreasoning).Aswesawlasttime,agoodway
ofestablishingaclosedformforarecurrenceistoguessananswerandthenproveby
inductionthattheansweriscorrect.Recurrencetreescanbeagoodmethodofguessingan

answer.
Let'sconsideranotherexample,
T(n)=T(n/3)+2T(n/3)+n.
Expandingoutthefirstfewlevels,therecurrencetreeis:
|nn
|/\
|(n/3)(2n/3)n
height=|/\/\
log3/2n|(n/9)(2n/9)(2n/9)(4n/9)n
|/\/\/\/\
|.
|.
|.

Notethatthetreehereisnotbalanced,thelongestpathkeepsreducingnbyafactorof2/3
andthusisoflengthlog3/2n.HenceourguessastotheclosedformofthisrecurrenceisO(n
lgn).

Themastermethod
Themastermethodisacookbookmethodforsolvingrecurrencesthatisveryhandyfor
dealingwithmanyrecurrencesseeninpractice.Supposeyouhavearecurrenceoftheform
T(n)=aT(n/b)+f(n).
Inotherwordswithasubproblemseachofsizen/b,wheretheworktosplittheproblem
intosubproblemsandrecombinetheresultsisf(n).
Wecanvisualizethisasarecurrencetree,wherethenodesinthetreehaveabranching
factorofa.Thetopnodehasworkf(n)associatedwithit,thenextlevelhasworkf(n/b)
associatedwitheachnode,thenextlevelhasworkf(n/b2)associatedwitheachnode,andso
on.Thetreehaslogbnlevels,sothetotalnumberofleavesinthetreeisalogbnwhich,asa
functionofnisnlogba.
Thetimetakenisjustthesumofthetermsf(n/bi)atallthenodes.Whatthissumlookslike
dependsonhowtheasymptoticgrowthoff(n)comparestotheasymptoticgrowthofthe
numberofleaves.Therearethreecases:
Case1:f(n)isO(nlogba).Sincetheleavesgrowfasterthanf,asymptoticallyallof
theworkisdoneattheleaves,andsoT(n)is(nlogba).
Case2:f(n)is(nlogba).Theleavesgrowatthesamerateash,sothesameorderof
workisdoneateverylevelofthetree.ThetreehasO(lgn)levels,timestheworkdone
ononelevel,yieldingT(n)is&Theta(nlogbalgn).
Case3:f(n)is(nlogba+).Inthiscasewealsoneedtoshowthataf(n/b)kf(n)for
someconstantkandlargen,whichmeansthatfgrowsfasterthanthenumberof
leaves.Asymptoticallyalloftheworkisdoneattherootnode,soT(n)is(f(n)).

Notethatthemastermethoddoesnotalwaysapply.Infactthesecondexampleconsidered
above,wherethesubproblemsizesareunequal,isnotcoveredbythemastermethod.
Let'slookatafewexampleswherethemastermethoddoesapply.
Example1Considertherecurrence
T(n)=4T(n/2)+n.
Forthisrecurrence,therearea=4subproblems,eachdividingtheinputbyb=2,andthe
workdoneoneachcallisf(n)=n.Thusnlogbaisn2,andf(n)isO(n2)for=1,andCase1
applies.ThusT(n)is(n2).
Example2Considertherecurrence
T(n)=4T(n/2)+n2.
Forthisrecurrence,thereareagaina=4subproblems,eachdividingtheinputbyb=2,but
nowtheworkdoneoneachcallisf(n)=n2.Againnlogbaisn2,andf(n)isthus(n2),soCase
2applies.ThusT(n)is(n2lgn).Notethatincreasingtheworkoneachrecursivecallfrom
lineartoquadratichasincreasedtheoverallasymptoticrunningtimeonlybyalogarithmic
factor.
Example3Considertherecurrence
T(n)=4T(n/2)+n3.
Forthisrecurrence,thereareagaina=4subproblems,eachdividingtheinputbyb=2,but
nowtheworkdoneoneachcallisf(n)=n3.Againnlogbaisn2,andf(n)isthus(n2+)for=1.
Moreover,4(n/2)3kn3fork=1/2,soCase3applies.ThusT(n)is(n3).

Example:Yetanothersortingalgorithm
Thefollowingfunctionsortsthefirsttwothirdsofalist,thenthesecondtwothirds,thenthe
firsttwothirdsagain:
letrecsort3a=
matchawith
[]>[]
|[x]>[x]
|[xy]>[(minxy)(maxxy)]
|_>

letn=List.length(a)in

letm=(2*n+2)/3in

letres1=sort3(takeam)@(dropam)in

letres2=(takeres1(nm))@sort3(dropres1(nm))in

sort3(takeres2m)@(dropres2m)

Perhapssurprisingly,thisalgorithmdoessortthelist.Weleavetheproofthatitsorts
correctlyasanexercisetothereader.Thekeyistoobservethatthefirsttwopassesensure
thatthetailofthefinallistdoescontainthecorrectelementsinthecorrectorder.
Therunningtimeofthealgorithmwecanderivefromitsrecurrence.Theroutinedoes
someO(n)workandthenmakesthreerecursivecallsonlistsoflength2n/3.Thereforeits
recurrenceis:

T(n)=cn+3T(2n/3)
Ifweapplythemastermethodtothesort3algorithm,weseeeasilythatweareincase1,
sothealgorithmisO(nlog3/23)=O(n2.71),makingitevenslowerthaninsertionsort!Note
thatthefactthatCase1appliesmeansthatimprovingf(n)willnotimprovetheoveralltime.
Forinstancereplacinglistswitharraysimprovesf(n)toconstantfromlineartime,butthe
asymptoticcomplexityisstillO(n2.71).

You might also like