Professional Documents
Culture Documents
net
ADVANCEDDATASTRUCTURES
UNITI
CSE
INTRODUCTION
``Data Structures and Algorithms'' is one of the classic, core topics of Computer Science. Data
structuresandalgorithmsarecentraltothedevelopmentofgoodqualitycomputerprograms.
WhatisaData?
Dataisthebasicentityorfactthatisusedincalculationormanipulationprocess.
There are two types of data such as numerical and alphanumerical data. Integer and floatingpoint
numbersareofnumericaldatatypeandstringsareofalphanumericdatatype.
Datamaybesingleorasetofvaluesanditistobeorganizedinaparticularfashion.Thisorganization
orstructuringofdatawillhaveprofoundimpactontheefficiencyoftheprogram.
WhatisaDataStructure?
Data structure is the structural representation of logical relationships between elements of data. In
otherwordsadatastructureisawayoforganizingdataitemsbyconsideringitsrelationshiptoeach
other.
Datastructureaffectsthedesignofboththestructuralandfunctionalaspectsofaprogram.
DataStructure=Organizeddata+Operations
Algorithm+DataStructure=Program
Definitions:
Algorithm: Algorithm is a stepbystep finite sequence of instructions, to solve a well defined
computational problem. Moreover there may be more than one algorithm to solve a problem. The
choiceofaparticularalgorithmdependsonfollowingperformanceanalysisandmeasurements:
1. Spacecomplexity
Analysis of space complexity of an algorithm or program is the amount of memory it
needstoruntocompletion.
2. Timecomplexity
Thetimecomplexityofanalgorithmoraprogramistheamountoftimeitneedstorun
tocompletion.
www.jntuworld.com || www.jwjobs.net
Page 1
UNITI
CSE
DataType:
Datatypeofavariableisthesetofvaluesthatthevariablemayassume.
Forexample:BasicdatatypesinCareint,char,float,double.
AbstractDataType(ADT):AnADTisasetofelementswithacollectionofwelldefinedoperations.
ExamplesofADTsincludelist,stack,queue,set,tree,graph,etc.
ClassificationofDataStructure:
FundamentalDataStructures:
Thefollowingfourdatastructuresareusedubiquitouslyinthedescriptionofalgorithmsandserveas
basicbuildingblocksforrealizingmorecomplexdatastructures.
Sequences(alsocalledaslists)
Dictionaries
PriorityQueues
Graphs
Dictionariesandpriorityqueuescanbeclassifiedunderabroadercategorycalleddynamicsets.Also,
binaryandgeneraltreesareverypopularbuildingblocksforimplementingdictionariesandpriority
queues.
www.jntuworld.com || www.jwjobs.net
Page 2
UNITI
CSE
DICTIONARIES
A dictionary is a container of elements from a totally ordered universe that supports the
basicoperationsofinserting/deletingelementsandsearchingforagivenelement.
In this chapter, first, we introduce the abstract data type Set which includes dictionaries,
priorityqueues,etc.assubclasses.
Sets:
Thesetisthemostfundamentaldatamodelofmathematics.
Asetisacollectionofwelldefinedelements.Themembersofasetarealldifferent.
There are special operations that are commonly performed on sets, such as Union,
intersection,difference.
1. TheunionoftwosetsSandT,denotedST,isthesetcontainingthe
elementsthatareinSorT,orboth.
2. TheintersectionofsetsSandT,writtenST,isthesetcontainingthe
elementsthatareinbothSandT.
3. ThedifferenceofsetsSandT,denotedST,isthesetcontainingthose
elementsthatareinSbutnotinT.
Forexample:
LetSbetheset{1,2,3}andTtheset{3,4,5}.Then
ST={1,2}
ST={1,2,3,4,5},
ST={3}, and
Setimplementation:
Possibledatastructuresinclude
BitVector
Array
LinkedList
o Unsorted
o Sorted
Dictionaries:
Narasaraopeta Engneering College
www.jntuworld.com || www.jwjobs.net
Page 3
UNITI
CSE
AdictionaryisadynamicsetADTwiththeoperations:
Search(S,k)anaccessoperationthatreturnsapointerxtoanelementwherex.key
=k
Insert(S,x)amanipulationoperationthataddstheelementpointedtobyxtoS
Delete(S, x) a manipulation operation that removes the element pointed to by x
fromS
Dictionariesstoreelementssothattheycanbelocatedquicklyusingkeys
It is useful in implementing symbol tables, text retrieval systems, database systems, page
mappingtables,etc.
Implementation:
1.FixedLengtharrays
2.Linkedlists:sorted,unsorted,skiplists
3.HashTables:open,closed
4.Trees
BinarySearchTrees(BSTs)
BalancedBSTs
o AVLTrees
o RedBlackTrees
SplayTrees
MultiwaySearchTrees
o 23Trees
o BTrees
Tries
Let n be the number of elements is a dictionary D. The following is a summary of the
performanceofsomebasicimplementationmethods:
Worst case complexity of
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(1)
O(n)
O(n)
O(n)
O(n)
Among these, the sorted list has the best average case performance.
Inthischapter,wediscusstwodatastructuresfordictionaries,namelyHashTablesandSkip
Lists.
www.jntuworld.com || www.jwjobs.net
Page 4
UNITI
CSE
HASHING
DivisionMethod:
Onecommonmethodofdeterminingahashkeyofthedivisionmethodofhashing
Theformulathatwillbeusedis:
H(key)=key%no.ofslotsinthetable
i.e.
h(key)=keymodarraysize
h(key)=keymodarraysize
Forexample:
Consideratablewith8slots.i.e.arraysize8.
Hashkey=key%tablesize
Thekeyvaluesare36,18,72,43,6,42
The division method is generally a reasonable strategy, unless the key happens to have
someundesirableproperties.
Note:ifthetablesizeis10andallofthekeysendinzero.
In the above example 42 mod 8 => 2, its already filled position in the hash table. This is
knownascollision.i.e.twoormorerecordkeysmaptothesamearrayindex.
InThiscase,thechoiceofhashfunctionandtablesizeneedstobecarefullyconsidered.The
besttablesizesareprimenumbers.
Narasaraopeta Engineering College
www.jntuworld.com || www.jwjobs.net
Page 5
UNITI
CSE
Multiplicationmethod:
The simplest situation when the keys are floating point numbers known to be in affixed
range.
Forexample:
Ifthekeysarenumbersthataregreaterthan0andlessthan1,wecanjustmultiplybym
(tablesize)androundofftothenearestintegertogetanaddressbetween0anm1.
Algorithm:
1.
2.
3.
4.
5.
ChooseconstantAintherange0<A<1.
MultiplykeykbyA.
Extractthefractionalpartofk*A
Multiplythefractionalpartbynumberofslots,m.
Taketheflooroftheresult.
Mathematically
h(k)= m(kAmod1)
wherekAmod1=kA kA =fractionalpartofkA
Disadvantage:Slowerthandivisionmethod.
Advantage:Valueofmisnotcritical.
Example:
m=8(impliesm=23,p=3)
w=5
k=21
Musthave0<s<25;chooses=13A=13/32.
h(k)= m(kAmod1)
h(21)= 8(2113/32mod1) =4
kA=2113/32=273/32=817/32kAmod1=17/32
m(kAmod1)=817/32=17/4=
41/4 m(kAmod1) =4
Sothath(21)=4.
www.jntuworld.com || www.jwjobs.net
Page 6
UNITI
CSE
Example:
m=8(impliesm=23,p=3)
w=5
k=21
s=13
ks
=2113
=273
=825+17
=r1.r0
r1 =825
r0 =17=100012
Writteninw=5bits,r0=100012Thep=3mostsignificantbitsofr0is1002or410,
soh(21)=4.
ExerciseExample:
m=4(impliesm=22,p=2)
w=3
k=12
s=50<s<2w=23=8
ks
=125
=?
=?23+?
=r1.r0
r1 =?23
r0 =?=?2
Writteninw=3bits,r0=?2
Thep=2mostsignificantbitsofr0is?2or?10,soh(12)=?.
www.jntuworld.com || www.jwjobs.net
Page 7
UNITI
CSE
Universalmethod:
Hashingisafunideathathaslotsofunexpecteduses.Here,welookatanoveltypeofhash
function that makes it easy to create a family of universal hash functions. The method is
basedonarandombinarymatrixandisverysimpletoimplement.
Theideaisverysimple.Supposeyouhaveaninputdataitemthatyouhaveinputdatawith
mbitsandyouwantahashfunctionthatproducesnbitsthenfirstgeneratearandom
binarymatrix(M)ofordernxm.
Thehashfunctionis h(x)=MxWherextobeabinaryvector
Forexample,Supposeyouhaveakey11,thebinaryformis1011anditisafourbitinput
value(m)andwanttogenerateoutputathreebithashvalue(n).
Thengeneratearandommatrixgivessay:
(0100)
M
=
(1011)
(1101)
andifthedatavaluewas1011thehashvaluewouldbecomputedas:
(0100)(1)
(0)
h(x)=Mx=
(1011)(0)
=
(1)
(1101)(1)
(0)
(1)
There are a number of other ways to look at the way the arithmetic is done that suggest
differentwaysofimplementingthealgorithm.
The first is to notice that what you are doing is anding each row with the data column
vector.Thatistakingthesecondrowasanexample:(1011)And(1011)=(1011)
andthenyouaddupthebitsintheresult:1+0+1+1=1
nowtheindexis010,convertthatintodecimalis2.
Hashinggivesanalternativeapproachthatisoftenthefastestandmostconvenientwayof
solving these problems like AI search programs, cryptography, networks, complexity
theory.
www.jntuworld.com || www.jwjobs.net
Page 8
UNITI
CSE
CollisionResolutionTechniques:
Ingeneral,ahashingfunctioncanmapseveralkeysintothesameaddress.Thatleadstoa
collision.Thecollidingrecordsmustbestoredandaccessedasdeterminedbyacollision
resolutiontechniques.
Therearetwobroadclassesofsuchtechniques:
OpenHashing(alsocalledseparatechaining)and
ClosedHashing(alsocalledopenaddressing)
The difference between the two has to do with whether collision are stored outside the
table(openhashing),orwhethercollisionresultinstoringandoftherecordsatanotherslot
inthetable(closedhashing).
The particular hashing method that one uses depends on many factors. One important
factoristheratiooftheno.ofkeysinthetabletotheno.ofhashaddresses.Itiscalledload
factor,andisgivenby:
Loadfactor()=n/m,
wherenisno.ofkeysinthetableandmisno.ofhashaddress(tablesize)
OpenHashing:
Thesimplestformofopenhashingdefineseachslotinthehashtabletobetheheadofa
linkedlist.Allrecordsthathashtoaparticularslotareplacedonthatslotslinkedlist.
Thebelowfigureillustratesahashtablewhereeachslotstoresonerecordandalinkpointer
totherestofthelist.
Considerthesameexampleofdivisionmethod:
www.jntuworld.com || www.jwjobs.net
Page 9
UNITI
CSE
Anykeythathashtothesameindexaresimplyaddedtothelinkedlist;thereisnoneedto
searchforemptycellsinthearray.Thismethodiscalledseparatingchaining.
ClosedHashing(Openaddressing):
Itresolvescollisionsintheprimeareathatisthatcontainsallofthehomeaddresses.
i.e.whenadataitemcannotbeplacedattheindexcalculatedbythehashfunction,another
locationinthearrayissought.
Therearedifferentmethodsofopenaddressing,whichvaryinthemethodusedtofindthe
nextvacantcell.
Theyare
(i)Linearprobing
(ii)Quadraticprobing
(iii)Pseudorandomprobing
(iv)Doublehashing
(v)Keyoffset
HashingwithLinearprobe:
Weresolvethecollisionbyadding1tothecurrentaddress.
Assumingthatthetableisnotfull
Weapplydivisionmethodofhashing
Considertheexample:
Addthekeys10,5,and15totheabovetable
H(k)=kmodtablesize
10mod8=2acollision,soadd1totheaddressthencheckisitemptyorfilled.Ifitisfilled
thenapplythesamefunction,likethiswecanplacethiskey10intheindex5cell.
Ifthephysicalendofthetableisreachedduringthelinearsearchwillwraparoundtothe
beginningofthetableandcontinuefromthere.
Ifanemptyslotisnotfoundbeforereachingthepointofcollision;thetableisfull
www.jntuworld.com || www.jwjobs.net
Page 10
UNITI
CSE
Aproblemwiththelinearprobemethodisthatitispossibleforblocksofdatatoformwhen
collisionsareresolved.Thisisknownasprimaryclustering.
Thismeansthatanykeythathashesintotheclusterwillrequireseveralattemptstoresolve
thecollision.
Linearprobeshavetwoadvantages:First,Theyarequitesimpletoimplement.Second,data
tendtoremainneartheirhomeaddress.
ExerciseExample:
Insert the nodes 89, 18, 49, 58, and 69 into a hash table that holds 10 items using the
divisionmethod.
HashingwithQuadraticprobe:
In this probe, rather than always moving one spot, move i2 spots from the point of
collision,whereiistheno.ofattemptstoresolvethecollision.
Inlinearprobe,iftheprimaryhashindexisx,subsequentprobegotox+1,x+2,x+3andso
on.InQuadraticprobing,probesgotox+1,x+4,x+9,andsoon,thedistancefromtheinitial
probeisthesquareofthestepnumber:x+12,x+22,x+32,x+42andsoon.
i.e., at first it picks the adjacent cell, if that is occupied, it tries 4 cells away, if that is
occupiedittries9cellsaway,andsoon.Iteliminatestheprimaryclusteringproblemwith
linearprobe.
Considertheaboveexerciseproblem,keys89,18,49,58,69withtablesize10.
Hereeachkeythathashestosamelocationwillrequirealongerprobe.Thisphenomenonis
calledsecondaryclustering.Itisnotaseriousproblem,butquadraticprobingisnotoften
usedbecausethereisaslightlybettersolution.
Narasaraopeta Engineering College
www.jntuworld.com || www.jwjobs.net
Page 11
UNITI
CSE
HashingwithPseudorandomprobing:
Thismethodusespseudorandomnumbertoresolvethecollisioni.e.thisprobefunction
wouldselectthenextpositionontheprobesequenceatrandomfromamongtheunvisited
slotsthatistheprobesequenceshouldbearandompermutationofhashtablepositions.
Unfortunately,wecantactuallyselectthenextpositionintheprobesequenceatrandom,
becausewewouldnotbeabletoduplicatethissameprobesequencewhensearchingfor
thekey.
Inthisprobing,theith slotintheprobesequenceis(h(key)+r1)modM)wherer1istheith
valueintherandompermutationofthenumbersfrom1toM1.
Allinsertionsandsearchesusethesamesequenceofrandomnumbers.
Considerthesameexampleofdivisionmethod:
36%8=4
18%8=2
72%8=0nowinsert60
43%8=360%8=4;isacollision
6%8=6
ThePseudorandompermutationtouseis:06523417
Forcollisionresolution
Currentslot=(4+0)%8=4;searchingslot4anditisoccupied
Again,Currentslot=(4+6)%8=2;occupied
Currentslot=(2+5)%8=1;itisempty;key60isoccupiesslot1
Pseudo random numbers are a relatively simple solution, but they have one significant
limitation all keys follow only one collision resolution path through the list. Because this
collisionresolutioncancreatesignificantsecondaryclustering
www.jntuworld.com || www.jwjobs.net
Page 12
UNITI
CSE
DoubleHashing:
Doublehashingusestheideaofapplyingasecondhashfunctiontothekeywhenacollision
occurs. The result of the second hash function will be the number of positions from the
pointofcollisiontoinsert.
Thereareacoupleofrequirementsforthesecondfunction:
Itmustneverevaluateto0
Mustmakesurethatallcellscanbeprobed
Apopularsecondhashfunctionis: H2(key)=R(key%R)
WhereRisaprimenumberthatissimilarthanthesizeofthetable
Tablesize=10
Hash1(key)=key%10
Hash2(key)=7(key%7)
Because7isaprimenumberthanthesizeofthetable
Insertkeys:89,18,49,58,69
Hash(89)=89%10=9
Hash(18)=18%10=8
Hash1(49)=49%10=9;itsacollision
Hash2(49)=7(49%7)=7;positionsfromlocation9
Hash1(58)=58%10=8;itsacollision
Hash2(58)=7(58%7)=5;positionsfromlocation8
NOTE:
Linearprobingmdistinctprobesequences,primaryclustering
Quadraticprobingmdistinctprobesequences,noprimarybutsecondaryclustering
Doublehashingm2distinctprobesequences,noprimaryandsecondaryclustering
www.jntuworld.com || www.jwjobs.net
Page 13
UNITI
CSE
Keyoffset:
Itisdoublehashingmethodthatproducesdifferentcollisionpathsfordifferentkeys.Where
as the pseudo random number generator produces a new address as a function of the
previousaddress;keyoffsetcalculatesthenewaddressasafunctionoftheoldaddressand
key.
Oneofthesimplestversionssimplyaddsthequotientofkeydividedbythelistsizetothe
addresstodeterminethenextcollisionresolutionaddress,asshownbelow
Offset= key/listsize
Address=((offset+oldaddress)modulolistsize))
Forexample:
Thekeyis166702andlistsizeis307,usingmodulodivisionhashingmethodgeneratesan
addressof1.Itsacollisionbecausetherewasakey070918.
Usingkeyoffsettocalculatethenextaddress,weget237asshownbelow
If 237 were also a collision, we would repeat the process to locate the next address, as
shownbelow
Ifitisfree,thenplacethekeyinthisaddress.
www.jntuworld.com || www.jwjobs.net
Page 14
UNITI
CSE
SkipLists:
Skiplistisatypeofdatastructurethatcanbeusedasanalternativetobalanced(binary)
treesorBTrees.Ascomparedtoabinarytree,skiplistsallowquicksearch,insertionsand
deletions of elements with simple algorithms. This is achieved by using probabilistic
balancingratherthanstrictlyenforcebalancingasdoneinBtrees.
SkiplistsarealsosignificantlyfasterthanequivalentalgorithmsforBTrees.
Askiplistisbasicallyalinkedlistwithadditionalpointersthatallowintermediatenodesto
beskipped,hencethenameskiplist.
Inasimplelinkedlistthatconsistsofnelements,toperformasearchncomparisonsare
requiredintheworstcase.
Forexample:
If a second pointer pointing two nodes ahead is added to every node, the number of
comparisonsgoesdownton/2+1intheworstcase.
Considerastoredlistwhereeveryothernodehasanadditionalpointer,tothenodetwoa
headofitinthelist.
Here,everyothernodehasanadditionalpointer.
Next,everysecondnodehasapointertwoaheadofit
www.jntuworld.com || www.jwjobs.net
Page 15
UNITI
CSE
In the list of above figure, every second node has a pointer two ahead of it; every fourth
+ 2
node has a pointer four ahead if it. Here we need to examine no more than
nodes.
Inbelowfigure,(every(2i)thnodehasapointer(2i)nodeahead(i=1,2,...);thenthenumber
ofnodestobeexaminedcanbereducedto log2n whileonlydoublingthenumberof
pointers.
Here,Every(2i)thnodehasapointertoanode(2i)nodesahead(i=1,2,...)
Anodethathaskforwardpointersiscalledalevelknode.Ifevery(2i)thnodehasa
pointer(2i)nodesahead,then
#oflevel1nodes50%
#oflevel2nodes25%
#oflevel3nodes12.5%
Suchadatastructurecanbeusedforfastsearchingbutinsertionsanddeletionswill
beextremelycumbersome,sincelevelsofnodeswillhavetochange.
What would happen if the levels of nodes were randomly chosen but in the same
proportions(belowfigure)?
o levelofanodeischosenrandomlywhenthenodeisinserted
o Anode'sithpointer,insteadofpointingtoanodethatis2i 1nodesahead,
pointstothenextnodeofleveliorhigher.
o Inthiscase,insertionsanddeletionswillnotchangethelevelofanynode.
o Somearrangementsoflevelswouldgivepoorexecutiontimesbutitcanbe
shownthatsucharrangementsarerare.
Suchalinkedrepresentationiscalledaskiplist.
Eachelementisrepresentedbyanodethelevelofwhichischosenrandomlywhen
the node is inserted, without regard for the number of elements in the data
structure.
Alevelinodehasiforwardpointers,indexed1throughi.Thereisnoneedtostore
thelevelofanodeinthenode.
Maxlevelisthemaximumnumberoflevelsinanode.
o Levelofalist=Maxlevel
o Levelofemptylist=1
o Levelofheader=Maxlevel
www.jntuworld.com || www.jwjobs.net
Page 16
UNITI
CSE
Itisaskiplist
Initialization:
AnelementNILisallocatedandgivenakeygreaterthananylegalkey.Alllevelsofalllists
areterminated withNIL.Anewlistisinitializedsothattheleveloflist= maxlevelandall
forwardpointersofthelist'sheaderpointtoNIL
Search:
We search for an element by traversing forward pointers that do not overshoot the node
containing the element being searched for. When no more progress can be made at the
currentlevelofforwardpointers,thesearchmovesdowntothenextlevel.Whenwecan
make no more progress at level 1, we must be immediately in front of the node that
containsthedesiredelement(ifitisinthelist).
InsertionandDeletion:
Insertionanddeletionarethroughsearchandsplice
update[i]containsapointertotherightmostnodeofleveliorhigherthatistothe
leftofthelocationofinsertionordeletion.
If an insertion generates a node with a level greater than the previous maximum
level,weupdatethemaximumlevelandinitializeappropriateportionsofupdatelist.
Afteradeletion,wechecktoseeifwehavedeletedthemaximumlevelelementof
thelistandifso,decreasethemaximumlevelofthelist.
BelowfigureprovidesanexampleofInsertandDelete.ThepseudocodeforInsert
andDeleteisshownbelow.
www.jntuworld.com || www.jwjobs.net
Page 17
UNITI
CSE
AnalysisofSkiplists:
Inaskiplistof16elements,wemayhave
9elementsatlevel1
3elementsatlevel2
3elementsatlevel3
1elementatlevel6
Oneimportantquestionis:
Wheredowestartoursearch?AnalysisshowsweshouldstartfromlevelL(n)where
L(n)=log2n
Ingeneralifpistheprobabilityfraction,
L(n)=log n
where p is the fraction of the nodes with level i pointers which also have level (i + 1)
pointers.
However,startingatthehighestleveldoesnotaltertheefficiencyinasignificant
way.
Anotherimportantquestiontoaskis:
WhatshouldbeMaxLevel?Agoodchoiceis
MaxLevel=L(N)=log N
whereNisanupperboundonthenumberofelementsisaskiplist.
Complexityofsearch,delete,insertisdominatedbythetimerequiredtosearchfor
the appropriate element. This in turn is proportional to the length of the search
path. This is determined by the pattern in which elements with different levels
appearaswetraversethelist.
Insertanddeleteinvolveadditionalcostproportionaltothelevelofthenodebeing
insertedordeleted.
www.jntuworld.com || www.jwjobs.net
Page 18
UNITII
CSE
BALANCEDTREES
Introduction:
Tree:
ATreeconsistsofafinitesetofelements,callednodes,andsetofdirectedlinescalled
branches,thatconnectthenodes.
Theno.ofbranchesassociatedwithanodeisthedegreeofthenode.
i.e.indegreeandoutdegree.
Aleafisanynodewithanoutdegreeofzero.i.e.nosuccessor
Anodethatisnotarootorleafisknownasaninternalnode
Anodeisaparentifithassuccessornode,converselyanodewithapredecessoris
calledchild
Apathisasequenceofnodesinwhicheachnodeisadjacenttothenextone
Thelevelofanodeisitsdistancefromtheroot
Theheightofthetreeistheleveloftheleafinthelongestpathfromtherootplus1
Asubtreeisanyconnectedstructurebelowtheroot.Subtreecanalsobefurther
dividedintosubtrees
Binarytree:
Abinarytreeisatreeinwhichnonodecanhavemorethantwosubtreesdesignatedasthe
leftsubtreeandtherightsubtree.
Note:eachsubtreeisitselfabinarytree.
Balancefactor:
Thebalancefactorofabinarytreeisthedifferenceinheightbetweenitsleftandrightsub
trees.
i.e.Balancefactor=HLHR
Inabalancedbinarytree,theheightofitssubtreesdiffersbynomorethanone(itsbalance
factoris1,0,+1)andalsoitssubtreesarealsobalanced.
www.jntuworld.com || www.jwjobs.net
Page 19
UNITII
CSE
Wenowturnourattentiontooperations:search,insertion,deletion
Inthedesignofthelinearliststructure,wehadtwochoices:anarrayoralinkedlist
Thearraystructureprovidesaveryefficientsearchalgorithm,butitsinsertionand
deletionalgorithmareveryinefficient.
Thelinkedliststructureprovidesefficientinsertionanddeletion,butitssearch
algorithmisveryinefficient.
Whatweneedisastructurethatprovidesanefficientsearch,atthesametimeefficient
insertionanddeletionalgorithms.
ThebinarysearchtreeandtheAVLtreeprovidethatstructure.
Binarysearchtree:
Abinarysearchtree(BST)isabinarytreewiththefollowingproperties:
Allitemsintheleftsubtreearelessthantheroot.
Allitemsintherightsubtreearegreaterthanorequaltotheroot.
Eachsubtreeisitselfabinarysearchtree.
Whilethebinarysearchtreeissimpleandeasytounderstand,ithasonemajorproblem:
Itisnotbalance.
Toovercomethisproblem,AVLtreesaredesigned,whicharebalanced.
AVLTREES
In1962,twoRussianmathematicians,G.MAdelsonvelskiiandE.MLandis,aeratedthe
balancedbinarytreestructurethatisnamedafterthemtheAVLtree.
AnAVLtreeisasearchtreeinwhichtheheightsofthesubtreesdifferbynomorethan1.It
isthusabalancedbinarytree.
AnAVLtreeisabinarytreethateitherisemptyorconsistsoftwoAVLsubtree,TLandTR,
whoseheightsdifferbynomorethan1.
|HLHR|<=1
WhereHListheheightoftheleftsubtree,HRistheheightoftherightsubtree
Thebarsymbolsindicateabsolutevalue.
NOTE:AnAVLtreeisaheightbalancedbinarysearchtree.
www.jntuworld.com || www.jwjobs.net
Page 20
UNITII
CSE
Consideranexample:AVLtree
AVLTreeBalancefactor:
TheBalancefactorforanynodeinanAVLtreemustbe+1,0,1.
Weusethedescriptiveidentifiers
LHforlefthigh(+1)toindicatethatthelengthsubtreeishigherthantherightsub
tree
EHforevenhigh(0)toindicatethatthesubtreearethesameheight
RHforrighthigh(1)toindicatethattheleftsubtreeisshortestthantherightsub
tree
BalancingTrees:
Wheneverweinsertanodeintoatreeordeleteanodefromatree,theresultingtreemay
beunbalancedthenwemustrebalanceit.
AVLtreesarebalancedbyrotatingnodeseithertotheleftortotheright.
Now,wearegoingtodiscussthebasicbalancingalgorithms.Theyare
1. Leftofleft:
Asubtreeofatreethatislefthighhasalsobecomelefthigh
2. Rightofright:
Asubtreeofatreethatisrighthighhasalsobecomerighthigh
3. Rightofleft:
Asubtreeofatreethatislefthighhasbecomerighthigh
4. Leftofright:
Asubtreeofatreethatisrighthighhasbecomelefthigh
www.jntuworld.com || www.jwjobs.net
Page 21
UNITII
CSE
Leftofleft:
Whentheoutofbalanceconditionhasbeencreatedbyalefthighsubtreeofalefthigh
tree,wemustbalancethetreebyrotatingtheoutofbalancenodetotheright.
LetsbeginwithaSimplecase:
Complexcase:
NOTE:
Intheabovetwocases,wehavesinglerotationright.
www.jntuworld.com || www.jwjobs.net
Page 22
UNITII
CSE
RightofRight:
Thiscaseisthemirrorofpreviouscase.Itcontainsasimpleleftrotation.
Simplecase:here,simpleleftrotation
Complexcase:here,complexleftrotation
NOTE:
Intheabovetwocases,wehavesinglerotationleft.
www.jntuworld.com || www.jwjobs.net
Page 23
UNITII
CSE
RightofLeft:
Theabovetwotypesrequiredsinglerotationtobalancethetrees.Nowwestudyabouttwo
outofbalanceconditionsinwhichweneedtorotatetwonodes,onetotheleftandone
totherighttobalancethetrees.
Simplecase:simpledoublerotationright.
Here,anoutofbalancetreeinwhichtherootislefthighandleftsubtreeisrighthigh
arightoflefttree.
Tobalancethistree,wefirstrotatetheleftsubtreetotheleft,thenwerotatetherootto
theright,makingtheleftnodethenewroot.
Complexcase:complexdoublerotationright.
www.jntuworld.com || www.jwjobs.net
Page 24
UNITII
CSE
LeftofRight:
Thiscaseisalsocomplicated
Simplecase:simpledoublerotation
Complexcase:
NOTE:
Inbothcases,i.e.RightofleftandLeftofright,wehavedoublerotations.
www.jntuworld.com || www.jwjobs.net
Page 25
UNITII
CSE
MaximumHeightofanAVLTree:
What is the maximum height of an AVL tree having exactly n nodes? To answer this
question, we will pose the following question:
What is the minimum number of nodes (sparsest possible AVL tree) an AVL tree of height h
can have ?
Let Fh be an AVL tree of height h, having the minimum number of nodes. Fh can be
visualized as in Figure.
Let Fl and Fr be AVL trees which are the left subtree and right subtree, respectively,
of Fh. Then Fl or Fr must have height h-2.
Suppose Fl has height h-1 so that Fr has height h-2. Note that Fr has to be an AVL tree
having the minimum number of nodes among all AVL trees with height of h-1.
Similarly, Fr will have the minimum number of nodes among all AVL trees of height
h--2. Thus we have
| Fh| = | Fh - 1| + | Fh - 2| + 1
where | Fr| denotes the number of nodes in Fr. Such trees are called Fibonacci trees.
See Figure. Some Fibonacci trees are shown in Figure 4.20. Note that | F0| = 1 and |
F1| = 2.
Adding 1 to both sides, we get
| Fh| + 1 = (| Fh - 1| + 1) + (| Fh - 2| + 1)
Thus the numbers | Fh| + 1 are Fibonacci numbers. Using the approximate formula for
Fibonacci numbers, we get
| Fh| + 1
h
1.44log| Fn|
1.44log n
www.jntuworld.com || www.jwjobs.net
Page 26
UNITII
CSE
www.jntuworld.com || www.jwjobs.net
Page 27
UNITII
CSE
23TREES
The basic idea behind maintaining a search tree is to make the insertion, deletion and
searchingoperationsefficient.
In AVL Trees the searching operation is efficient. However, insertion & deletion involves
rotationthatmakestheoperationcomplicated.
Toeliminatethiscomplicationadatastructurewasdesigned,calledas23trees.
To build a 2 3 tree there are certain rules that need to be followed. These rules are as
follows:
Allthenonleafnodesina23treemustalwayshavetwoorthreenonempty
childnodesthatareagain23trees.
Thelevelofalltheleafnodesmustalwaysbethesame.
Onesinglenodecancontain(leftandright)thenthatnodecontainssingledata.The
dataoccurringonleftsubtreeofthatnodeislessthanthedataofthenodeandthe
dataoccurringonrightsubtreeofthatnodeisgreaterthanthedataofthenode.
Ifanynodehasthreechildren(left,middle,right)thenthatnodecontainstwodata
values,letsayiandjwherei<j,thedataofallthenodesonthemiddlesubtreeare
greater than i but less than j and the data of all nodes on the right sub tree are
greaterthanj.
Exampleof23Trees:
www.jntuworld.com || www.jwjobs.net
Page 28
UNITII
CSE
Insertionsin23Trees:
Letusnowtrytounderstandtheprocessofinsertionofavaluein23trees.Toinserta
valueina23treeswefirstneedtosearchthepositionwherethevaluecanbeinserted,
andthenthevalueandnodeinwhichthevalueistobeinsertedareadjusted.
Algorithm:
Insertnewleafinappropriateplace
Repeatuntilallnonleafnodeshave2or3children
Ifthereisanodewith4children,splittheparentintotwoparentnodes,with2
childreneach
Ifyousplittheroot,thenaddanewroot
Adjustsearchvaluesalonginsertionpath
Example:
Insert5
5
Insert21
Insert8
Insert63
www.jntuworld.com || www.jwjobs.net
Page 29
UNITII
CSE
Insert69
Insert32
Insert7,9,25
www.jntuworld.com || www.jwjobs.net
Page 30
UNITII
CSE
Deletionsin23Trees:
Deletionofavaluefroma23treesisexactlyoppositetoinsertion
Incaseofinsertionthenodewherethedataistobeinsertedissplitifitalreadycontains
maximumno.ofvalues.Butincaseofdeletion,twonodesaremergedifthenodeofthe
valuetobedeletedcontainsminimumnumberofvalues(i.e.onlyonevalue)
Example1:
Considera23trees
Delete47
Delete63
www.jntuworld.com || www.jwjobs.net
Page 31
UNITII
CSE
Example2:
Delete47
TheResultant23Afterdeletionof47:
www.jntuworld.com || www.jwjobs.net
Page 32
UNITIII
CSE
PRIRORITYQUEUES
Apriorityqueueisanimportantdatatypeincomputerscience.Majoroperationssupported
bypriorityqueuesareInsertingandDeletemin.
Insert,whichdoestheobviousthing;andDeletemin,whichfinds,returns,andremovesthe
minimumelementinthepriorityqueue.
Thepriorityqueuesareextensiveusein.
ImplementingschedulersinOS,andDistributedsystems
Representingeventlistsindiscreteeventsimulation
Implementingnumerousgraphalgorithmsefficiently
Selectingkthlargestorkthsmallestelementinlists(orderstatisticsproblem)
SortingApplications
SimpleImplementation:
Thereareseveralwaystoimplementapriorityqueue
Linkedlist:storedandunsorted
PerforminginsertionsatfrontinO(1)andtraversingthelistwhichrequiresO(N)time
To delete the minimum, we could insist that the list be kept always sorted: this
makesinsertionsexpensiveO(N)anddeleteminscheapO(1)
Anotherwayofimplementingpriorityqueueswouldbeuseabinarysearchtree.
ThisgivesanO(logN)averagerunningtimeforbothoperations
Thebasicdatastructurewewillusewillnotrequirepointersandwillsupportboth
operationsinO(logN)worstcasetime.Theimplementationswewilluseisknownas
abinaryheap
BinaryHeaps:
Heaps(occasionallycalledas partially orderedtrees)areavery populardatastructurefor
implementingpriorityqueues.
Binary heaps are refer to merely as heaps, like binary search trees, heaps have two
properties,namely,astructurepropertyandaheaporderproperty.
www.jntuworld.com || www.jwjobs.net
Page 33
UNITIII
CSE
Structureproperty:
Aheapisabinarytreethatiscompletelyfilled,withthepossibleexceptionofthebottom
level,whichisfilledfromlefttoright,suchtreeisknownasacompletebinarytreeasshown
below
Abinaryheapisacompletebinarytreewithelementsfromapartiallyorderedset,suchthat
the element at every node is less than (or equal to) the element at its left child and the
elementatitsrightchild.
Itiseasytoshowthatacompletebinarytreeheighthhasbetween2h and2h+11nodes.
Thisimpliesthattheheightofacompletebinarytreeis logN ,whichisclearlyO(logN).
One important observation is that because a complete binary tree is so regular, it can be
representedinanarrayandnopointersarenecessary.
Sinceaheapisacompletebinarytree,theelementscanbeconvenientlystoredinanarray.
Ifanelementisatpositioniinthearray,thentheleftchildwillbeinposition2i,theright
childwillbeintheposition(2i+1),andtheparentisinposition i/2
Theonlyproblemwiththisimplementationisthatanestimateofthemaximumheapsizeis
requiredinadvance,buttypicallythisisnotaproblem.
Becauseoftheheapproperty,theminimumelementwillalwaysbepresentattherootof
theheap.ThusthefindminoperationwillhaveworstcaseO(1)runningtime.
www.jntuworld.com || www.jwjobs.net
Page 34
UNITIII
CSE
Heaporderproperty:
Itisthepropertythatallowsoperationstobeperformedquickly.Sincewewanttobeable
tofindtheminimumquickly,itmakessensethatthesmallestelementshouldbeattheroot.
If we consider that any sub tree should also be a heap, then any node should be smaller
thanallofitsdescendants.
Applyingthislogic,wearriveattheheaporderproperty.Inaheap,foreverynodeX,the
keyintheparentofXissmallerthan(orequalto)thekeyX,withexceptionoftheroot.
(Whichhasnoparent)?
NOTE:BinaryheapswerefirstintroducedbyWilliamsin1964.
NOTE:BinaryHeapiseitheraminheaporamaxheap.Aminheapsupportstheinsert
anddeleteminoperationswhileamaxheapsupportstheinsertanddeletemaxoperations
BasicHeapOperations:
It is easy to perform the two required operations. All the work involves ensuring that the
heaporderpropertyismaintained.
Insert:
Toinsertanelementsayx,intotheheapwithnelementswefirstcreateaholeinposition
(n+1)andseeiftheheappropertyisviolatedbyputtingxintothehole.Iftheheapproperty
is violated then we have found the current position for x. Otherwise we push up or
percolateupxuntiltheheappropertyisrestored.
Todothisweslidetheelementthatisintheholesparentnodeintotheholethusbubbling
theholeuptowardtheroot.Wecontinuethisprocessuntilxcanbeplacedinthewhole.
Considertheheap
www.jntuworld.com || www.jwjobs.net
Page 35
UNITIII
CSE
Wecreateaholeinthenextavailableheaplocation.Inserting14intheholewouldviolate
theheaporderpropertyso31issliddownintothehole.Thisstrategyiscontinueduntilthe
correctlocationfor14isfound.
Thisgeneralstrategyisknownasapercolateup.i.e.thenewelementispercolatedupthe
heapuntilthecorrectlocationisfound.
NOTE: Worst case complexity of insert is O(h) where h is the height of the heap. Thus
insertionsareO(logn)wherenistheno.ofelementsintheheap.
Deletemin:
Wheretheminimumisdeletedaholeiscreatedattherootlevel.Sincetheheapnowhas
onelesselementandtheheapisacompletebinarytree,theelementintheleastpositionis
toberelocated.
Thiswefirstdobyplacingthelastelementintheholecreatedattheroot.Thiswillleavethe
heappropertypossiblyviolatedattherootlevel.
Wenowpushdownorpercolatedowntheholeattherootuntiltheviolationofheap
propertyisstopped.Whilepushingdowntheholeitisimportanttoslideitdowntotheless
ofitstwochildren(pushingupthelatter).Thisisdonesoasnottocreateanotherviolation
ofheapproperty.
Considerthepreviousexample:
Firstremoveordeleteminis13.
www.jntuworld.com || www.jwjobs.net
Page 36
UNITIII
CSE
Thisgeneralstrategyisknownasapercolatedown.Weusesametechniqueasintheinsert
routinetoavoidtheuseofswapsinthisroutine.
NOTE:TheworstcaserunningtimeofdeleteminisO(logn)wherenistheno.ofelements
intheheap.
www.jntuworld.com || www.jwjobs.net
Page 37
UNITIII
CSE
CreatingHeap:
Thebuildheapoperationtakesasinputnelements.Theproblemhereistocreateaheapof
theseelementsi.e.placesthemintoanemptyheap.
Obvious approach is to insert the n element one at a time into an initially empty
heap.SinceeachinsertwilltakeO(1)averageandO(logn)worstcasetime,thetotal
runningtimeofthisalgorithmwouldbeO(n)averagebutO(nlogn)worstcase
for(i=n/2;I>0;i)
percolatedown(i)
Considerthetreeistheunorderedtree
Left:initialtree
Right:afterpercolatedown(7)
www.jntuworld.com || www.jwjobs.net
Page 38
Left:afterpercolatedown(6)
UNITIII
CSE
Right:afterpercolatedown(5)
Left:afterpercolatedown(4)
Right:afterpercolatedown(3)
Left:afterpercolatedown(2)
Right:afterpercolatedown(1)
Eachdashedlinecorrespondstotwocomparisons:onetofindthesmallestchildandoneto
comparethesmallerchildwiththenode.
Noticethatthereareonly10dashedlinesintheentirealgorithm(therecouldbeenan11th
where?)correspondingto20comparisons.
www.jntuworld.com || www.jwjobs.net
Page 39
UNITIII
CSE
Toboundingtherunningtimeofbuildheap,wemustboundtheno.ofdashedlines.This
canbedonebycomputingthesumoftheheightsofallthenodesintheheap,whichisthe
maximumno.ofdashedlines.WhatwewouldliketoshowisthatthisisO(n).
THEOREM:
For the perfect binary tree of height h containing 2h+11 nodes, the sum of the heights of
nodesis2h+11(h+1)
Proof:
Itiseasytoseethatthistreeconsistsof1nodeatheighth,2nodesatheighth1,22 nodes
atheighth2,andingeneral2inodesatheighthi
ThesumoftheheightofallthenodesisthenS=2i(hi)wherei=otoh
S=h+2(h1)+4(h2)+8(h3)+16(h4)++2h1(1)
Multiplyingby2givestheequation
S=h+2+4+8+16++2h1+2h
ThereforeS=(2h+11)(h+1)whichprovesthetheorem
It is easy to see that the above is an upper bound on the sum of heights of nodes of a
complete binary tree. Since a complete binary tree of height h has between 2h and 2h+1
nodes,theabovesumisthereforeO(n)
Wherenistheno.ofnodesintheheap
Sincetheworstcasecomplexityoftheheapbuildingalgorithmisoftheorderofthesumof
height of the nodes of the heap built, we then have the worst case complexity of heap
buildingasO(n)
www.jntuworld.com || www.jwjobs.net
Page 40
UNITIII
CSE
BinomialQueues:
Weknowthatpreviousconceptssupportmerging,insertion,anddeleteminalleffectivelyin
O(logn)timeperoperationbutinsertiontakeconstantaveragetime.
BinomialQueuessupportallthreeoperationsinO(logn)worstcasetimeperoperation,but
insertionstakeconstanttimeonaverage.
BinomialQueueStructure:
Itdiffersfromallthepriorityqueueimplementationsthatabinomialqueueisnotaheap
orderedtreebutratheracollectionofheaporderedtreesknownasaforest.
Eachoftheheaporderedtreesisofaconstrainedfromknownasabinomialtree.Thereis
atmostonebinomialtreeofeveryheight.
Abinomialtreeofheight0isaonenodetree
AbinomialtreeBk ofheightkisformedbyattachingabinomialtreeBk1totheroot
ofanotherbinomialtreeBk1
B0B1B2B3
The above diagram shows binomial trees B0 B1 B2 and B3 from the diagram we see that a
binomialtreeBkconsistsofarootwithchildrenB0B1B2Bk1
NOTE:Binomialtreeofheightkhaveexactly2knodesandtheno.ofnodesatdepthdisthe
binomialcoefficientkCd
NOTE:Ifweimposeheaporderonthebinomialtreeandallowatmostonebinomialtreeof
anyheightwecanuniquelyrepresentapriorityqueueofanysizebyacollectionofbinomial
trees(forest).
www.jntuworld.com || www.jwjobs.net
Page 41
UNITIII
CSE
Forinstance,apriorityqueueofsize13couldberepresentedbytheforestB3B2B0
We might write this representation as 1 1 0 1. Which not only represent 13 in binary but
alsorepresentthefactthatB3B2andB0arepresentintherepresentationandB1isnot.
Asanexample,apriorityqueueofsixelementscouldberepresentedasinbelowfigure
H1:
Figure:BinomialqueueH1withsixelements
BinomialQueueoperations:
Findmin:
Thisisimplementedbyscanningtherootsoftheentiretree.Sincethereareatmostlogn
differenttrees,theminimumcanbefoundinO(logn)time.
Alternatively, one can keep track of the current minimum and perform find min in O(1)
time.Ifweremembertoupdatetheminimumifitchangesduringotheroperations.
Merge:
Merging two binomial queues is a conceptually easy operation, which we will describe by
example.
Considerthetwo binomialqueuesH1 andH2 withsixandsevenelementsrespectivelyas
shownbelow.
H1:with6elements
www.jntuworld.com || www.jwjobs.net
Page 42
UNITIII
CSE
H2:with7elements
MergeoftwoB1trees(i.e.21=2nodes)inH1andH2.i.e.
Nowweleftwith1treeofheight0and3treesofheight2
BinomialqueueH3:theresultofmergingH1andH2
H3:with13elements
www.jntuworld.com || www.jwjobs.net
Page 43
UNITIII
CSE
Themergingisperformedbyessentiallyaddingthetwoqueuestogether.
LetH3bethenewbinomialqueue.
SinceH1hasnobinomialtreeofheight0,andH2does,wecanjustusethebinomialtreeof
heightoinH2aspartofH3.
Next,weaddbinomialtreesofheight1.
SincebothH1andH2havebinomialtreeofheight1,wemergethembymakingthelarger
rootasubtreeofthesmaller,creatingabinomialtreeofheight2.
Thus,H3willnothaveabinomialtreeofheight1asshownintheabovediagrams.
Therearenowthreebinomialtreesofheight2,namely,theoriginaltreesinbothH1andH2
plusthetreeformedbyaddingofheight1treeinbothH1andH2.
Wekeeponebinomialtreeofheight2inH3andmergetheothertwo,creatingabinomial
treeofheight3.
SinceH1andH2havenotreesofheight3,thistreebecomespartofH3andwearefinished.
Theresultingbinomialqueueisasshowninabovefigure.
Since merging two binomial trees takes constant time with almost any reasonable
implementation,andthereareO(logn)binomialtree,themergetakesO(logn)timeinthe
worstcase.
Tomakethisoperationefficient,weneedtokeepthetreesinthebinomialqueuesortedby
height,whichiscertainlyasimplethingtodo.
Insertion:
Insertionisaspecialcaseofmerging,sincewemerelycreateaonenodetreeand
performamerge.
TheworstcasetimeofthisoperationislikewiseO(logn)
Moreprecisely,ifthepriorityqueueintowhichtheelementisbeinginsertedhasthe
property that the smallest non existent binomial tree is Bi the running time is
proportionaltoi+1.
www.jntuworld.com || www.jwjobs.net
Page 44
UNITIII
CSE
Forexample:
In The previous example, H3 is missing a binomial tree of height 1, so the insertion will
terminateintwosteps.Sinceeachtreeinabinomialqueueispresentwithprobability.
IfwedefinetherandomvariableXasrepresentingtheno.ofstepsinaninsertoperation,
then
X=1withprobability1/2(B0notpresent)
X=2withprobability1/2(B0andB1notpresent)
X=3withprobability1/8
Thusaveragenumberofstepsinaninsertoperation=2.
Thus we expect an insertion to terminate in two steps on the average. Further more
performingninsertsonaninitiallyemptybinomialqueuewilltakeO(n)worstcasetime.
Indeeditispossibletodothisoperationusingonly(n1)comparisons.
Consideranexample,thebinomialqueuethatareformedbyinserting1through7inorder.
After1isinserted:
After2isinserted:
After3isinserted:
After4isinserted:
www.jntuworld.com || www.jwjobs.net
Page 45
UNITIII
CSE
After5isinserted:
After6isinserted:
After7isinserted:
Ifweinsert8then
Inserting4showsoffabadcase,wemerge4withB0 obtaininganewtreeofheight1.We
mergethistreewithB1obtainingatreeofheight2whichisthenewpriorityqueue.
Thenextinsertionafter7isanotherbadcaseandwouldrequirethreemerges.
www.jntuworld.com || www.jwjobs.net
Page 46
UNITIII
CSE
Deletemin:
ConsiderthesameexampleofmergeoperationwhichhasH3.
H3:
Theminimumrootis12soweobtainthetwopriorityqueuesH&H
ThebinomialqueuethatresultsfrommergingH&Hisasshownbelow
NOTE:TheentiredeleteminoperationtakesO(logn)worstcasetime
Narasaraopeta Engineering College
www.jntuworld.com || www.jwjobs.net
Page 47
UNITIII
CSE
BinomialAmortizedAnalysis:
AmortizedAnalysisofMerge
To merge two binomial queues, an operation similar to addition of binary integers is
performed:
Atanystage,wemayhavezero,one,two,orthreeBk trees,dependingonwhetherornot
thetwopriorityqueuescontainaBktreeandwhetherornotaBktreeiscarriedoverfrom
thepreviousstep.
IfthereiszeroormoreBktree,itisplacedasatreeintheresultingbinomialqueue.
Iftherearetwo,theyaremergedintoaBk+1treeandcarriedover
Iftherearethree,oneisretainedandothertwomerged.
Result 1:
Amortized Analysis
Consider the result of an insertion.
If there is no B0 tree, then the insertion costs one time unit. The result of
insertion is that there is now a B0 tree and the forest has one more tree.
o If there is a B0 tree but not B1 tree, then insertion costs 2 time units. The new
forest will have a B1 tree but not a B0 tree. Thus number of trees in the forest is
unchanged.
o An insertion that costs 3 time units will create a B2 tree but destroy a B0 and
B1, yielding one less tree in the forest.
o In general, an insertion that costs c units results in a net increase of 2 - c trees.
Since
a Bc - 1 tree is created
all Bi trees, 0
i
c - 1 are removed.
o
www.jntuworld.com || www.jwjobs.net
Page 48
UNITIII
CSE
Thus expensive insertions remove trees and cheap insertions create trees.
Let ti =
ci =
We have
=0
c0
ti + (ci - ci - 1) = 2
Result2:
TheamortizedrunningtimesofInsert,Deletemin,andMergeare0(1),0(logn),and
0(logn)respectively.
Potentialfunction=#oftreesinthequeue
Toprovethisresultwechoose:
Insertion
ti
ci
ai
= ti +(ci ci1)
ai
= 2
= 2n (cn c0)
Aslongas(cnc0)ispositive,wearedone.
Inanycase(cnc0)isboundedbylognifwestartwithanemptytree.
Merge:
Assumethatthetwoqueuestobemergedhaven1andn2nodeswithT1andT2trees.Letn=
n1+n2.Actualtimetoperformmergeisgivenby:
ti =0(logn1 +logn2)
=0(max(logn1,logn2)
=0(logn)
(cici1)isatmost(logn)sincetherecanbeatmost(logn)treesaftermerge.
Deletemin:
Theanalysisherefollowsthesameargumentasformerge.
www.jntuworld.com || www.jwjobs.net
Page 49
UNITIII
CSE
LazyBinomialQueues:
Binomialqueuesinwhichmergingisdonelazily.
Here,tomergetwobinomialqueues,wesimplyconcatenatethetwolistsofbinomialtrees.
Intheresultingforest,theremaybeseveraltreesofthesamesize.
Becauseofthelazymerge,mergeandinsertarebothworstcase0(1)time.
Deletemin:
o Convertlazybinomialqueueintoastandardbinomialqueue
o Dodeleteminasinstandardqueue.
FibonacciHeaps
Fibonacciheapsupportsallbasicheapoperationsin0(1)amortizedtime,withtheexception
ofdeleteminanddeletewhichtake0(logn)amortizedtime.
Fibonacciheapsgeneralizebinomialqueuesbyaddingtwonewconcepts:
Adifferentimplementationofdecreasekey
Lazymerging:Twoheapsaremergedonlywhenitisrequired.
It can be shown in a Fibonacci heap that any node of rank r
descendant.
www.jntuworld.com || www.jwjobs.net
1 has at least Fr + 1
Page 50
UNITIV
CSE
GRAPHS
Inthischapter,weturnourattentiontoadatastructureGraphsthatdiffersfromallof
the other in one major concept: each node may have multiple predecessors as well as
multiplesuccessors.
Graphs are very useful structures. They can be used to solve complex routing problems,
suchasdesigningandroutingairlinesamongtheairportstheyserve.Similarly,theycanbe
usedtoroutemessagesoveracomputernetworkfromonenodetoanother.
BasicConcepts:
A graph is a collection of nodes, called vertices and a collection of segments called lines
connectingpairofvertices.Inotherwordsagraphconsistsoftwosets,asetofverticesand
setoflines.
Graphsmaybeeitherdirectedorundirected.
A directed graph or digraph is a graph in which each line has a direction (arrow
head)toitssuccessor.Thelineinadirectedgraphisknownasarc.Theflowalong
thearcbetweentwoverticescanfollowonlytheindirecteddirection.
Anundirectedgraphisagraphinwhichthereisnodirection(arrowhead)onanyof
thelines,whichareknownasedges.Theflowbetweentwoverticescangoineither
direction.
Apathisasequenceofverticesinwhicheachvertexisadjacenttothenextone.
Forexample:{A,B,C,E}isaonepathand{A,B,E,F}isanother.
NOTE:Bothdirectedandundirectedgraphshavepaths.
Narasaraopeta Engineering College
www.jntuworld.com || www.jwjobs.net
Page 51
UNITIV
CSE
Twoverticesinagrapharesaidtobeadjacentvertices(orneighbors)ifthereisapathof
length1connectingthem.
Considertheabovediagrams
Indirectedgraph,BisadjacenttoA,whereasEisnotadjacenttoD;butDisadjacenttoE.
Inundirectedgraph,EandDareadjacent,butDandFarenot.
Acycleisapath,itstartwithvertexandendswithsamevertex.
Example:
ABCAisacycle
Aloopisaspecialcaseofcycleinwhichasinglearcbeginsandendswiththesamevertex.
Inalooptheendpointsofthelinearethesame.
Twoverticesaresaidtobeconnectedifthereisapathbetweenthem.Agraphissaidtobe
connectedif,ignoringdirection,thereisapathfromanyvertextoanyothervertex.
A directed graph is strongly connected if there is a path from each vertex to every other
vertexinthedigraph.
www.jntuworld.com || www.jwjobs.net
Page 52
UNITIV
CSE
Agraphisadisjointgraphifitisnotconnected
Thedegreeofavertexistheno/oflinesincidenttoit
Theoutdegreeofavertexinadigraphistheno.ofarcsleavingthevertex
Theindegreeistheno.ofarcsenteringthevertex
Forexample:forvertexB;degree=3,indegree=1,outdegree=2
NOTE:Atreeisagraphinwhicheachvertexhasonlyonepredecessor;howeveragraphis
notatree.
OperationsonGraphs:
Therearesixprimitivegraphoperationsthatprovidethebasicmodulesneededtomaintain
agraph.Theyare
1.
2.
3.
4.
5.
6.
Insertavertex
Deleteavertex
Addanedge
Deleteanedge
Findavertex
Traverseagraph
Vertexinsertion:
Insertvertexaddsanewvertextoagraph
Whenavertexisinserteditisdisjoint;itisnotconnectedtoanyotherverticesinthelist
Afterinsertingavertexitmustbeconnected
Thebelowdiagramshowsagraphbeforeandafteranewvertexisadded
Narasaraopeta Engineering College
www.jntuworld.com || www.jwjobs.net
Page 53
UNITIV
CSE
Algorithm:
Algorithminsertvertex(graph,data)
Allocatememoryfornewvertex
Storedatainnewvertex
Incrementgraphcount
if(emptygraph)
Setgraphfirsttonewnode
else
Searchforinsertionpoint
if(insertingbeforefirstvertex)
Setgraphfirsttonewvertex
else
Insertnewvertexinsequence
endif
endinsertvertex
Vertexdeletion:
Delete vertex removes a vertex from the graph when a vertex is deleted; all connecting
edgesarealsoremoved
www.jntuworld.com || www.jwjobs.net
Page 54
UNITIV
CSE
Algorithm:
Algorithmdeletevertex(graph,key)
Return
+1ifsuccessful
1ifdegreenotzero
2ifkeyisnotfound
if(emptygraph)
return2;
endif
searchforvertextobedeleted
if(notfound)
return2;
endif
if(vertexindegree>0orindegree>0)
return1;
endif
deletevertex
decrementgraphcount
return1;
enddeletevertex
Edgeaddition:
Addedgeconnectsavertextodestinationvertex.Ifavertexrequiresmultipleedges,add
anedgemustbecalledonceforeachadjacentvertex.Toaddanedge,twoverticesmustbe
specified.Ifthegraphisadigraph,oneoftheverticesmustbespecifiedasthesourceand
oneasthedestination.
Thebelowdiagramshowsaddinganedge{A,E}tothegraph
www.jntuworld.com || www.jwjobs.net
Page 55
UNITIV
CSE
Algorithm:
AlgorithminsertArc(graph,fromkey,tokey)
Return
+1ifsuccessful
2iffromkeynotfound
3iftokeynotfound
Allocatememoryfornewarc
Searchandsetfromvertex
if(fromvertexnotfound)
return2;
endif
searchandsettovertex
if(tovertexnotfound)
return3;
endif
incrementfromvertexoutdegree
incrementtovertexindegree
setarcdestinationtotovertex
if(fromvertexarclistempty)
setfromvertexfirstArctonewarc
setnewarcnextArctonull
return1;
endif
findinsertionpointinarclist
if(insertatbeginningofarclist)
setfromvertexfirstArctonewarc
else
insertinarclist
endif
return1;
endinsertArc
www.jntuworld.com || www.jwjobs.net
Page 56
UNITIV
CSE
Edgedeletion:
Deleteedgeremovesoneedgefromagraph.
Belowdiagramshowsthatdeletedtheedge{B,E}fromthegraph
Algorithm:
AlgorithmdeleteArc(graph,fromkey,tokey)
Return
+1ifsuccessful
2iffromkeynotfound
3iftokeynotfound
if(emptygraph)
return2;
endif
searchandsetfromvertextotovertexwithkeyequaltofromkey
if(fromvertexarcnotfound)
return2;
endif
if(fromvertexarclistnull)
return3;
endif
searchandfindarcwithkeyequaltotokey
if(tokeynotfound)
return3;
enfif
settovertextoarcdestination
deletearc
decrementfromvertexoutdegree
decrementtovertexindegree
return1;
enddeleteArc
www.jntuworld.com || www.jwjobs.net
Page 57
UNITIV
CSE
Findvertex:
Findvertextraverseagraph,lookingforaspecifiedvertex.Ifthevertexisfounditsdataare
returnedandifitisnotfoundthenanerrorisindicated.
Thebelowfigureshowsfindvertextraversesthegraph,lookingforvertexC
Algorithm:
Algorithmretrievevertex(graph,key,dataout)
Return
+1ifsuccessful
2ifkeynotfound
if(emptygraph)
return2;
endif
searchforvertex
if(vertexfound)
movelocptrdatatodataout
return1;
else
return2;
endif
endretrievevertex
www.jntuworld.com || www.jwjobs.net
Page 58
UNITIV
CSE
GraphStorageStructure:
Torepresentagraph,weneedtostoretwosets.Thefirstsetrepresentstheverticesofthe
graphandthe secondsetrepresentstheedgesorarcs.Thetwomostcommonstructures
usedtostorethesesetsarearraysandlinkedlists.Althoughthearraysoffersomesimplicity
thisisamajorlimitation.
AdjacencyMatrix:
Theadjacencymatrixusesavector(onedimensionalarray)fortheverticesandamatrix
(twodimensionalarray)tostoretheedges.Iftwoverticesareadjacentthatisifthereis
noedgebetweenthem,intersectissetto0.
Ifthegraphisdirected,theintersectionintheadjacencymatrixindicatesthedirection
Inthebelowdiagram,thereisanarcfromsourcesvertexBtodestinationvertexC.Inthe
adjacencymatrix,thisarcisseenasa1intheintersectionfromB(ontheleft)toC(onthe
top).BecausethereisnoarcfromCtoB,however,theintersectionfromCtoBis0.
www.jntuworld.com || www.jwjobs.net
Page 59
UNITIV
CSE
NOTE:Inadjacencymatrixrepresentation,weuseavectortostoretheverticesandamatrix
tostoretheedges.
Inadditiontothelimitationthatthesizeofgraphmustbeknowbeforetheprogramstarts,
there is another serious limitation in the adjacency matrix: only one edge can be stored
between any two vertices. Although this limitation does not prevent many graphs from
usingthematrixformat,somenetworkstructuresrequiremultiplelinesbetweenvertices.
Adjacencylist:
Theadjacencylistusesatwodimensionalraggedarraytostoretheedges.Anadjacency
listisshownbelow.
The vertex list is asingly linked list of vertices in the list. Depending on the application, it
couldalsobeimplementedusingdoublylinkedlistsorcircularlylinkedlists.Thepointerat
the left of the list links the vertex entries. The pointer at the right in the vertex is a head
pointertoalinkedlistofedgesfromthevertex.Thus,inthenondirectedgraphontheleft
inabovefigurethereisapathfromvertexBtoverticesA,C,andE.Tofindtheseedgesin
theadjacencylist,westartatBsvertexlistentryandtraversethelinkedlisttoA,thentoC,
andfinallytoE.
NOTE:Intheadjacencylist,weusealinkedlisttostoretheverticesandatwodimensional
linkedlisttostorethearcs.
www.jntuworld.com || www.jwjobs.net
Page 60
UNITIV
CSE
Traversegraph:
There is always at least one application that requires that all vertices in a given graph be
visited;aswetraversethegraph,wesetthevisitedflagtoontoindicatethatthedatahave
beenprocessed
That is traversal of a graph means visiting each of its nodes exactly once. This is
accomplishedbyvisitingthenodesinasystematicmanner
Therearetwostandardgraphtraversals:depthfirstandbreadthfirst.Bothusevisitedflag
DepthFirstTraversal:
Inthedepthfirsttraversal,weprocessallofavertexsdescendantsbeforewemovetoan
adjacentvertex.Thisconceptismosteasilyseenwhenthegraphisatree
Inthebelowfigureweshowthetreepreordertraversalprocessingsequence,oneofthe
standarddepthfirsttraversals
In a similar manner, the depth first traversal of a graph starts by processing the first
vertex;weselectanyvertexadjacenttothefirstvertexandprocessit.Thiscontinuesuntil
wefoundnoadjacententries
Thisissimilartoreachingaleafinatree.Werequireastacktocompletethetraversal
i.e.lastinfirstout(LIFO)order
Letstraceadepthfirsttraversalthroughthegraphinbelowfigurethenumberinginthe
boxnexttoavertexindicatestheprocessingorder
TraceoftheDFS:
1. Webeginbypushingthefirstvertex,intothestack
www.jntuworld.com || www.jwjobs.net
Page 61
UNITIV
CSE
2. Wethenloop,popthestackandafterprocessingthevertex,pushalloftheadjacent
verticesintothestack
3. Whenthestackisemptytraversaliscompleted
NOTE: In the depth first traversal, all of a nodes descendents are processed before
movingtoanadjacentnode
Considertheabovegraph,letnodeAbethestartingvertex
1. BeginwithnodeApushontostack
2. Whilestacknotequaltoempty
PopA;stateAisvisited
PushnodesadjacenttoAtostackandmaketheirstatewaiting
3. PopX;stateBisvisited
PushnodesadjacenttoXintostack
4. PopH;stateHisvisited
PushnodesadjacenttoHintostackalreadyGisinwaitingstate,thenpushnodesE
andP
5. PopP;statePisvisited
Narasaraopeta Engineering College
www.jntuworld.com || www.jwjobs.net
Page 62
UNITIV
CSE
Thistraversalstartsatlevel0andthenprocessesalltheverticesinlevel1beforegoingon
toprocesstheverticesinlevel2.
Thebreadthfirsttraversalofagraphfollowsthesameconcept,beginbypickingastarting
vertexAafterprocessingit,processallofitsadjacentverticesandcontinuethisprocess
untilgetnoadjacentvertices
Narasaraopeta Engineering College
www.jntuworld.com || www.jwjobs.net
Page 63
UNITIV
CSE
Thebreadthfirsttraversalusesaqueueratherthanastack.Asweprocesseachvertex,we
placeallofitsadjacentverticesinthequeue.Thentoselectthenextvertextobeprocessed,
wedeleteavertexfromthequeueandprocessit.
TraceoftheBFS:
1. WebeginbyenqueuingvertexAinthequeue
2. Wethenloop,dequeuingthequeueandprocessingthevertexfromthefrontof
thequeue.Afterprocessingthevertex,weplaceallofitsadjacentverticesinto
thequeue.ThusintheabovediagramwedequeuevertexX,processit,andthen
placeverticesGandHinthequeue.
3. Whenthequeueisempty,thetraversaliscomplete.
NOTE:Inthebreadthfirsttraversal,alladjacentverticesareprocessedbeforeprocessing
thedescendentsofavertex.
Letstracethislogicthroughthegraphinbelowfigure:
www.jntuworld.com || www.jwjobs.net
Page 64
UNITIV
CSE
Algorithms:
DepthFirstSearch:
Policy:Dontpushnodestwice
//nonrecursive,preorder,depthfirstsearch
voiddfs(Nodev){
if(v==null)
return;
push(v);
while(stackisnotempty){
pop(v);
if(vhasnotyetbeenvisited)
mark&visit(v);
for(eachwadjacenttov)
if(whasnotyetbeenvisited&¬yetstacked)
push(w);
}//while
}//dfs
BreadthFirstSearch:
//nonrecursive,preorder,breadthfirstsearch
voidbfs(Nodev){
if(v==null)
return;
enqueue(v);
while(queueisnotempty){
dequeue(v);
if(vhasnotyetbeenvisited)
mark&visit(v);
for(eachwadjacenttov)
if(whasnotyetbeenvisited&&hasnotbeenqueued)
enqueue(w);
}//while
}//bfs
www.jntuworld.com || www.jwjobs.net
Page 65
UNITIV
CSE
Exerciseproblems:
www.jntuworld.com || www.jwjobs.net
Page 66
UNITV
CSE
GRAPHALGORITHMS
The trees are the special case of graphs. A tree may be defined as a connected graph
withoutanycycles.
Aspanningtreeofagraphisasubgraphwhichisbasicallyatreeanditcontainsallthe
verticesofgraphcontainingnocycles.
Minimumcostspanningtree:
Anetworkisagraphwhoselinesareweighted.Itisalsoknownasaweightedgraph.The
weight is an attribute of an edge. In an adjacency matrix, the weight is stored as the
intersectionvalue.Inanadjacencylist,itisstoredasthevalueintheadjacencylinkedlist.
A minimum cost spanning tree is a spanning tree in which the total weight of lines is
guaranteedtobetheminimumofallpossibletreesinthegraph.
Narasaraopeta Engineering College
www.jntuworld.com || www.jwjobs.net
Page 65
UNITV
CSE
Example:
Beforegoingintothetypesofalgorithmswhichwillapplytogettheminimumspanningtree
ofagraph
Letsmanuallydeterminetheminimumcostspanningtree
Wecanstartwithanyvertex.Becausethevertexlistisusuallykeysequenced,
LetsstartwithA
Theaboveexampleshowsagraphandoneofitsminimumcostspanningtrees.Sincethe
identificationofaminimumcostspanningtreeinvolvestheselectionofasubsetofedges.
Applicationsofspanningtree:
1. Spanningtreesareveryimportantindesigningefficientroutingalgorithms
2. Spanningtreesarehavewideapplicationsinmanyareassuchasnetworkdesign
www.jntuworld.com || www.jwjobs.net
Page 66
UNITV
CSE
There are two popular techniques to construct a minimum cost spanning tree from a
weighted graph. One such method is known as Prims algorithm and another one is
Kruskalsalgorithm
PrimsAlgorithm:
Theprimsalgorithmis implementedusingtheadjacencymatrixofagraph.Thismatrixis
denoted by adjMatrix [i, j] where i and j operate from 0 to n1 for n node weighted
undirectedgraph.
We can represent a weighted graph by an adjacency matrix to store the set of edges, an
entry (i, j) in an adjacency matrix contains information on the edge that goes from the
vertexitothevertexj.eachmatrixentrycontainstheweightofthecorrespondingedge.
Letsconsideranexample:
Nowthesequenceofbelowfiguresshowtheworkingofprimsalgorithm:
www.jntuworld.com || www.jwjobs.net
Page 67
UNITV
CSE
KruskalsAlgorithm:
Like Prims algorithm, Kruskals algorithm also constructs the minimum spanning tree of a
graph by adding edges to the spanning tree one by one. At all points during its
execution, the set of edges selected by prims algorithm forms exactly one tree, on the
otherhand,thesetofedgesselectedbyKruskalsalgorithmformsaforestoftrees.
Letusconsideragraph
www.jntuworld.com || www.jwjobs.net
Page 68
UNITV
CSE
Theedgesofthegrapharearrangedinincreasingorderofweightsasshownbelow.Initially
thespanningtree,Tisempty.WeselecttheedgewithsmallestweightandincludeitinT.
Ifselectededgecreatesacycle,thenitwillberemovedfromT.Werepeatthesetwosteps
untilthetreeTcontainsn1edges(wherenisthenumberofverticesinthegraph)
Ifthetreecontainslessthann1edgesandtheedgelistisempty,thennospanningtreeis
possibleforthegraph
www.jntuworld.com || www.jwjobs.net
Page 69
UNITV
CSE
MinimumcostspanningtreeusingKruskalsalgorithm
www.jntuworld.com || www.jwjobs.net
Page 70
UNITV
CSE
ShortestpathAlgorithm:
Aminimumspanningtreegivesnoindicationabouttheshortestpathbetweentwovertices.
Ratheronlytheoverallcostorweightisminimized.Inreallife,wearerequiredtofindthe
shortestpathbetweentwocities.
Forexample:onewouldbeinterestedinfindingmosteconomicalroutebetweenanytwo
citiesinagivenrailwaysnetwork.
WearegivenadirectedgraphGinwhicheveryedgehasaweightandourproblemistofind
apathfromonevertexvtoanothervertexwsuchthatthesumoftheweightsonthepath
isasminimalaspossible.Weshallcallsuchpathashortestpath.
The shortest path from vertex A to vertex E is ADCE and has a total cost of 12
comparedtothecostof20fortheedgedirectlyfromAtoEandthecostof14forthepath
ABCE
It turns out that is just easy to solve the more general problem of starting at one vertex,
calledthesource,andfindingtheshortestpathtoeveryothervertex,insteadoftojustone
destinationvertex.Forsimplicity,wetakethesourcetobevertexAandourproblemthen
consistsoffindingtheshortestpathfromvertexAtoeveryothervertexinthegraph.
DijkstrasAlgorithm:
ThesolutionwewillshowfortheshortestpathproblemiscalledDijkstrasAlgorithm,after
Edsger Dijkstra, who first described it in 1959. This algorithm is based on the adjacency
matrixrepresentationofagraph.Somewhatsurprisingly,itfindsnotonlytheshortestpath
fromonespecifiedvertextoanother,buttheshortestpathsfromthespecifiedvertextoall
theothervertices.
DijkstrasAlgorithmisoftencalledasgreedytechnique.
www.jntuworld.com || www.jwjobs.net
Page 71
UNITV
CSE
The algorithm works by maintaining a set S of vertices whose shortest distance from the
sourceisalreadyknown.
Whered[i]containsthelengthofthecurrentshortestpathfromsourcevertextovertexi
LetsapplyDijkstrasalgorithmtothegivendigraphforgettingstagesofalgorithm
www.jntuworld.com || www.jwjobs.net
Page 72
UNITV
CSE
www.jntuworld.com || www.jwjobs.net
Page 73
UNITV
CSE
Exerciseproblems:
1. Constructaminimumcostspanningtreeofagivengraphbyusingprimsalgorithm
andkruskalsalgorithm
2. Find the minimum cost spanning tree for the below graphs using prims and
kruskalsalgorithms
www.jntuworld.com || www.jwjobs.net
Page 74
UNITV
CSE
3. FindtheshortestpathfromAtoallotherverticesusingDijkstrasalgorithmforthe
graphasshownbelow
4. FindtheshortestpathbyusingDijkstrasalgorithmonthefollowingedgeweighted
directedgraphwiththevertexPasthesource
www.jntuworld.com || www.jwjobs.net
Page 75