Transcription of Solutions for Introduction to algorithms second edition
1 Merelya vaguesuggestiontoa solutiontosomeoftheexercisesposedinthebo okIntroductiontoalgo-rithmsbyCormen, is youhavefoundanerror, havea bettersolutionorwishtocontributeinsomeco nstructivewaypleasesenda is lastresortortocheckif yourinstructorgotit underconstructionandis updatedonlysporadically. ,PhilipBilleLastupdate:December9, 20021:2-2 Insertionsortbeatsmergesortwhen8n2< 64nlgn,)n < 8lgn,)2n=8< n. Thisis truefor26n643(foundbyusinga calculator). 107236 1082864 10822592 109294608 1010294608 1012pn101236 10141296 1016746496 10166718464 10188950673664 10208950673664 1024n1066 10736 108864 1082592 10994608 101094608 1012nlgn627462801417??????????n2103244948976 104293938160996830758413307584134n31023911532442013736981694556612n19253136414956n!911121315171822:1-2 Inline5ofINSERTION-SORT alterA[i]> keytoA[i]< :1-3 Algorithm1 LINEAR-SEARCH(A; v)Input:A=ha1; a2; : : : anianda :Anindexisuchthatv=A[i]ornilifv62 Afori 1tondoifA[i] =vthenreturniend ifend forreturnnilAsa loopinvariantwesaythatnoneoftheelementsa tindexA[1; : : : ; i-1] , allpropertiesarefull :2-1n3=1000-100n2-100n+3= (n3).
2 2:2-2 AssumethatFIND-MIN(A; r; s)returnstheindexofthesmallestelementinA betweenindicesrands. Clearly, thiscanbeimplementedinO(s-r)timeifr> (A)Input:A=ha1; a2; : : : 1ton-1doj FIND-MIN(A; i; n)A[j]$A[i]end forAsa loopinvariantwechoosethatA[1; : : : ; i-1] : nXi=1i!= (n2) :2-3 Giventhateachelementis equallylikelyto betheonesearchedforandtheelementsearched forispresentinthearray, a becausehalfthetimethewantedelementwillbe inthe rsthalfandhalfthetimeit willbeinthesecondhalf. Boththeworst-caseandaverage-caseofLINEAR -SEARCHis (n).32:2-4 Onecanmodifyanalgorithmtohavea best-caserunningtimebyspecializingit tohandlea best-caseinputef :3-5A recursiveversionofbinarysearchonanarray. Clearly, theworst-caserunningtimeis (lgn).Algorithm3 BINARY-SEARCH(A; v; p; r)Input:A sortedarrayAanda :Anindexisuchthatv=A[i] >randv6=A[p]thenreturnnilend ifj A[b(r-p)=2c]ifv=A[j]thenreturnjelseifv < A[j]thenreturnBINARY-SEARCH(A; v; p; j)elsereturnBINARY-SEARCH(A; v; j; r)end ifend if2:3-7 Givea (nlgn)timealgorithmfordeterminingif thereexisttwoelementsin ansetSwhosesumis (A; x)Input:AnarrayAanda :A booleanvalueindicatingif thereis SORT(A)n length[A]fori tondoifA[i]>0andBINARY-SEARCH(A; A[i] -x; 1; n)thenreturntrueend ifend forreturnfalseClearly, thisalgorithmdoesthejob.
3 (Itis assumedthatnilcannotbetrueintheif-statem ent.)43:1-1 Letf(n),g(n) (f(n); g(n)) = (f(n) +g(n)). Thismeansthatthereexistspositiveconstant sc1,c2andn0suchthat,06c1(f(n) +g(n))6max(f(n); g(n))6c2(f(n) +g(n))foralln> alwaysgreaterthantheweightedaverageoff(n )andg(n). Notethesigni canceofthe asymptoticallynonnegative rstinequalitycouldnotbesatis :1-42n+1=O(2n)since2n+1=2 2n62 2n! However22nis notO(2n): byde nitionwehave22n= (2n)2whichfornoconstantcasymptoticallyma ybelessthanorequaltoc (n)andg(n) ,f(n) =O(g(n))doesnotimplyg(n) =O(f(n)). Clearly,n=O(n2)butn26=O(n). ,f(n) +g(n)is not (min(f(n); g(n))). Asanexamplenoticethatn+16= (min(n; 1)) = (1). ,iff(n) =O(g(n))thenlg(f(n)) =O(lg(g(n)))providedthatf(n)>1andlg(g(n) )>1aregreaterthanorequal1. We havethat:f(n)6cg(n))lgf(n)6lg(cg(n)) =lgc+lgg(n)To showthatthisis smallerthanblgg(n)forsomeconstantbwesetl gc+lgg(n) =blgg(n).
4 Dividingbylgg(n)yields:b=lg(c) +lgg(n)lgg(n)=lgclgg(n)+16lgc+1 Thelastinequalityholdssincelgg(n)> ,f(n) =O(g(n))doesnotimply2f(n)=O(2g(n)). Iff(n) =2nandg(n) =nwehavethat2n62 nbutnot22n6c2nforanyconstantcbyexercise3 ,iff(n)< 1forlargenthenf(n)2< f(n) (n)> 1andthestatementis ,f(n) =O(g(n))impliesg(n) = (f(n)). We havef(n)6cg(n)forpositivecandthus1=c f(n)6g(n). ,clearly2n66c2n=2=cp2nforanyconstantcifn is suf smallmodi cationto exercise3:1-1wehavethatf(n)+o(f(n)) = (max(f(n); o(f(n))))= (f(n)).54:1-1 ShowthatT(n) =T(dn=2e) +1isO(lgn). UsingsubstitutionwewanttoprovethatT(n)6c lg(n-b). Assumethisholdsfordn=2e. We have:T(n)6clg(dn=2-be) +1< clg(n=2-b+1) +1=clg(n-2b+22) +1=clg(n-2b+2) -clg2+16clg(n-b)Thelastinequalityrequiresthatb>2andc> :2-1 DetermineanupperboundonT(n) =3T(bn=2c) +nusinga havethateachnodeof depthiis boundedbyn=2iandthereforethecontribution of eachlevelis at most(3=2) (3lgn) = (nlg3).
5 Summingupweobtain:T(n) =3T(bn=2c) +n6n+ (3=2)n+ (3=2)2n+ + (3=2)lgn-1n+ (nlg3)=nlgn-1Xi=0(3=2)i+ (nlg3)=n (3=2)lgn-1(3=2) -1+ (nlg3)=2(n(3=2)lgn-n) + (nlg3)=2n3lgn2lgn-2n+ (nlg3)=2 3lgn-2n+ (nlg3)=2nlg3-2n+ (nlg3)= (nlg3)We canprovethisbysubstitutionbyassummingtha tT(bn=2c)6cbn=2clg3-cbn=2c. Weobtain:T(n) =3T(bn=2c) +n63cbn=2clg3-cbn=2c+n63cnlg32lg3-cn2+n6 cnlg3-cn2+n6cnlg3 Wherethelastinequalityholdsforc> :2-3 DrawtherecursiontreeofT(n) =4T(bn=2c) +cn. Theheightofthetreeis lgn, theoutdegreeofeachnodewillbe4andthecontr ibutionoftheithlevelwillbe4ibcn=2ic. Thelastlevelcontributes4lgn (1) = (n2). Hencewehavea boundonthesumgivenby:T(n) =4T(bn=2c) +cn=lgn-1Xi=04i bcn=2ic+ (n2)6lgn-1Xi=04i cn=2i+ (n2)=cnlgn-1Xi=02i+ (n2) + (n2)=cn 2lgn-12-1+ (n2)= (n2) (bn=2c)6cbn=2c2-cbn=2c.
6 We have:T(n) =4T(bn=2c) +cn64(cbn=2c2-cbn=2c) +cn< 4c(n=2)2-4cn=2+cn=cn2-2cn+cn=cn2-cn4:3-1 Usethemastermethodto ; b=4andnlog24=n2 T(n) =4T(n=2) +n. Sincen=O(n2- )case1appliesandwegetT(n) = (n2). T(n) =4T(n=2) +n2. Sincen2= (n2)wehaveT(n) = (n2lgn). T(n) =4T(n=2) +n3. Sincen3= (n2+ )and4(n=2)3=1=2n36cn3forsomec < 1wehavethatT(n) = (n3).76:1-1 Thereis a most2h+1-1verticesin a completebinarytreeofheighth. Sincethelowerlevelneednotbe :1-2 Sincetheheightofann-elementheapmustsatis fythat2h6n62h+1-1 < 2h+1. wehaveh6lgn < h+ anintegersoh= :1-3 Themax-heappropertyinsuresthatthelargest elementina subtreeofa heapis at :1-4 Thesmallestelementina max-heapis alwaysata :1-6No,thesequenceh23;17;14;6; 13;10;1; 5; 7; 12iis nota :2-6 Settingtherootto0andallothernodesto1, willcausethe0topropagatetobottomofthetre eusingatleastlgnoperationseachcostingO(1 ).
7 Hencewehavea (lgn) :4-4 Showthattheworst-caserunningtimeofheapso rtis (nlgn). Thisis clearsincesortinghasalowerboundof (nlgn)6:5-3To supportoperationsfora :5-4 Sincetheheapdatastructureis representedbyanarrayanddeletionsareimple mentedbyre-ducingthesizeofthearraytherem aybeunde is :5-5 Bythefollowingloopinvariantwecanprovethe correctnessofHEAP-INCREASE-KEY:At thestartofeachiterationofthewhileloopofl ines4-6, thearrayA[1 : : :heap-size[A]]sati esthemax-heapproperty, exceptthattheremaybeoneviolation:A[i]may belargerthanA[PARENT(i)].8 Initialization:Beforethe rstiterationofthewhiletheonlychangeofthe max-heapis thatA[i]is :Theterminationconditionofthewhilestates thatattheendoftheiterationthemax-heappro pertybetweenianditsparentis restoredortheiis seebytheloopinvariantthattheheapproperty is :5-7 Algorithm5 HEAP-DELETE(A; i)Input:A :TheheapAwiththeelementa [i]$A[heap-size[A]]heap-size[A] heap-size[A] -1key A[i]ifkey6A[PARENT(i)]thenMAX-HEAPIFY(A; i)elsewhilei > 1andA[PARENT(i)]<keydoA[i]$A[PARENT(i)]i PARENT(i)end whileend if6:5-8 Givenksortedlistswitha totalofnelementsshowhowtomergetheminO(nl gk) position1fromeachlistintoa obtainthe (nlgk).
8 KeptinA[1], itsdchildrenarekeptinorderinA[2]throughA [d+1] :D -ARY-PARENT(i)returnb(i-2)=d+1cD -ARY-CHILD(i; j)returnd(i-1) +j+ (logdn). neford-aryheaps;theproblemis MAX-HEAPIFY. (dlogdn) ,thatis (logdn). :1-2 WhenalltheelementsinAarethesame,noticeth atthecomparisoninline4ofPARTITION isalwayssati edandithereforeis p-1andi+1is makePARTITION return(p+r)=2whenallelementsarethesame, :1-3 TherunningtimeofPARTITIONis (n)sinceeachiterationoftheforloopinvolve sa constantnumberofoperationsandthereis (n) :1-4To makeQUICKSORT sortinnonincreasingorderreplacethe6compa risoninPARTITION line4with>.7:2-2If theelementsinAarethesame,thenbyexercise7 :1-2thereturnedelementfromeachcalltoPART ITION(A; p; r) (n2).7:2-3If theelementsinAaredistinctandsortedindecr easingorderthen,asinthepreviousexercise, again (n2).
9 , theQUICKSORT' ' tobe (n). werecursivelycallQUICKSORT' onthesmallestsubarrayreturnedbyPARTITION wewillavoidtheproblemandretainaO(lgn) :2-3 COUNTING-SORT willworkcorrectlynomatterwhatorderAis processedin,howeverit is :2-4 Givennintegersfrom1tokshowhowtocountthen umberofelementsfromatobinO(1)timewithO(n +k) [i]containsthenumberofelementslessthanor equaltoi. Clearly,C[b] -C[a] :3-4 Showhowtosortnintegersintherange0ton2-1i nO(n) (n2). Thusconsideringthen2numbersasradixnnumbe rsgivesusthat:d=logn(n2) =2logn(n) =2 Radixsortwillthenhavea runningtimeof (d(n+k) = (2(n+n)) = (n).8:4-2 Showhotoimprovetheworst-caserunningtimeo fbucketsorttoO(nlgn). Simplyreplacetheinsertionsortusedtosortt helinkedlistswithsomeworstcaseO(nlgn)sor tingalgorithm, :n-1Xi=0O(nilgni)6n-1Xi=0O(nilgn) =O(lgn)n-1Xi=0O(ni) =O(nlgn)Thetotaltimeofbucketsortis thusO(nlgn).)
10 129:1-1 Showhowto ndthethesecondsmallestelementofnelements usingn+ ndthesmallestelementconstructa tournamentasfollows:Compareallthenumbers in potentiallythesmallestofallsotheproblemi s reducedtosizedn=2e. Continuinginthisfashionuntilthereis (showbyinductiononn). Eachofthesenodescorrespondtoa canusethisbinarytreetoalsolocatethesecon dsmallestnumber. Thepathfromtheroottothesmallestelement(o fheightdlgne) :n-1+dlgne-1=n+ :3-1 Considertheanalysisofthealgorithmforgrou psofk. Thenumberofelementslessthan(orgreatertha n)themedianofthemediansxwillbeatleastdk2 e 12dnke -2 >n4-k. Hence,intheworst-caseSELECT willbecalledrecursivelyonatmostn- n4-k =3n4+ (n)6T(dn=ke) +T(3n=4+k) +O(n)Solvingbysubstitutionweobtaina boundforwhichkthealgorithmwillbelinear. AssumeT(n)6cnforallsmallern. We have:T(n)6clnkm+c 3n4+k +O(n)6c(nk+1) +3cnk+ck+O(n)6cnk+3cnk+c(k+1) +O(n)=cn 1k+34 +c(k+1) +O(n)6cnWherethelastequationonlyholdsfor k > 4.