Lesson2 6DistributedBFS PDF
Lesson2 6DistributedBFS PDF
GraphsandAdjacencyMatrices
Itishelpfultolookatagraphasanadjacencymatrix.
Todothis:
1. Giveeachvertexanintegerlabel.
2. Thencreateamatrix,A,torepresentthegraph.Arowforeveryedge,andacolumnfor
everyvertex.
3. Foreachedge,puta1inthecorrespondingrow,column.Forthevertex0,ithastwo
edgesattachedtoit.Oneoftheedgesisgoingtovertex1andtheothertovertex5.
4. Undirectedgraphshavesymmetricmatrix.Soputa1inthecorrespondingcolumns.
5. Thefinalgraph
ForanyundirectedgraphG,
Assume:
n=numberofvertices
m=numberofedges
Itsadjacencymatrixwillbenxn.
Thenumberofnonzeros:nnz(A)=2m(#ofnonzeros)
TheAdjacencyMatrixforaDIRECTEDGraph
Arbitrarilynumberthegraph.
ThenrecordtheOUTGOINGedgesofeachvertex.
Nowtreatthematrixasabooleanmatrix:
1=True,0=False
Whatistheundirectedgraphforthisdirected
graph?
ThelogicalorofBanditstranspose.
BreadthFirstSearch:Review
First:calculatetheminimumdistanceofeachvertexfroms.
Letvertex0bes.
sis0distancefromitself,allotherverticesareinfinitedistancefroms.
Forlevel=0,theFrontierF
={0}.Foreachlevel,itisjustthedistanceoftheverticesfroms.
l
Sonowweneedtovisitallofthefrontiersneighbors,inthiscasetheneighborsofthefrontier
arevertices1and5.Theirdistancesareupdatedwiththecurrentdistance+1.(inthiscase0+
1=1)
Thesevisitedneighborsnowbecomethenewfrontier.Thelevelis1,soupdateallneighborsto
distances1+1=2.
Repeatforallnodes.
Thefinalresult:
Thecostofthealgorithmis:O(m+n)n=vertices,m=edges
MatrixbasedBFS
NowtranslatetheBFSalgorithmintothelanguageofMatrices.
Isthereanedgefromthefrontiertoi?Ifthereis,thedistance
shouldbeupdated.
Ifthereisanedgefromji,thentheadjacencymatrix
shouldhavea1(ortrue)ata
.
ji
ConsiderthegraphasamatrixAandthefrontierasa
booleanvectorf.
Todetermineifthereisanedgefromitothefrontier,thereneedstobeaniatthe
correspondingvertices.Torecordtheseedges,marktheminabooleanvectorcalledu(for
update).
Thiscanbesaidas:
Updateiifanyvertexjisinthefrontierandpointstoi.
(Vislogicalorandtheupcarrotisalogicaland)
Forasparsegraph,thevectorandthematrixcanbemaintainedwithsparsedatastructures.
togofromtheupdatevectortotheupdateddistances,
MATRIXBASEDBFSTHEQUIZ
Theexampleoffillingintheupdatevector.Use
thethematrixvectorproduct.
1DDistributedBFS
ThematrixgivesyouaneasywayfordistributingtheBFS.
Todothis:
Dividethenumberofcolumnsintoequalsectionsforeachprocessor.Thecolumnpartitioning
correspondstothepartitioningofthevertices.
Theupdatevectorwillbepartitioned
toeachprocess,butthefrontier
vectorwillneedtobereplicatedon
eachprocess.
Witheachupdate,youwillneedto
replicatethefrontieragain.
Usealltoall
TheDistributed1DAlgorithm
1. partitioncolumnsofAandentriesofu.
T
2. ComputeruA
f
3. Locallyupdatethelocaldistances
4. Identifylocalverticesofthenextfrontier
5. Toanalltoallexchangeofthefrontier
Thealltoallistheonlycommunicationstep,whatisthecost?
The1Dcostsscalelinearly.Whatisthecostwhenitis2D?Squarerootofp(pisthenumber
ofprocessors).