Professional Documents
Culture Documents
Lesson1 5TreeComputations PDF
Lesson1 5TreeComputations PDF
Lesson1 5TreeComputations PDF
TreeWarmUp
Atreecanbestoredinanarray.
Step1:numberthenodes
Step2:storetheparentofeachnodeinthearray.Undirectededgesareconvertedtodirected
edges.
Step3:ThearrayiscalledPandhasoneentrypernode.Forexample,Node2pointstoparent
3.Soa3isplacedinarraylocation2.Fillinthearray.Inthetreeexamplebelowyoushould
getthefollowingarray:
Tofindtherootofthetree:
Pickanynode
Followtheparentpointersuntilyoureachtheroot.Therootisanodewithnoparent.
Runningtime=O(n)
Therunningtimeislinearinthenumberofnodes
becauseintheworstcase,thetreeisasinglebranch.
Youmightthenhavetotraversetheentiretreetoget
totheroot.
Howtodothisinparallel?
Explorefromallnodessimultaneously.Ateachnode,changeparenttograndparent.Ifanode
hasnograndparent,itmustbepointingtotheroot.Forexample,nodes2and8donothave
grandparents.
Nodes1,2,5,8allpointtotheroot.Continuewiththerestof
thenodes.Eventuallyallnodeswillpointtotheroot.
Thispseudocodedeterminesifanodehasagrandparent:
Thispseudocoderecordseitherthegrandparent(ifithasone)ortheparentsid.
ThefindRootsalgorithm:
Thisalgorithmdoublebufferstheparentsarray,theouterloopissequentialandtakenoverall
thepossiblelevels,themax.numofwhichislog(n).
findRootsuses:pointerjumping,haspolylogarithmicspan,andworksonaforest,notjusta
tree.Thealgorithmwouldmakeeverynodepointtotherootofitsowntree.Itisnotwork
optimal.
WorkOptimalListScan/PrefixSumPart1
Givenalinklist,computeitsrank.
Dothiswithusingscanorprefixsumnparallelusingpointerjumping,knownasWylliesalgo.
Wyllie'sAlgorithm
Thecostoftheschemeisnotworkoptimal:
W(n)=O(nlog(n))
D(n)=O(log(n))
ThereisatleastonetricktomakeWylliesmoreworkoptimal.
1. Shrinkthelisttosizem<n.
2. RunWyllieonthesmallerlist.O(mlog(m))
3. Restorefulllistandranks
Thequestionnowis.howshouldmbechosentoleadtoworkoptimality?n/log(n)
ParallelIndependentSets,Part1:ARandomizedAlgorithm
Toshrinkalistinparallel,useanindependentset.
TodothisN[i]=issuccessor(next)
Giventhislist,hereisthesuccessorarray.
Anindependentsetis:AsetIofverticessuchthati I N[i]
/ I
(iisanelementofthesetdoesnothaveitssuccessorintheset)
AnindependentsetoftheaboveisI={3,7,8}
Thissetis
not
anindependentset{3,4,6,8}because8isasuccessof6.
Togenerateanindependentsetsequentially,traversethelistandputeveryothernodeinthe
list.Forexampletheindependentsetdoingthisalgorithmwouldbe:I={4,7,3,6}(followthe
linkedlist)
Doingthesamethinginparallelistrickier.
Problemofsymmetry:whenlookingatanindividualnode,allnodeslookalike.
Aschemeisneededtobreakthesymmetry.
Foreachnodeflipacoin.HeadsPr[heads]=Pr[tails]=
Headswillbeincludedintheindependentsetandtailswillbeleftout.
Thereisapossibilitythatanodeanditsneighborarebothheads.
Inthiscasechangetheheadintoatail,soanyheadisadjacenttoatail
Creatingtheparallelindependentset:
ParallelIndependentSet
Whichofthesenodeswillendupintheindependentset.Rememberthetestforheadsmustbe
runsimultaneously.Sonodes5and6willbothseethattheyarepartofadoubleheadpair.
Bothwillchangetotailsaccordingtothealgorithm.
TheWorkandSpanoftheParallelIndependentSets
Whatistheaveragenumberofverticesthatendupintheindependentset?n
Fromthecoinflip,therearefourpossibilities(hh,ht,th,tt)aftertheinitialflip.Thenacorrectis
appliedforthecaseofdoubleheads.Sothepossibilitiesarenow(th,ht,th,tt).Sonowthereis
onepossibilitythefirstnodeofthepairisaheads.
WorkOptimalListScan/Prefixsum,Part2:Details
Recallthetrickforaworkoptimallistrankingalgorithm.
1. Shrinklisttosizem
2. RunWyllie
3. Restorefulllistandranks.
HowtoShrinktheList
Usetheindependentset,jumpingovertheindependentsetelements.
1. beginwiththelistofranks:
2. Assigna0totheheadanda1toalltheothernodes.(4istheheadofthelinkedlist)
3. Nowfindtheindependentset.
Flipthecoinforeachnode,andremovethedoubleheads.
4. Theindependentis{4,1,8}
5. Removetheindependentset.Thismeansremovingthevertices4,1,8andrewiring7to
pointto3.
6. Theindependent
7. Weneedtogettoalistofsizen/log(n).
8. Flipthecoinsanddoagain.
9. Inthisinstance,5and7areintheindependentset.Sotheyneedtoberemoved.
10. Now,supposethelistisnowtherightsize.
11. RunListScanonit.Yougetthefollowingresult:
12. Comparethefoundresultwiththewhattheyshouldbebasedontheoriginallist.They
arethesame.
Thedownloadableshasadetaileddiscussiononhowtodotherestofthelist.
WorkOptimalListScan/PrefixSumPart3
Inadditiontothebookkeeping,howmanytimesdoyouneedtoruntheindependentsetto
shrinkthelisttoO(n/log(n))inlength?O(log(log(n)))
Youexpecttopickn/4nodestostart,solengthafterthefirstParISis..
E[listlengthafter1ParIS]=3/4n
NowrunParISktimes:
k
E[listlengthafterkcalls]=()
n
Thetargetlistlengthisn/log(n)so.
k
E[listlengthafterkcalls]=()
n<n/log(n)
k< (loglogn)
W
=O(nloglog(n))
1
D
=O(log(n)
1
Youwillalsowanttoknowwhatisthelengthwhennotaroundtheaverage.
Youwillalsowanttoknowhowmuchbookkeepingisnecessarytoimplementsteps1and3.
ASeeminglySequentialTreeAlgorithm
Toperformpostordernumberingonatree:
Usethealgorithmonatree:
Startwiththeroot(inthiscase#0).
Traceittoleafnumber4.Sincenode4hasnochildren,itgetsthe
initialvalueof0.
Thenextchildwithoutachild(node5)willgetthenextvalue(0+1).Continuingfortheentire
treeyoushouldget:
PreOrderNumbering
ThetreeispreOrdered:
Thereisaslightambiguityitdoesnotsayinwhatordertovisitthechildren.
Sothisanswerisvalidalso:
EulerTourTechnique
Toviewatreeasalist:
takeeveryundirectededgeandrepresentitasapairofdirectededges.
Ateverynodethenumberofincomingedgesequalsthenumberofoutgoingedges.
ThismakesthisgraphEulerian.
ForeveryEuleriandirectedgraphthereisadirectedcircuit.
Eulercircuit:aclosedpaththatuseseveryedgeonce.
Thiscircuitgivesyoualinkedlist.
TodopostOrdernumbering:
Assigna0totheheadofthelist.
Thenmarkallparenttochildsinkswitha0
.
Fortheremainingnodes,noticetheyareallsinksthatgofromchildrentoparents.This
correspondstothereturnvalue,soputa1onthesenodes.
Nowwhenyoudoascanofthiscircuit,startingattheroot,thevaluesarethepostOrder
numberings.
AsummaryoftheEulerTourTechnique
1. TreeList
2. Parenttochildsink=0
3. ChildtoParentsink=1
4. ChildtoParentSource=postOrder
values
5. Listprefixscan
ThespanofanEulerTour
TheoverallspanoftheEulertourisO(log(n))
ThewholepointoftheEulertouristoturnthetreeintoalistandthenthetreesshapewillnot
matter.
ItisnotalwayspossibletoconvertacomputationintoanequivalentEulercomputation.
ComputingLevels
Thelevel(depth)ofatreenodeistheminimumnumberofedgesfromtherootofthenode.
1. ComputeranEulercircuit
2. Thenconsidereachsink.Puta+1foreachparenttochildpath.
3. Thenputa1foreachchildtoparentpath.
4. ThenrunalistScan
ImplementingEulerTours
Howshouldthetreebestoredandhowshouldthetourbecomputed?
Makeeachundirectededgeapairofdirectededges.
Foreachnode,itsadjacencylistwillbethesetofitsoutgoingneighbors.
Thefulllistoftheadjacencylists:
TocompletetheEulertour:defineaSuccessorFunction
Givenanedgethatgoesfromu
tov,thefunctionreturnstheNEXTneighborinvadjacencylist.
i
Themodfunctionmakesthelistcircular.
Forexamplethesuccessoroftheedge01is14
Whatisthecostofthesuccessorfunction?
Tobeabletoquicklygettothesuccessorfunctionaddcrossedges.
WhatisS(S(S(6,8)))?
S(6,8)returns(8,6)
applythedefinitionagainget(6,1)
applyagainget(1,0)