Tải bản đầy đủ (.pdf) (742 trang)

Data structures and algorithm analysis in c mark allen wei

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.54 MB, 742 trang )

DataStructuresandAlgorithm
AnalysisinC
byMarkAllenWeiss
PREFACE
CHAPTER1:INTRODUCTION
CHAPTER2:ALGORITHMANALYSIS
CHAPTER3:LISTS,STACKS,ANDQUEUES
CHAPTER4:TREES
CHAPTER5:HASHING
CHAPTER6:PRIORITYQUEUES(HEAPS)
CHAPTER7:SORTING
CHAPTER8:THEDISJOINTSETADT
CHAPTER9:GRAPHALGORITHMS
CHAPTER10:ALGORITHMDESIGNTECHNIQUES
CHAPTER11:AMORTIZEDANALYSIS



PREFACE


Purpose/Goals
Thisbookdescribesdatastructures,methodsoforganizinglargeamountsof
data,andalgorithmanalysis,theestimationoftherunningtimeofalgorithms.
Ascomputersbecomefasterandfaster,theneedforprogramsthatcanhandle
largeamountsofinputbecomesmoreacute.Paradoxically,thisrequiresmore
carefulattentiontoefficiency,sinceinefficienciesinprogramsbecomemost
obviouswheninputsizesarelarge.Byanalyzinganalgorithmbeforeitis
actuallycoded,studentscandecideifaparticularsolutionwillbefeasible.For
example,inthistextstudentslookatspecificproblemsandseehowcareful
implementationscanreducethetimeconstraintforlargeamountsofdatafrom


16yearstolessthanasecond.Therefore,noalgorithmordatastructureis
presentedwithoutanexplanationofitsrunningtime.Insomecases,minute
detailsthataffecttherunningtimeoftheimplementationareexplored.
Onceasolutionmethodisdetermined,aprogrammuststillbewritten.As
computershavebecomemorepowerful,theproblemstheysolvehavebecome
largerandmorecomplex,thusrequiringdevelopmentofmoreintricateprograms
tosolvetheproblems.Thegoalofthistextistoteachstudentsgood
programmingandalgorithmanalysisskillssimultaneouslysothattheycan
developsuchprogramswiththemaximumamountofefficiency.
Thisbookissuitableforeitheranadvanceddatastructures(CS7)courseora
first-yeargraduatecourseinalgorithmanalysis.Studentsshouldhavesome
knowledgeofintermediateprogramming,includingsuchtopicsaspointersand
recursion,andsomebackgroundindiscretemath.


Approach
Ibelieveitisimportantforstudentstolearnhowtoprogramforthemselves,not
howtocopyprogramsfromabook.Ontheotherhand,itisvirtuallyimpossible
todiscussrealisticprogrammingissueswithoutincludingsamplecode.Forthis
reason,thebookusuallyprovidesabouthalftothree-quartersofan
implementation,andthestudentisencouragedtosupplytherest.
ThealgorithmsinthisbookarepresentedinANSIC,which,despitesomeflaws,
isarguablythemostpopularsystemsprogramminglanguage.TheuseofC
insteadofPascalallowstheuseofdynamicallyallocatedarrays(seeforinstance
rehashinginCh.5).Italsoproducessimplifiedcodeinseveralplaces,usually
becausetheand(&&)operationisshort-circuited.
MostcriticismsofCcenteronthefactthatitiseasytowritecodethatisbarely
readable.Someofthemorestandardtricks,suchasthesimultaneousassignment
andtestingagainst0via
if(x=y)


aregenerallynotusedinthetext,sincethelossofclarityiscompensatedbyonly
afewkeystrokesandnoincreasedspeed.Ibelievethatthisbookdemonstrates
thatunreadablecodecanbeavoidedbyexercisingreasonablecare.


Overview
Chapter1containsreviewmaterialondiscretemathandrecursion.Ibelievethe
onlywaytobecomfortablewithrecursionistoseegoodusesoverandover.
Therefore,recursionisprevalentinthistext,withexamplesineverychapter
exceptChapter5.
Chapter2dealswithalgorithmanalysis.Thischapterexplainsasymptotic
analysisanditsmajorweaknesses.Manyexamplesareprovided,includingan
in-depthexplanationoflogarithmicrunningtime.Simplerecursiveprogramsare
analyzedbyintuitivelyconvertingthemintoiterativeprograms.More
complicateddivide-and-conquerprogramsareintroduced,butsomeofthe
analysis(solvingrecurrencerelations)isimplicitlydelayeduntilChapter7,
whereitisperformedindetail.
Chapter3coverslists,stacks,andqueues.Theemphasishereisoncodingthese
datastructuresusing ,fastimplementationofthesedatastructures,andan
expositionofsomeoftheiruses.Therearealmostnoprograms(justroutines),
buttheexercisescontainplentyofideasforprogrammingassignments.
ADTS

Chapter4coverstrees,withanemphasisonsearchtrees,includingexternal
searchtrees(B-trees).The filesystemandexpressiontreesareusedas
examples. treesandsplaytreesareintroducedbutnotanalyzed.Seventy-five
percentofthecodeiswritten,leavingsimilarcasestobecompletedbythe
student.Additionalcoverageoftrees,suchasfilecompressionandgametrees,is
deferreduntilChapter10.Datastructuresforanexternalmediumareconsidered

asthefinaltopicinseveralchapters.
UNIX

AVL

Chapter5isarelativelyshortchapterconcerninghashtables.Someanalysisis
performedandextendiblehashingiscoveredattheendofthechapter.
Chapter6isaboutpriorityqueues.Binaryheapsarecovered,andthereis
additionalmaterialonsomeofthetheoreticallyinterestingimplementationsof
priorityqueues.
Chapter7coverssorting.Itisveryspecificwithrespecttocodingdetailsand
analysis.Alltheimportantgeneral-purposesortingalgorithmsarecoveredand


compared.Threealgorithmsareanalyzedindetail:insertionsort,Shellsort,and
quicksort.Externalsortingiscoveredattheendofthechapter.
Chapter8discussesthedisjointsetalgorithmwithproofoftherunningtime.
ThisisashortandspecificchapterthatcanbeskippedifKruskal'salgorithmis
notdiscussed.
Chapter9coversgraphalgorithms.Algorithmsongraphsareinterestingnot
onlybecausetheyfrequentlyoccurinpracticebutalsobecausetheirrunning
timeissoheavilydependentontheproperuseofdatastructures.Virtuallyallof
thestandardalgorithmsarepresentedalongwithappropriatedatastructures,
pseudocode,andanalysisofrunningtime.Toplacetheseproblemsinaproper
context,ashortdiscussiononcomplexitytheory(includingNP-completeness
andundecidability)isprovided.
Chapter10coversalgorithmdesignbyexaminingcommonproblem-solving
techniques.Thischapterisheavilyfortifiedwithexamples.Pseudocodeisused
intheselaterchapterssothatthestudent'sappreciationofanexamplealgorithm
isnotobscuredbyimplementationdetails.

Chapter11dealswithamortizedanalysis.ThreedatastructuresfromChapters4
and6andtheFibonacciheap,introducedinthischapter,areanalyzed.
Chapters1-9provideenoughmaterialformostone-semesterdatastructures
courses.Iftimepermits,thenChapter10canbecovered.Agraduatecourseon
algorithmanalysiscouldcoverChapters7-11.Theadvanceddatastructures
analyzedinChapter11caneasilybereferredtointheearlierchapters.The
discussionofNP-completenessinChapter9isfartoobrieftobeusedinsucha
course.GareyandJohnson'sbookonNP-completenesscanbeusedtoaugment
thistext.


Exercises
Exercises,providedattheendofeachchapter,matchtheorderinwhichmaterial
ispresented.Thelastexercisesmayaddressthechapterasawholeratherthana
specificsection.Difficultexercisesaremarkedwithanasterisk,andmore
challengingexerciseshavetwoasterisks.
Asolutionsmanualcontainingsolutionstoalmostalltheexercisesisavailable
separatelyfromTheBenjamin/CummingsPublishingCompany.


References
Referencesareplacedattheendofeachchapter.Generallythereferenceseither
arehistorical,representingtheoriginalsourceofthematerial,ortheyrepresent
extensionsandimprovementstotheresultsgiveninthetext.Somereferences
representsolutionstoexercises.


Acknowledgments
Iwouldliketothankthemanypeoplewhohelpedmeinthepreparationofthis
andpreviousversionsofthebook.TheprofessionalsatBenjamin/Cummings

mademybookaconsiderablylessharrowingexperiencethanIhadbeenledto
expect.I'dliketothankmypreviouseditors,AlanAptandJohnThompson,as
wellasCarterShanklin,whohaseditedthisversion,andCarter'sassistant,
VivianMcDougal,foransweringallmyquestionsandputtingupwithmy
delays.GailCarriganatBenjamin/CummingsandMelissaG.MadsenandLaura
SnyderatPublicationServicesdidawonderfuljobwithproduction.TheC
versionwashandledbyJoeHeathwardandhisoutstandingstaff,whowereable
tomeettheproductionscheduledespitethedelayscausedbyHurricaneAndrew.
Iwouldliketothankthereviewers,whoprovidedvaluablecomments,manyof
whichhavebeenincorporatedintothetext.Alphabetically,theyareVickiAllan
(UtahStateUniversity),HenryBauer(UniversityofWyoming),AlexBiliris
(BostonUniversity),JanCarroll(UniversityofNorthTexas),DanHirschberg
(UniversityofCalifornia,Irvine),JuliaHodges(MississippiStateUniversity),
BillKraynek(FloridaInternationalUniversity),RaynoD.Niemi(Rochester
InstituteofTechnology),RobertO.Pettus(UniversityofSouthCarolina),Robert
Probasco(UniversityofIdaho),CharlesWilliams(GeorgiaStateUniversity),
andChrisWilson(UniversityofOregon).Iwouldparticularlyliketothank
VickiAllan,whocarefullyreadeverydraftandprovidedverydetailed
suggestionsforimprovement.
AtFIU,manypeoplehelpedwiththisproject.XinweiCuiandJohnTso
providedmewiththeirclassnotes.I'dliketothankBillKraynek,WesMackey,
JaiNavlakha,andWeiSunforusingdraftsintheircourses,andthemany
studentswhosufferedthroughthesketchyearlydrafts.MariaFiorenza,Eduardo
Gonzalez,AncinPeter,TimRiley,JefreRiser,andMagalySotolongoreported
severalerrors,andMikeHallcheckedthroughanearlydraftforprogramming
errors.AspecialthanksgoestoYuzhengDing,whocompiledandtestedevery
programintheoriginalbook,includingtheconversionofpseudocodetoPascal.
I'dberemisstoforgetCarlosIbarraandSteveLuis,whokepttheprintersand
thecomputersystemworkingandsentouttapesonaminute'snotice.
Thisbookisaproductofalovefordatastructuresandalgorithmsthatcanbe



obtainedonlyfromtopeducators.I'dliketotakethetimetothankBobHopkins,
E.C.Horvath,andRichMendez,whotaughtmeatCooperUnion,andBob
Sedgewick,KenSteiglitz,andBobTarjanfromPrinceton.
Finally,I'dliketothankallmyfriendswhoprovidedencouragementduringthe
project.Inparticular,I'dliketothankMicheleDorchak,ArvinPark,andTim
Snyderforlisteningtomystories;BillKraynek,AlexPelin,andNorman
Pestainaforbeingcivilnext-door(office)neighbors,evenwhenIwasn't;Lynn
andTobyBerkforshelterduringAndrew,andtheHTMCforworkrelief.
Anymistakesinthisbookare,ofcourse,myown.Iwouldappreciatereportsof
anyerrorsyoufind;mye-mailaddressis
M.A.W.
Miami,Florida
September1992
GotoChapter1ReturntoTableofContents



CHAPTER11:AMORTIZED
ANALYSIS
Inthischapter,wewillanalyzetherunningtimeforseveraloftheadvanceddata
structuresthathavebeenpresentedinChapters4and6.Inparticular,wewill
considertheworst-caserunningtimeforanysequenceofmoperations.This
contrastswiththemoretypicalanalysis,inwhichaworst-caseboundisgiven
foranysingleoperation.
Asanexample,wehaveseenthatAVLtreessupportthestandardtreeoperations
inO(logn)worst-casetimeperoperation.AVLtreesaresomewhatcomplicatedto
implement,notonlybecausethereareahostofcases,butalsobecauseheight
balanceinformationmustbemaintainedandupdatedcorrectly.Thereasonthat

AVLtreesareusedisthatasequenceof (n)operationsonanunbalancedsearch
treecouldrequire (n2)time,whichwouldbeexpensive.Forsearchtrees,the
O(n)worst-caserunningtimeofanoperationisnottherealproblem.Themajor
problemisthatthiscouldhappenrepeatedly.Splaytreesofferapleasant
alternative.Althoughanyoperationcanstillrequire (n)time,thisdegenerate
behaviorcannotoccurrepeatedly,andwecanprovethatanysequenceofm
operationstakesO(mlogn)worst-casetime(total).Thus,inthelongrunthis
datastructurebehavesasthougheachoperationtakesO(logn).Wecallthisan
amortizedtimebound.
Amortizedboundsareweakerthanthecorrespondingworst-casebounds,
becausethereisnoguaranteeforanysingleoperation.Sincethisisgenerallynot
important,wearewillingtosacrificetheboundonasingleoperation,ifwecan
retainthesameboundforthesequenceofoperationsandatthesametime
simplifythedatastructure.Amortizedboundsarestrongerthantheequivalent
average-casebound.Forinstance,binarysearchtreeshaveO(logn)average
timeperoperation,butitisstillpossibleforasequenceofmoperationstotake
O(mn)time.
Becausederivinganamortizedboundrequiresustolookatanentiresequenceof
operationsinsteadofjustone,weexpectthattheanalysiswillbemoretricky.
Wewillseethatthisexpectationisgenerallyrealized.


Inthischapterweshall
Analyzethebinomialqueueoperations.
Analyzeskewheaps.
IntroduceandanalyzetheFibonacciheap.
Analyzesplaytrees.


11.1.AnUnrelatedPuzzle

Considerthefollowingpuzzle:Twokittensareplacedonoppositeendsofa
footballfield,100yardsapart.Theywalktowardseachotheratthespeedoften
yardsperminute.Atthesametime,theirmotherisatoneendofthefield.She
canrunat100yardsperminute.Themotherrunsfromonekittentotheother,
makingturnswithnolossofspeed,untilthekittens(andthusthemother)meet
atmidfield.Howfardoesthemotherrun?
Itisnothardtosolvethispuzzlewithabruteforcecalculation.Weleavethe
detailstoyou,butoneexpectsthatthiscalculationwillinvolvecomputingthe
sumofaninfinitegeometricseries.Althoughthisstraightforwardcalculation
willleadtoananswer,itturnsoutthatamuchsimplersolutioncanbearrivedat
byintroducinganextravariable,namely,time.
Becausethekittensare100yardsapartandapproacheachotheratacombined
velocityof20yardsperminute,ittakesthemfiveminutestogettomidfield.
Sincethemotherruns100yardsperminute,hertotalis500yards.
Thispuzzleillustratesthepointthatsometimesitiseasiertosolveaproblem
indirectlythandirectly.Theamortizedanalysesthatwewillperformwillusethis
idea.Wewillintroduceanextravariable,knownasthepotential,toallowusto
proveresultsthatseemverydifficulttoestablishotherwise.


11.2.BinomialQueues
ThefirstdatastructurewewilllookatisthebinomialqueueofChapter6,which
wenowreviewbriefly.RecallthatabinomialtreeB0isaone-nodetree,andfor
k>0,thebinomialtreeBkisbuiltbymeldingtwobinomialtreesBk-1together.
BinomialtreesB0throughB4areshowninFigure11.1.

Figure11.1BinomialtreesB0,B1,B2,B3,andB4

Figure11.2TwobinomialqueuesH1andH2
Therankofanodeinabinomialtreeisequaltothenumberofchildren;in

particular,therankoftherootofBkisk.Abinomialqueueisacollectionof
heap-orderedbinomialtrees,inwhichtherecanbeatmostonebinomialtreeBk
foranyk.Twobinomialqueues,H1andH2,areshowninFigure11.2.
Themostimportantoperationismerge.Tomergetwobinomialqueues,an


operationsimilartoadditionofbinaryintegersisperformed:Atanystagewe
mayhavezero,one,two,orpossiblythreeBktrees,dependingonwhetherornot
thetwopriorityqueuescontainaBktreeandwhetherornotaBktreeiscarried
overfromthepreviousstep.IfthereiszerooroneBktree,itisplacedasatreein
theresultantbinomialqueue.IftherearetwoBktrees,theyaremeldedintoa
Bk+1treeandcarriedover;iftherearethreeBktrees,oneisplacedasatreein
thebinomialqueueandtheothertwoaremeldedandcarriedover.Theresultof
mergingH1andH2isshowninFigure11.3.
Insertionisperformedbycreatingaone-nodebinomialqueueandperforminga
merge.Thetimetodothisism+1,wheremrepresentsthesmallesttypeof
binomialtreeBmnotpresentinthebinomialqueue.Thus,insertionintoa
binomialqueuethathasaB0treebutnoB1treerequirestwosteps.Deletionof
theminimumisaccomplishedbyremovingtheminimumandsplittingthe
originalbinomialqueueintotwobinomialqueues,whicharethenmerged.Aless
terseexplanationoftheseoperationsisgiveninChapter6.
Weconsideraverysimpleproblemfirst.Supposewewanttobuildabinomial
queueofnelements.Weknowthatbuildingabinaryheapofnelementscanbe
doneinO(n),soweexpectasimilarboundforbinomialqueues.

Figure11.3BinomialqueueH3:theresultofmergingH1andH2
CLAIM:
AbinomialqueueofnelementscanbebuiltbynsuccessiveinsertionsinO(n)
time.
Theclaim,iftrue,wouldgiveanextremelysimplealgorithm.SincetheworstcasetimeforeachinsertionisO(logn),itisnotobviousthattheclaimistrue.

Recallthatifthisalgorithmwereappliedtobinaryheaps,therunningtime
wouldbeO(nlogn).


Toprovetheclaim,wecoulddoadirectcalculation.Tomeasuretherunning
time,wedefinethecostofeachinsertiontobeonetimeunitplusanextraunit
foreachlinkingstep.Summingthiscostoverallinsertionsgivesthetotal
runningtime.Thistotalisnunitsplusthetotalnumberoflinkingsteps.The1st,
3rd,5th,andallodd-numberedstepsrequirenolinkingsteps,sincethereisnoB0
presentatthetimeofinsertion.Thus,halfoftheinsertionsrequirenolinking
steps.Aquarteroftheinsertionsrequireonlyonelinkingstep(2nd,6th,10th,
andsoon).Aneighthrequiretwo,andsoon.Wecouldaddthisallupandbound
thenumberoflinkingstepsbyn,provingtheclaim.Thisbruteforcecalculation
willnothelpwhenwetrytoanalyzeasequenceofoperationsthatincludemore
thanjustinsertions,sowewilluseanotherapproachtoprovethisresult.
Considertheresultofaninsertion.IfthereisnoB0treepresentatthetimeofthe
insertion,thentheinsertioncostsatotalofoneunit,byusingthesame
accountingasabove.TheresultoftheinsertionisthatthereisnowaB0tree,and
thuswehaveaddedonetreetotheforestofbinomialtrees.IfthereisaB0tree
butnoB1tree,thentheinsertioncoststwounits.ThenewforestwillhaveaB1
treebutwillnolongerhaveaB0tree,sothenumberoftreesintheforestis
unchanged.AninsertionthatcoststhreeunitswillcreateaB2treebutdestroya
B0andB1tree,yieldinganetlossofonetreeintheforest.Infact,itiseasyto
seethat,ingeneral,aninsertionthatcostscunitsresultsinanetincreaseof2-c
treesintheforest,becauseaBc-1treeiscreatedbutallBitrees0 iremoved.Thus,expensiveinsertionsremovetrees,whilecheapinsertionscreate
trees.
LetCibethecostoftheithinsertion.LetTibethenumberoftreesaftertheith
insertion.T0=0isthenumberoftreesinitially.Thenwehavetheinvariant
Ci+(Ti-Ti-1)=2

(11.1)
Wethenhave
C1+(T1-T0)=2
C2+(T2-T1)=2








Cn-1+(Tn-1-Tn-2)=2
Cn+(Tn-Tn-1)=2

Ifweaddalltheseequations,mostoftheTitermscancel,leaving

orequivalently,

RecallthatT0=0andTn,thenumberoftreesaftertheninsertions,iscertainly
notnegative,so(Tn-T0)isnotnegative.Thus

whichprovestheclaim.
Duringthebuild_binomial_queueroutine,eachinsertionhadaworst-casetime
ofO(logn),butsincetheentireroutineusedatmost2nunitsoftime,the
insertionsbehavedasthougheachusednomorethan2unitseach.
Thisexampleillustratesthegeneraltechniquewewilluse.Thestateofthedata
structureatanytimeisgivenbyafunctionknownasthepotential.Thepotential
functionisnotmaintainedbytheprogram,butratherisanaccountingdevice
thatwillhelpwiththeanalysis.Whenoperationstakelesstimethanwehave

allocatedforthem,theunusedtimeis"saved"intheformofahigherpotential.
Inourexample,thepotentialofthedatastructureissimplythenumberoftrees.
Intheanalysisabove,whenwehaveinsertionsthatuseonlyoneunitinsteadof
thetwounitsthatareallocated,theextraunitissavedforlaterbyanincreasein
potential.Whenoperationsoccurthatexceedtheallottedtime,thentheexcess
timeisaccountedforbyadecreaseinpotential.Onemayviewthepotentialas
representingasavingsaccount.Ifanoperationuseslessthanitsallottedtime,


thedifferenceissavedforuselateronbymoreexpensiveoperations.Figure
11.4showsthecumulativerunningtimeusedbybuild_binomial_queueovera
sequenceofinsertions.Observethattherunningtimeneverexceeds2nandthat
thepotentialinthebinomialqueueafteranyinsertionmeasurestheamountof
savings.

Figure11.4Asequenceofninserts
Onceapotentialfunctionischosen,wewritethemainequation:
Tactual+ Potential=Tamortized
(11.2)
Tactual,theactualtimeofanoperation,representstheexact(observed)amountof
timerequiredtoexecuteaparticularoperation.Inabinarysearchtree,for
example,theactualtimetoperformafind(x)is1plusthedepthofthenode
containingx.Ifwesumthebasicequationovertheentiresequence,andifthe
finalpotentialisatleastaslargeastheinitialpotential,thentheamortizedtime
isanupperboundontheactualtimeusedduringtheexecutionofthesequence.
NoticethatwhileTactualvariesfromoperationtooperation,Tamortizedisstable.
Pickingapotentialfunctionthatprovesameaningfulboundisaverytrickytask;
thereisnoonemethodthatisused.Generally,manypotentialfunctionsaretried
beforetheonethatworksisfound.Nevertheless,thediscussionabovesuggestsa
fewrules,whichtellusthepropertiesthatgoodpotentialfunctionshave.The



potentialfunctionshould
Alwaysassumeitsminimumatthestartofthesequence.Apopularmethod
ofchoosingpotentialfunctionsistoensurethatthepotentialfunctionisinitially
0,andalwaysnonnegative.Alloftheexamplesthatwewillencounterusethis
strategy.
Cancelatermintheactualtime.Inourcase,iftheactualcostwasc,thenthe
potentialchangewas2-c.Whentheseareadded,anamortizedcostof2is
obtained.ThisisshowninFigure11.5.
Wecannowperformacompleteanalysisofbinomialqueueoperations.

Figure11.5Theinsertioncostandpotentialchangeforeachoperationina
sequence
THEOREM11.1.
Theamortizedrunningtimesofinsert,delete_min,andmergeareO(1),O(log
n),andO(logn),respectively,forbinomialqueues.
PROOF:
Thepotentialfunctionisthenumberoftrees.Theinitialpotentialis0,andthe
potentialisalwaysnonnegative,sotheamortizedtimeisanupperboundonthe
actualtime.Theanalysisforinsertfollowsfromtheargumentabove.Formerge,
assumethetwotreeshaven1andn2nodeswithT1andT2trees,respectively.Let
n=n1+n2.TheactualtimetoperformthemergeisO(log(n1)+log(n2))=O(log
n).Afterthemerge,therecanbeatmostlogntrees,sothepotentialcanincrease


byatmostO(logn).ThisgivesanamortizedboundofO(logn).Thedelete_min
boundfollowsinasimilarmanner.



11.3.SkewHeaps
Theanalysisofbinomialqueuesisafairlyeasyexampleofanamortized
analysis.Wenowlookatskewheaps.Asiscommonwithmanyofour
examples,oncetherightpotentialfunctionisfound,theanalysisiseasy.The
difficultpartischoosingameaningfulpotentialfunction.
Recallthatforskewheaps,thekeyoperationismerging.Tomergetwoskew
heaps,wemergetheirrightpathsandmakethisthenewleftpath.Foreachnode
onthenewpath,exceptthelast,theoldleftsubtreeisattachedastheright
subtree.Thelastnodeonthenewleftpathisknowntonothavearightsubtree,
soitissillytogiveitone.Thebounddoesnotdependonthisexception,andif
theroutineiscodedrecursively,thisiswhatwillhappennaturally.Figure11.6
showstheresultofmergingtwoskewheaps.
Supposewehavetwoheaps,H1andH2,andtherearer1andr2nodesontheir
respectiverightpaths.Thentheactualtimetoperformthemergeisproportional
tor1+r2,sowewilldroptheBig-Ohnotationandchargeoneunitoftimefor
eachnodeonthepaths.Sincetheheapshavenostructure,itispossiblethatall
thenodesinbothheapslieontherightpath,andthiswouldgivea (n)worstcaseboundtomergetheheaps(Exercise11.3asksyoutoconstructanexample).
WewillshowthattheamortizedtimetomergetwoskewheapsisO(logn).

Figure11.6Mergingoftwoskewheaps
Whatisneededissomesortofapotentialfunctionthatcapturestheeffectof
skewheapoperations.Recallthattheeffectofamergeisthateverynodeonthe
rightpathismovedtotheleftpath,anditsoldleftchildbecomesthenewright
child.Oneideamightbetoclassifyeachnodeasarightnodeorleftnode,


dependingonwhetherornotitisarightchild,andusethenumberofrightnodes
asapotentialfunction.Althoughthepotentialisinitially0andalways
nonnegative,theproblemisthatthepotentialdoesnotdecreaseafteramerge
andthusdoesnotadequatelyreflectthesavingsinthedatastructure.Theresult

isthatthispotentialfunctioncannotbeusedtoprovethedesiredbound.
Asimilarideaistoclassifynodesaseitherheavyorlight,dependingonwhether
ornottherightsubtreeofanynodehasmorenodesthantheleftsubtree.
DEFINITION Anodepisheavyifthenumberofdescendantsofp'sright
subtreeisatleasthalfofthenumberofdescendantsofp,andlightotherwise.
Notethatthenumberofdescendantsofanodeincludesthenodeitself.
:

Asanexample,Figure11.7showsaskewheap.Thenodeswithkeys15,3,6,
12,and7areheavy,andallothernodesarelight.
Thepotentialfunctionwewilluseisthenumberofheavynodesinthe
(collection)ofheaps.Thisseemslikeagoodchoice,becausealongrightpath
willcontainaninordinatenumberofheavynodes.Becausenodesonthispath
havetheirchildrenswapped,thesenodeswillbeconvertedtolightnodesasa
resultofthemerge.
THEOREM11.2.
TheamortizedtimetomergetwoskewheapsisO(logn).
PROOF:
LetH1andH2bethetwoheaps,withn1andn2nodesrespectively.Supposethe
rightpathofH1hasl1lightnodesandh1heavynodes,foratotalofl1+h1.
Likewise,H2hasl2lightandh2heavynodesonitsrightpath,foratotalofl2+
h2nodes.


Figure11.7Skewheap--heavynodesare3,6,7,12,and15

Figure11.8Changeinheavy/lightstatusafteramerge
Ifweadopttheconventionthatthecostofmergingtwoskewheapsisthetotal
numberofnodesontheirrightpaths,thentheactualtimetoperformthemerge
isl1+l2+h1+h2.Nowtheonlynodeswhoseheavy/lightstatuscanchangeare

nodesthatareinitiallyontherightpath(andwindupontheleftpath),sinceno
othernodeshavetheirsubtreesaltered.ThisisshownbytheexampleinFigure
11.8.
Ifaheavynodeisinitiallyontherightpath,thenafterthemergeitmustbecome
alightnode.Theothernodesthatwereontherightpathwerelightandmayor
maynotbecomeheavy,butsinceweareprovinganupperbound,wewillhave
toassumetheworst,whichisthattheybecomeheavyandincreasethepotential.
Thenthenetchangeinthenumberofheavynodesisatmostl1+l2-h1-h2.
Addingtheactualtimeandthepotentialchange(Equation11.2)givesan
amortizedboundof2(l1+l2).
Nowwemustshowthatl1+l2=O(logn).Sincel1andl2arethenumberof
lightnodesontheoriginalrightpaths,andtherightsubtreeofalightnodeisless
thanhalfthesizeofthetreerootedatthelightnode,itfollowsdirectlythatthe


×