Vous êtes sur la page 1sur 77

Jntuk Fast Updates

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. Data may be single or a set of values and it is to be organized in a particular fashion. This organization orstructuringofdatawillhaveprofoundimpactontheefficiencyoftheprogram. WhatisaDataStructure? Data structure is the structural representation of logical relationships between elements of data. In other words a data structure is a way of organizing data items by considering its relationship to each 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 The time complexity of an algorithm or a program is the amount of time it needs to run tocompletion.
Narasaraopeta Engneering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 1

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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.

Narasaraopeta Engneering College


www.jntukfastupdates.com

Page 2

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 3

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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(n) O(n)

O(n) O(1) O(n)

Among these, the sorted list has the best average case performance. Inthischapter,wediscusstwodatastructuresfordictionaries,namelyHashTablesandSkip Lists.

Narasaraopeta Engneering College


www.jntukfastupdates.com

Page 4

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITI

CSE

HASHING
DivisionMethod:
Onecommonmethodofdeterminingahashkeyofthedivisionmethodofhashing Theformulathatwillbeusedis: i.e. H(key)=key%no.ofslotsinthetable 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.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 5

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITI

CSE

Multiplicationmethod:
The simplest situation when the keys are floating point numbers known to be in affixed range. Forexample: If the keys are numbers that are greater than 0 and less than 1, we can just multiply by m (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.

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 6

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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)=?.


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 7

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITI

CSE

Universalmethod:
Hashingisafunideathathaslotsofunexpecteduses.Here,welookatanoveltypeofhash function that makes it easy to create a family of universal hash functions. The method is basedonarandombinarymatrixandisverysimpletoimplement. The idea is very simple. Suppose you havean input dataitem that you have input data with m bits and you want a hash function that produces n bits then first generate a random binarymatrix(M)ofordernxm. Thehashfunctionis h(x)=MxWherextobeabinaryvector For example, Suppose you have a key 11, the binary form is 1011 and it is a four bit input 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. There is no.of other ways to look at the way the arithmetic is done that suggest different waysofimplementingthealgorithm. Hashing gives an alternative approach that is often the fastest and most convenient way of solving these problems like AI search programs, cryptography, networks, complexity theory.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 8

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITI

CSE

CollisionResolutionTechniques:
In general, a hashing function can map several keys into the same address. That leads to a collision. The colliding records must be stored and accessed as determined by a collision 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 factor is the ratio of the no.of keys in the table to the no.of hash addresses. It is called load factor,andisgivenby: Loadfactor()=n/m, wherenisno.ofkeysinthetableandmisno.ofhashaddress(tablesize)

OpenHashing:
The simplest form of open hashing defines each slot in the hash table to be the head of a linkedlist.Allrecordsthathashtoaparticularslotareplacedonthatslotslinkedlist. Thebelowfigureillustratesahashtablewhereeachslotstoresonerecordandalinkpointer totherestofthelist. Considerthesameexampleofdivisionmethod:

Narasaraopeta Engineering College


www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 9

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITI

CSE

Any key that hash to the same index are simply added to the linked list; there is no need to searchforemptycellsinthearray.Thismethodiscalledseparatingchaining.

ClosedHashing(Openaddressing):
Itresolvescollisionsintheprimeareathatisthatcontainsallofthehomeaddresses. i.e.whenadataitemcannotbeplacedattheindexcalculatedbythehashfunction,another locationinthearrayissought. There are different methods of open addressing, which vary in the method used to find the nextvacantcell. Theyare (i)Linearprobing (ii)Quadraticprobing (iii)Pseudorandomprobing (iv)Doublehashing (v)Keyoffset HashingwithLinearprobe: Weresolvethecollisionbyadding1tothecurrentaddress. Assumingthatthetableisnotfull Weapplydivisionmethodofhashing Considertheexample:

Addthekeys10,5,and15totheabovetable H(k)=kmodtablesize 10 mod 8 = 2 a collision, so add 1 tothe addressthen check is itempty or filled. If it is filled thenapplythesamefunction,likethiswecanplacethiskey10intheindex5cell.

If the physical end of the table is reached during the linear search will wrap around to the beginningofthetableandcontinuefromthere. Ifanemptyslotisnotfoundbeforereachingthepointofcollision;thetableisfull
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 10

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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. In linear probe, if the primary hash index is x, subsequent probe go to x+1, x+2, x+3 and so 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 occupied it tries 9 cells away, and so on. It eliminates the primary clustering problem with linearprobe. Considertheaboveexerciseproblem,keys89,18,49,58,69withtablesize10.

Hereeachkeythathashestosamelocationwillrequirealongerprobe.Thisphenomenonis called secondary clustering. It is not a serious problem, but quadratic probing is not often usedbecausethereisaslightlybettersolution.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 11

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITI

CSE

HashingwithPseudorandomprobing: This method uses pseudo random number to resolve the collision i.e. this probe function would select the next position on the probe sequence at random from among the unvisited slotsthatistheprobesequenceshouldbearandompermutationofhashtablepositions. Unfortunately, we cant actually select the next position in the probe sequence at random, because we would not be able to duplicate this same probe sequence when searching for thekey. In this probing, the ith slot in the probe sequence is (h (key) + r1) mod M) where r1 is the ith 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
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 12

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 13

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITI

CSE

Keyoffset: Itisdoublehashingmethodthatproducesdifferentcollisionpathsfordifferentkeys.Where as the pseudo random number generator produces a new address as a function of the previousaddress;keyoffsetcalculatesthenewaddressasafunctionoftheoldaddressand key. One of the simplest versions simply adds the quotient of key divided by the list size to the addresstodeterminethenextcollisionresolutionaddress,asshownbelow Offset= key/listsize Address=((offset+oldaddress)modulolistsize)) Forexample: The key is 166702 and list size is 307, using modulo division hashing method generates an addressof1.Itsacollisionbecausetherewasakey070918. Usingkeyoffsettocalculatethenextaddress,weget237asshownbelow Offset= 166702/307 =543 Address=((543+001))modulo307=237 If 237 were also a collision, we would repeat the process to locate the next address, as shownbelow Offset= 166702/307 =543 Address=((543+237))modulo307=166 Ifitisfree,thenplacethekeyinthisaddress.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 14

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITI

CSE

SkipLists: Skip list is a type of data structure that can be used as an alternative to balanced (binary) trees or BTrees. As compared to a binary tree, skip lists allow quick search, insertions and deletions of elements with simple algorithms. This is achieved by using probabilistic balancingratherthanstrictlyenforcebalancingasdoneinBtrees. SkiplistsarealsosignificantlyfasterthanequivalentalgorithmsforBTrees. A skip list is basically a linked list with additional pointers that allow intermediate nodes to beskipped,hencethenameskiplist. In a simple linked list that consists of n elements, to perform a search n comparisons are requiredintheworstcase. Forexample:

If a second pointer pointing two nodes ahead is added to every node, the number of comparisonsgoesdownton/2+1intheworstcase. Consider a stored list where every other node has an additional pointer, to the node two a headofitinthelist. Here,everyothernodehasanadditionalpointer.

Next,everysecondnodehasapointertwoaheadofit


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 15

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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 of nodes to be examined can be reduced to log2n while only doubling the number of pointers. Here,Every(2i)thnodehasapointertoanode(2i)nodesahead(i=1,2,...)

A node that has k forward pointers is called a level k node. If every (2i)th node has a pointer(2i)nodesahead,then #oflevel1nodes50% #oflevel2nodes25% #oflevel3nodes12.5% Such a data structure can be used for fast searching but insertions and deletions will beextremelycumbersome,sincelevelsofnodeswillhavetochange. What would happen if the levels of nodes were randomly chosen but in the same proportions(belowfigure)? o levelofanodeischosenrandomlywhenthenodeisinserted o A node's ith pointer, instead of pointing to a node that is 2i 1 nodes ahead, pointstothenextnodeofleveliorhigher. o Inthiscase,insertionsanddeletionswillnotchangethelevelofanynode. o Some arrangements of levels would give poor execution times but it can be shownthatsucharrangementsarerare. Suchalinkedrepresentationiscalledaskiplist. Each element is represented by a node the level of which is chosen randomly when the node is inserted, without regard for the number of elements in the data structure. A level i node has i forward pointers, indexed 1 through i. There is no need to store thelevelofanodeinthenode. Maxlevelisthemaximumnumberoflevelsinanode. o Levelofalist=Maxlevel o Levelofemptylist=1 o Levelofheader=Maxlevel
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 16

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITI

CSE

Itisaskiplist

Initialization: An element NIL is allocated and given a key greater than any legal key. All levels of all lists are terminated with NIL. A new list is initialized so that the level of list = maxlevel and all 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 current level of forward pointers, the search moves down to the next level. When we can make no more progress at level 1, we must be immediately in front of the node that containsthedesiredelement(ifitisinthelist). InsertionandDeletion: Insertionanddeletionarethroughsearchandsplice update [i] contains a pointer to the rightmost node of level i or higher that is to the leftofthelocationofinsertionordeletion. If an insertion generates a node with a level greater than the previous maximum level,weupdatethemaximumlevelandinitializeappropriateportionsofupdatelist. After a deletion, we check to see if we have deleted the maximum level element of thelistandifso,decreasethemaximumlevelofthelist. Below figure provides an example of Insert and Delete. The pseudo code for Insert andDeleteisshownbelow.


Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 17

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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. Complexity of search, delete, insert is dominated by the time required to search for 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. Insert and delete involve additional cost proportional to the level of the node being insertedordeleted.

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 18

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 19

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 20

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 21

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITII

CSE

Leftofleft: Whentheoutofbalanceconditionhasbeencreatedbyalefthighsubtreeofalefthigh tree,wemustbalancethetreebyrotatingtheoutofbalancenodetotheright. LetsbeginwithaSimplecase:

Complexcase:

NOTE: Intheabovetwocases,wehavesinglerotationright.
Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 22

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITII

CSE

RightofRight: Thiscaseisthemirrorofpreviouscase.Itcontainsasimpleleftrotation. Simplecase:here,simpleleftrotation

Complexcase:here,complexleftrotation

NOTE: Intheabovetwocases,wehavesinglerotationleft.
Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 23

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITII

CSE

RightofLeft: Theabovetwotypesrequiredsinglerotationtobalancethetrees.Nowwestudyabouttwo outofbalanceconditionsinwhichweneedtorotatetwonodes,onetotheleftandone totherighttobalancethetrees. Simplecase:simpledoublerotationright.

Here,anoutofbalancetreeinwhichtherootislefthighandleftsubtreeisrighthigh arightoflefttree. Tobalancethistree,wefirstrotatetheleftsubtreetotheleft,thenwerotatetherootto theright,makingtheleftnodethenewroot. Complexcase:complexdoublerotationright.


Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 24

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITII

CSE

LeftofRight: Thiscaseisalsocomplicated Simplecase:simpledoublerotation

Complexcase:

NOTE: Inbothcases,i.e.RightofleftandLeftofright,wehavedoublerotations.
Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 25

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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|

The sparsest possible AVL tree with n nodes has height h 1.44log n

The worst case height of an AVL tree with n nodes is 1.44log n

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 26

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITII

CSE

Figure 5.3: Fibonacci trees

Figure 5.4: Rotations in a binary search tree


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 27

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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: All the non leaf nodes in a 2 3 tree must always have two or three non empty childnodesthatareagain23trees. Thelevelofalltheleafnodesmustalwaysbethesame. One single node can contain (leftand right) thenthat node containssingle data. The dataoccurringonleftsubtreeofthatnodeislessthanthedataofthenodeandthe dataoccurringonrightsubtreeofthatnodeisgreaterthanthedataofthenode. If any node has three children (left, middle, right) then that node contains two data 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:


Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 28

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITII

CSE

Insertionsin23Trees: Letusnowtrytounderstandtheprocessofinsertionofavaluein23trees.Toinserta valueina23treeswefirstneedtosearchthepositionwherethevaluecanbeinserted, andthenthevalueandnodeinwhichthevalueistobeinsertedareadjusted. Algorithm: Insertnewleafinappropriateplace Repeatuntilallnonleafnodeshave2or3children Ifthereisanodewith4children,splittheparentintotwoparentnodes,with2 childreneach Ifyousplittheroot,thenaddanewroot Adjustsearchvaluesalonginsertionpath Example: Insert5 5 Insert21

Insert8

Insert63

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 29

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITII

CSE

Insert69

Insert32

Insert7,9,25


Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 30

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITII

CSE

Deletionsin23Trees: Deletionofavaluefroma23treesisexactlyoppositetoinsertion Incaseofinsertionthenodewherethedataistobeinsertedissplitifitalreadycontains maximumno.ofvalues.Butincaseofdeletion,twonodesaremergedifthenodeofthe valuetobedeletedcontainsminimumnumberofvalues(i.e.onlyonevalue) Example1: Considera23trees

Delete47

Delete63


Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 31

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITII

CSE

Example2:

Delete47

TheResultant23Afterdeletionof47:


Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 32

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

PRIRORITYQUEUES
Apriorityqueueisanimportantdatatypeincomputerscience.Majoroperationssupported bypriorityqueuesareInsertingandDeletemin. Insert,whichdoestheobviousthing;andDeletemin,whichfinds,returns,andremovesthe minimumelementinthepriorityqueue. Thepriorityqueuesareextensiveusein. 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 The basic data structure we will use will not require pointers and will support both operationsinO(logN)worstcasetime.Theimplementationswewilluseisknownas abinaryheap ImplementingschedulersinOS,andDistributedsystems Representingeventlistsindiscreteeventsimulation Implementingnumerousgraphalgorithmsefficiently Selectingkthlargestorkthsmallestelementinlists(orderstatisticsproblem) SortingApplications

BinaryHeaps:
Heaps (occasionally called as partially ordered trees) are a very popular data structure for implementingpriorityqueues. Binary heaps are refer to merely as heaps, like binary search trees, heaps have two properties,namely,astructurepropertyandaheaporderproperty.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 33

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

Structureproperty: A heap is a binary tree that is completely filled, with the possible exception of the bottom 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. It is easy to show that a complete binary tree height h has between 2h and 2h+11 nodes. Thisimpliesthattheheightofacompletebinarytreeis logN ,whichisclearlyO(logN). One important observation is that because a complete binary tree is so regular, it can be representedinanarrayandnopointersarenecessary. Sinceaheapisacompletebinarytree,theelementscanbeconvenientlystoredinanarray. If an element is at position i in the array, then the left child will be in position 2i, the right childwillbeintheposition(2i+1),andtheparentisinposition i/2 Theonlyproblem withthisimplementationisthatanestimateofthemaximumheapsizeis requiredinadvance,buttypicallythisisnotaproblem. Because of the heap property, the minimum element will always be present at the root of theheap.ThusthefindminoperationwillhaveworstcaseO(1)runningtime.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 34

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

Heaporderproperty: It is the property that allows operations to be performed quickly. Since we want to be able tofindtheminimumquickly,itmakessensethatthesmallestelementshouldbeattheroot. If we consider that any sub tree should also be a heap, then any node should be smaller thanallofitsdescendants. Applying this logic, we arrive at the heap order property. In a heap, for every node X, the keyintheparentofXissmallerthan(orequalto)thekeyX,withexceptionoftheroot. (Whichhasnoparent)? NOTE:BinaryheapswerefirstintroducedbyWilliamsin1964. NOTE: Binary Heap is either a min heap or a max heap. A min heap supports the insert anddeleteminoperationswhileamaxheapsupportstheinsertanddeletemaxoperations

BasicHeapOperations:
It is easy to perform the two required operations. All the work involves ensuring that the heaporderpropertyismaintained. Insert: To insert an element say x, into the heap with n elements we first create a hole in position (n+1)andseeiftheheappropertyisviolatedbyputtingxintothehole.Iftheheapproperty is violated then we have found the current position for x. Otherwise we push up or percolateupxuntiltheheappropertyisrestored. To do this we slide the element that is in the holes parent node into the hole thus bubbling theholeuptowardtheroot.Wecontinuethisprocessuntilxcanbeplacedinthewhole. Considertheheap

Narasaraopeta Engineering College


www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 35

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

We create a hole in the next available heap location. Inserting 14 in the hole would violate theheaporderpropertyso31issliddownintothehole.Thisstrategyiscontinueduntilthe correctlocationfor14isfound.

This general strategy is known as a percolate up. i.e. the new element is percolated up the heapuntilthecorrectlocationisfound. NOTE: Worst case complexity of insert is O(h) where h is the height of the heap. Thus insertionsareO(logn)wherenistheno.ofelementsintheheap. Deletemin: Where the minimum is deleted a hole is created at the root level. Since the heap now has onelesselementandtheheapisacompletebinarytree,theelementintheleastpositionis toberelocated. Thiswefirstdobyplacingthelastelementintheholecreatedattheroot.Thiswillleavethe heappropertypossiblyviolatedattherootlevel. We now push down or percolate down the hole at the root until the violation of heap propertyisstopped.Whilepushingdowntheholeitisimportanttoslideitdowntotheless of its two children (pushing up the latter). This is done so as not to create another violation ofheapproperty. Considerthepreviousexample: Firstremoveordeleteminis13.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 36

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

Thisgeneralstrategy isknown as apercolatedown. We usesame technique as in theinsert routinetoavoidtheuseofswapsinthisroutine. NOTE: The worst case running time of delete min is O(log n) where n is the no. of elements intheheap.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 37

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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 Another approach proposed by Floyd in 1964 is to use a procedure called push downorpercolatedownrepeatedly.Startingwiththearrayconsistingofthegiven nelementsintheinputorder. If percolate down (i) percolate down from node i, perform the algorithm to create aheaporderedtree. for(i=n/2;I>0;i) percolatedown(i) Considerthetreeistheunorderedtree Left:initialtree Right:afterpercolatedown(7)


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 38

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

Left:afterpercolatedown(6)

Right:afterpercolatedown(5)

Left:afterpercolatedown(4)

Right:afterpercolatedown(3)

Left:afterpercolatedown(2) Right:afterpercolatedown(1)

Eachdashedlinecorrespondstotwocomparisons:onetofindthesmallestchildandoneto comparethesmallerchildwiththenode. Notice that there are only 10 dashed lines in the entire algorithm (there could been an 11th where?)correspondingto20comparisons.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 39

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

To bounding the running time of build heap, we must bound the no. of dashed lines. This can be done by computing the sum of the heights of all the nodes in the heap, which is the 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: It is easy to see that this tree consistsof 1node at height h, 2 nodes at height h1, 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 Sincetheworstcasecomplexity of theheapbuildingalgorithmisoftheorderofthesumof height of the nodes of the heap built, we then have the worst case complexity of heap buildingasO(n)
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 40

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

BinomialQueues:
Weknowthatpreviousconceptssupportmerging,insertion,anddeleteminalleffectivelyin O(logn)timeperoperationbutinsertiontakeconstantaveragetime. BinomialQueuessupportallthreeoperationsinO(logn)worstcasetimeperoperation,but insertionstakeconstanttimeonaverage. BinomialQueueStructure: It differs from all the priority queue implementations that a binomial queue is not a heap orderedtreebutratheracollectionofheaporderedtreesknownasaforest. Eachoftheheaporderedtreesisofaconstrainedfromknownasabinomialtree.Thereis atmostonebinomialtreeofeveryheight. Abinomialtreeofheight0isaonenodetree A binomial tree Bk of height k is formed by attaching a binomial tree Bk1 to the root 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).
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 41

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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: This is implemented by scanning the roots of the entire tree. Since there are at most log n 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. Consider the two binomial queues H1 and H2 with six and seven elements respectively as shownbelow. H1:with6elements


Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 42

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

H2:with7elements

MergeoftwoB1trees(i.e.21=2nodes)inH1andH2.i.e.

Nowweleftwith1treeofheight0and3treesofheight2

BinomialqueueH3:theresultofmergingH1andH2 H3:with13elements

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 43

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

Themergingisperformedbyessentiallyaddingthetwoqueuestogether. LetH3bethenewbinomialqueue. SinceH1hasnobinomialtreeofheight0,andH2does,wecanjustusethebinomialtreeof heightoinH2aspartofH3. Next,weaddbinomialtreesofheight1. Since both H1 and H2 have binomial tree of height 1, we merge them by making the larger rootasubtreeofthesmaller,creatingabinomialtreeofheight2. Thus,H3willnothaveabinomialtreeofheight1asshownintheabovediagrams. Therearenowthreebinomialtreesofheight2,namely,theoriginaltreesinbothH1andH2 plusthetreeformedbyaddingofheight1treeinbothH1andH2. We keep one binomial tree of height 2 in H3 and merge the other two, creating a binomial treeofheight3. SinceH1andH2havenotreesofheight3,thistreebecomespartofH3andwearefinished. Theresultingbinomialqueueisasshowninabovefigure. Since merging two binomial trees takes constant time with almost any reasonable implementation, and there are O(log n) binomial tree, the merge takes O(log n) time in the worstcase. Tomakethisoperationefficient,weneedtokeepthetreesinthebinomialqueuesortedby height,whichiscertainlyasimplethingtodo. Insertion: Insertion is a special case of merging, since we merely create a one node tree and performamerge. TheworstcasetimeofthisoperationislikewiseO(logn) Moreprecisely,ifthepriorityqueueintowhichtheelementisbeinginsertedhasthe property that the smallest non existent binomial tree is Bi the running time is proportionaltoi+1.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 44

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

Forexample: In The previous example, H3 is missing a binomial tree of height 1, so the insertion will terminateintwosteps.Sinceeachtreeinabinomialqueueispresentwithprobability. If we define the random variable X as representing the no. of steps in an insert operation, 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:


Narasaraopeta Engineering College
www.jntukfastupdates.com

Page 45

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

After5isinserted:

After6isinserted:

After7isinserted:

Ifweinsert8then

Inserting 4 shows off a bad case, we merge 4 with B0 obtaining a new tree of height 1. We mergethistreewithB1obtainingatreeofheight2whichisthenewpriorityqueue. Thenextinsertionafter7isanotherbadcaseandwouldrequirethreemerges.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 46

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

Deletemin: A delete min can be performed by first finding the binomial tree with the smallest root. LetthistreebeBkandlettheoriginalpriorityqueuebeH Remove the binomial tree Bk from the forest of trees in H forming the new binomial queueH Now remove the root of Bk creating binomial trees B0 B1 Bk 1 which collectively frompriorityqueueH. FinishtheoperationbymergingH&H ConsiderthesameexampleofmergeoperationwhichhasH3. H3:

Theminimumrootis12soweobtainthetwopriorityqueuesH&H

ThebinomialqueuethatresultsfrommergingH&Hisasshownbelow

NOTE:TheentiredeleteminoperationtakesO(logn)worstcasetime
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 47

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIII

CSE

BinomialAmortizedAnalysis:
AmortizedAnalysisofMerge To merge two binomial queues, an operation similar to addition of binary integers is performed: At any stage, we may have zero, one, two, or three Bk trees, depending on whether or not the two priority queues contain a Bk tree and whether or not a Bk tree is carried over from thepreviousstep. IfthereiszeroormoreBktree,itisplacedasatreeintheresultingbinomialqueue. Iftherearetwo,theyaremergedintoaBk+1treeandcarriedover Iftherearethree,oneisretainedandothertwomerged. Result 1:

A binomial queue of n elements can be built by n successive insertions in 0(n) time. Brute force Analysis Define the cost of each insertions to be o 1time unit + an extra unit for each linking step thus the total will be n units plus the total number of linking steps. o The 1st, 3rd, ... and each odd-numbered step requires no linking steps since there is no B0 present. o A quarter of the insertions require only one linking step: 2nd, 6th, 10, ... o One eighth of insertions require two linking steps. We could do all this and bound the number of linking steps by n. The above analysis will not help when we try to analyze a sequence of operations that include more than just insertions.

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

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 48

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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: The amortized running times of Insert, Deletemin, and Merge are 0(1),0(log n), and 0(logn)respectively. Potentialfunction=#oftreesinthequeue Toprovethisresultwechoose: Insertion ti ci ai ai =

= ti +(ci ci1) = 2 i

= 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.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 49

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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. 1 has at least Fr + 1

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 50

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIV

CSE

GRAPHS
In this chapter, we turn our attention to a data structure Graphs that differs from all of 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, such as designing and routing airlines among the airports they serve. Similarly, they can be 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) to its successor. The line in a directed graph is known as arc. The flow along thearcbetweentwoverticescanfollowonlytheindirecteddirection. Anundirectedgraphisagraphinwhichthereisnodirection(arrowhead)onanyof thelines,which areknownasedges.Theflowbetweentwoverticescangoineither direction.

Apathisasequenceofverticesinwhicheachvertexisadjacenttothenextone. Forexample:{A,B,C,E}isaonepathand{A,B,E,F}isanother. NOTE:Bothdirectedandundirectedgraphshavepaths.


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 51

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIV

CSE

Two vertices in a graph are said to be adjacent vertices (or neighbors) if there is a path of length1connectingthem. Considertheabovediagrams Indirectedgraph,BisadjacenttoA,whereasEisnotadjacenttoD;butDisadjacenttoE. Inundirectedgraph,EandDareadjacent,butDandFarenot. Acycleisapath,itstartwithvertexandendswithsamevertex. Example:

ABCAisacycle

A loop is a special case of cycle in which a single arc begins and ends with the same vertex. Inalooptheendpointsofthelinearethesame. Twoverticesaresaidtobeconnectedifthereisapathbetweenthem.Agraphissaidtobe connectedif,ignoringdirection,thereisapathfromanyvertextoanyothervertex. A directed graph is strongly connected if there is a path from each vertex to every other vertexinthedigraph.

A directed graph is weakly connected if at least two vertices are connected (A connected undirected graph would always be strongly connected, so the concept is not normally used withundirectedgraphs)
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 52

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIV

CSE

Agraphisadisjointgraphifitisnotconnected Thedegreeofavertexistheno/oflinesincidenttoit Theoutdegreeofavertexinadigraphistheno.ofarcsleavingthevertex Theindegreeistheno.ofarcsenteringthevertex Forexample:forvertexB;degree=3,indegree=1,outdegree=2 NOTE: A tree is a graph in whicheach vertex hasonly one predecessor; how ever a graph is notatree.

OperationsonGraphs:
Therearesixprimitivegraphoperationsthatprovidethebasicmodulesneededtomaintain agraph.Theyare 1. 2. 3. 4. 5. 6. Vertexinsertion: Insertavertex Deleteavertex Addanedge Deleteanedge Findavertex Traverseagraph

Insertvertexaddsanewvertextoagraph Whenavertexisinserteditisdisjoint;itisnotconnectedtoanyotherverticesinthelist Afterinsertingavertexitmustbeconnected Thebelowdiagramshowsagraphbeforeandafteranewvertexisadded


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 53

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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

Narasaraopeta Engineering College


www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 54

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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: Add edge connects a vertex to destination vertex. If a vertex requires multiple edges, add anedgemustbecalledonceforeachadjacentvertex.Toaddanedge,twoverticesmustbe specified. If the graph is a digraph, one of the vertices must be specified as the source and oneasthedestination. Thebelowdiagramshowsaddinganedge{A,E}tothegraph


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 55

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 56

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 57

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 58

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIV

CSE

GraphStorageStructure:
To represent a graph, we need to store two sets. The first set represents the vertices of the graph and the second set represents the edges or arcs. The two most common structures usedtostorethesesetsarearraysandlinkedlists.Althoughthearraysoffersomesimplicity thisisamajorlimitation. AdjacencyMatrix: The adjacency matrix uses a vector (one dimensional array) for the vertices and a matrix (twodimensionalarray)tostoretheedges.If twoverticesareadjacentthatisifthereis noedgebetweenthem,intersectissetto0.

Ifthegraphisdirected,theintersectionintheadjacencymatrixindicatesthedirection In the below diagram, there is an arc from sources vertex B to destination vertex C. In the adjacency matrix, this arc is seen as a 1 in the intersection from B (on the left) to C (on the top).BecausethereisnoarcfromCtoB,however,theintersectionfromCtoBis0.

Narasaraopeta Engineering College


www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 59

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIV

CSE

NOTE:Inadjacencymatrixrepresentation,weuseavectortostoretheverticesandamatrix tostoretheedges. In addition to the limitation that the size of graph must be know before the program starts, 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: The adjacency list uses a two dimensional ragged array to store the edges. An adjacency listisshownbelow.

The vertex list is a singly linked list of vertices in the list. Depending on the application, it could also be implemented using doubly linked lists or circularly linked lists. The pointer at the left of the list links the vertex entries. The pointer at the right in the vertex is a head pointertoalinkedlistofedgesfromthevertex.Thus,inthenondirectedgraphontheleft in above figure there is a path from vertex B to vertices A, C, and E. To find these edges in theadjacencylist,westartatBsvertexlistentryandtraversethelinkedlisttoA,thentoC, andfinallytoE. NOTE:Intheadjacencylist,weusealinkedlisttostoretheverticesandatwodimensional linkedlisttostorethearcs.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 60

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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 In the below figure we show the tree pre order traversal processing sequence, one of the standarddepthfirsttraversals

In a similar manner, the depth first traversal of a graph starts by processing the first vertex; we select any vertex adjacent to the first vertex and process it. This continues until wefoundnoadjacententries Thisissimilartoreachingaleafinatree.Werequireastacktocompletethetraversal i.e.lastinfirstout(LIFO)order Lets trace a depth first traversal through the graph in below figure the numbering in the boxnexttoavertexindicatestheprocessingorder TraceoftheDFS: 1. Webeginbypushingthefirstvertex,intothestack

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 61

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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 Push nodes adjacent to H into stack already G is in waiting state, then push nodes E andP 5. PopP;statePisvisited
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 62

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIV

CSE

Push nodes adjacent to P are H, G, E; H is already in visited state, G and E are in waitingstate 6. PopE;stateEisvisited Pushadjacentnodes,Hisalreadyvisited,sopushYandMintothestack 7. PopY;stateYisvisited PushnodesadjacenttoYintostack,Eisvisited,Malreadyinwaitingstate 8. PopM;stateMisvisited PushnodesadjacenttoM,whichisJ 9. PopJ;stateJisvisited Nonodesaretheretobeprocess 10. PopG;stateGisvisited Nowthestackisempty ThedepthfirstorderofthevisitednodesareAXHPEYMJG BreadthFirsttraversal: Inthebreadthfirsttraversalofagraph,weprocessalladjacentverticesofavertexbefore goingtothenextlevel.Wefirstsawthebreadthfirsttraversalofatreeasshowninbelow

Thistraversalstartsatlevel0andthenprocessesalltheverticesinlevel1beforegoingon toprocesstheverticesinlevel2. Thebreadthfirsttraversalofagraphfollowsthesameconcept,beginbypickingastarting vertexAafterprocessingit,processallofitsadjacentverticesandcontinuethisprocess untilgetnoadjacentvertices


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 63

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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:


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 64

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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&&notyetstacked) 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
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 65

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITIV

CSE

Exerciseproblems:

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 66

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITV

CSE

GRAPHALGORITHMS
The trees are the special case of graphs. A tree may be defined as a connected graph withoutanycycles. A spanning tree of a graph is a sub graph which is basically a tree and it contains all the verticesofgraphcontainingnocycles.

Minimumcostspanningtree:
A network is a graph whose lines are weighted. It is also known as a weighted graph. 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.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 65

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITV

CSE

Example:

Beforegoingintothetypesofalgorithmswhichwillapplytogettheminimumspanningtree ofagraph Letsmanuallydeterminetheminimumcostspanningtree Wecanstartwithanyvertex.Becausethevertexlistisusuallykeysequenced, LetsstartwithA

The above example shows a graph and one of its minimum cost spanning trees. Since the identificationofaminimumcostspanningtreeinvolvestheselectionofasubsetofedges. Applicationsofspanningtree: 1. Spanningtreesareveryimportantindesigningefficientroutingalgorithms 2. Spanningtreesarehavewideapplicationsinmanyareassuchasnetworkdesign
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 66

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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:
The prims algorithm is implemented using the adjacency matrix of a graph. This matrix is 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:

Narasaraopeta Engineering College


www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 67

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

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

Narasaraopeta Engineering College


www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 68

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITV

CSE

Theedgesofthegrapharearrangedinincreasingorderofweightsasshownbelow.Initially thespanningtree,Tisempty.WeselecttheedgewithsmallestweightandincludeitinT. If selected edge creates a cycle, then it will be removed from T. We repeat these two steps untilthetreeTcontainsn1edges(wherenisthenumberofverticesinthegraph) If the tree contains less than n1 edges and the edge list is empty, then no spanning tree is possibleforthegraph

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 69

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITV

CSE

MinimumcostspanningtreeusingKruskalsalgorithm


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 70

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITV

CSE

ShortestpathAlgorithm:
Aminimumspanningtreegivesnoindicationabouttheshortestpathbetweentwovertices. Rather only the overall cost or weight is minimized. In real life, we are required to find the shortestpathbetweentwocities. For example: one would be interested in finding most economical route between any two citiesinagivenrailwaysnetwork. WearegivenadirectedgraphGinwhicheveryedgehasaweightandourproblemistofind a path from one vertex v to another vertex w such that the sum of the weights on the path isasminimalaspossible.Weshallcallsuchpathashortestpath.

The shortest path from vertex A to vertex E is ADCE and has a total cost of 12 compared to the cost of 20 for theedge directly from A to E and the cost of 14 forthe path ABCE It turns out that is just easy to solve the more general problem of starting at one vertex, calledthesource,andfindingtheshortestpathtoeveryothervertex,insteadoftojustone destination vertex. For simplicity, we take the source to be vertex A and our problem then consistsoffindingtheshortestpathfromvertexAtoeveryothervertexinthegraph.

DijkstrasAlgorithm:
Thesolutionwe willshowfortheshortestpathproblemiscalledDijkstrasAlgorithm,after Edsger Dijkstra, who first described it in 1959. This algorithm is based on the adjacency matrix representation of a graph. Somewhat surprisingly, it finds not only the shortest path from one specified vertex to another, but the shortest paths from the specified vertex to all theothervertices. DijkstrasAlgorithmisoftencalledasgreedytechnique.
Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 71

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITV

CSE

The algorithm works by maintaining a set S of vertices whose shortest distance from the sourceisalreadyknown.

Whered[i]containsthelengthofthecurrentshortestpathfromsourcevertextovertexi LetsapplyDijkstrasalgorithmtothegivendigraphforgettingstagesofalgorithm

Narasaraopeta Engineering College


www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 72

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITV

CSE


Narasaraopeta Engineering College
www.jntukfastupdates.com LIKE Us www.fb.com/jntukinfo

Page 73

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITV

CSE

Exerciseproblems:
1. Constructaminimumcostspanningtreeofagivengraphbyusingprimsalgorithm andkruskalsalgorithm

2. Find the minimum cost spanning tree for the below graphs using prims and kruskalsalgorithms

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 74

LIKE Us www.fb.com/jntukinfo

Jntuk Fast Updates

ADVANCEDDATASTRUCTURES

UNITV

CSE

3. Find the shortest path from A to all other vertices using Dijkstras algorithm for the graphasshownbelow

4. Find the shortest path by using Dijkstras algorithm on the following edge weighted directedgraphwiththevertexPasthesource

Narasaraopeta Engineering College


www.jntukfastupdates.com

Page 75

LIKE Us www.fb.com/jntukinfo

Vous aimerez peut-être aussi