BreadthfirstSearch(BFS)101
GivenavertexS,whatisthedistanceofalltheotherverticesfromS?
Whywouldthisbeuseful?Tofindthefastestroutethroughanetwork.
TodoaBFSkeepadistanceforeachvertex.
1.
Sisthefirstvertexitis0distancefromitself.Setalltheotherverticestoinfinity.
2.
BeginatS.Lookatitsimmediateneighbors.TheirdistanceforSis1.
3.
Nowmovetotheseimmediateneighborsandmarkthedistancebetweenthese
neighborsasone.
4.
Dothisforeachvertexinthegraph.
EachsuccessiveadvancementverticesbeingexaminediscalledaFrontier.
StartwithagraphG.
Setallthedistancestoinfinity
Setthesourcedistanceto0
DeterminethefrontierFofS
Lookateachvertexinthefrontier
Examineeachneighborwofeach
vertex
Ifthedistanceofthatneighboris
infinity,ithasnotbeenvisited.
Ifithasnotbeenvisitedthe
neighborsdistanceisthevertexs
distance+1
Dothisforallverticesinthesearch.
Whatdoesthisalgorithmcost?Bycostwemeanrunningtime.
1.
Thefrontieronlygrowswhenanunvisitedverticesisinserted.Whichmeansthefrontier
canonlybeaslargeasthenumberofvertices.
2.
Eachedgeisvisitedatmostonce,ifthegraphisdirected.
3.
Ifthegraphisundirected,eachedgewillbevisitedatmosttwice.
Thismeanstheruntimeislinearandthesizeofthegraph
BFSQuizNotethisisadirectedgraph.
WhenlookingforneighborsofSlookonlyat
thoseverticesthatgoINTOS,notoutofit.For
example,disaneighborofSandfisNOT.
AnalysisIsBFSInherentlySequential?
Canthisalgorithmbeparallelized?
Thereisabottleneckintherelationshipbetweenextractingv,insertingw.
TheavailableparallelismisW/Dwhichisdependentupon
|E|/|V|.
Isthisagoodthingornot?
Inreallife,graphsaresparse.So|E|=O(|V|).Thismeansthe
averageavailableparallelismwillbeaconstantandthatisbad.
Thisleadstotheconclusionthatthesequentialalgorithmisbad.
IntuitionWhywemightdobetter
TheBFSvisitsthegraphinwaves.Therearetwoimportantimplicationsfromthisfact:
1.
Theupperboundonthespanshouldbethenumberofwaves,notthenumberof
vertices.Thesewavesarecalledlevels.Alevelisalloftheverticesequidistanttothe
source.
Thediameterofagraphisthemaximumshortestdistancebetweenanypairofvertices.
Diameterisapropertyofthewholegraph,andisanupperboundonthenumberoflevels
ofanystartingvertex.
Levelsynchronoustransversalisvisitingthenodeslevelbylevel.
2.
IfyouperformalevelsynchronousBFS,thentheorderinwhichyouvisiteachvertexofa
givenfrontierisimmaterial.Thismeanswecanvisitthematthesametime.
Sothisisanopportunityforparallelismalltheverticesofafrontiercanbevisitedatthe
sametime.
HighLevelApproachtoParallelBFS
1.
2.
CarryouttheBFSlevelbylevel(notvertexbyvertex)
Processtheentirelevelinparallelwecandothisbecausetheorderinwhichthe
verticesarevisitedshouldnotmatter.
Thisisverysimilartotheserialversion,withafew
exceptions.
l0..thecodeislevelsynchronous.
Thisisthelevelcountersetto0
Thefrontiersreferencedarealsolevelspecific(Fl)
ll+1.incrementsthecounter
Thespan(definedbythewhileloop)willbeno
largerthanthediameterofthegraph
Processlevelwill:takethegraphandthecurrent
frontier.Itwillproduceanewfrontierandupdatethe
distances(D)
Bag:KeyProperties
ThedatastructuretobeusedwiththeparallelBFSisabag.
Abaghasthefollowingdataproperties:
Thedataisanunorderedcollection
Itwillallowrepetition
Allowredundantinsertionsofthesamevertex,ifnecessary
Abaghasthefollowingoperationalproperties:
Fasttraversal
Fastandlogicallyassociativeunionandsplit
LogicallyassociativemeansifAUB==BUA
ThiswillalsogivetheabilitytoapplyReducerHyperObjects.
Pennant,BuildingBlocksforBags
Pennantis:atreewith2knodesandaunaryroot
havingachild.thechildistherootofacompletebinary
tree.
Xistheroot
Xsisthecompletebinarytree
Iftherearetwopennants,andtheyareEXACTLYTHE
SAMESIZE(|X|==|Y|)
Thenthetwopennantscanbe
combined.
1.
Chooseoneoftherootstobe
therootofthecombinedpennant.
2.
Thetwopennantsarenow
childrenoftheroot.
Thecombinationisalsoapennant.
Therewillnowbe2k+1nodes.
Thiswasfairlyeasy,itinvolvedrearrangingpointers.
Thereverse,splittingapennantintotwopennantscan
bedoneusingthereverseofthegivenmethod.
CombiningPennantsintoBags
Pennantsaregoodforrepresentingcollectionsthathaveasize2N,butthereneedstobe
somethingforothersizecollections.
Abagneedstocontainanysizecollection.Howcanpennantsbeusedtoformbags?
Anexampleofpennantsusedtoputacollectioninabag.
23items.Dividedintogroupsofpowersof2.(23=10111)
Toconnectthesegroups,useanarrayofpointers,usethenullpointerforanyemptybits.
Thisarrayiscalledaspine.
Toinsertanewelementintothecollection.
1.
TrytoinsertitintotheLSB
2.
IftheLSBisoccupied,combinetheLSBwiththenewelementandcarryit.
3.
MovetothenextLSB,repeattheprocess.
WhatistheCostofInsertion?
Rememberwheninsertinganelement,youmayneedtotraversetheentirespine.
AnintegerofsizeNneedsNbitstostoreitspower.
Insertinganelementissimplyamatterofshufflingpointers.
IttakesthesameasymptotictimetoinsertNelementsintoabagasitdoestoinsert1.
Theamortizedtimetoinsertelementsintoabagisconstant.
BagSplitting
Arightbitshiftisthesameadivisionbytwo.
Tosplitabaganemptybagisalsonecessary.
Splitthepennantinhalf.
Store1ofthehalvesinthesparebagandoneinthenextlowerbitonthebag.
Thecostofsplittingabag..
FinishingtheParallelBFSwithBags
Ifthebagisbigenoughusedivideand
conquerotherwiseusethesequentialmethod.
Thedifferencewiththissequentialmethodisthe
forloopthisusesaparallelforloop.
Thereisadataracehereaseachtasktriesto
updatetheneighbors.Thisisperfectlysafe
here.
Thecostsaretrickyforthisexperiment.
Spanisaffectedbythenumberoflevels.
Spanofprocesslevelhas3parts:depth,costofsplitting,costofthebasecase.