Graph Part3
Graph Part3
Theprettiestwaytoformatthe'for'loopsandthebestnumberofcurlybracestousearehottopicsfor discussionbecausethealgorithmitselfissosimple.First,let'slookattheinitialization.graph[i][j] shouldbesettotheweightoftheedge(i,j).Fornonexistentedges,itshouldbesetto"infinity". However,becauseinthelastlineoffloydWarshall()weaddtogethertwoentriesinthegraph[i][j] matrix, the "infinity" value we pick should not be larger than INT_MAX/2 to avoid overflow. The algorithm itselftakestwoinputstheadjacencymatrix,graph[][], andthenumberofvertices,n. AftertheexecutionoffloydWarshall(),graph[][]ismodifiedtocontainthelengthsoftheshortest pathsbetweenallpairsofvertices.Thisworksforbothdirectedandundirectedgraphs. Theorderofthe3'for'loopsisimportant.Doingthemintheorder"i,j,k"willnotwork.Hereisa simpleproofthattheorder"k,i,j"doeswork.DefineD(k,i,j)tobethelengthoftheshortestpath fromitojthatonlyusesvertices0throughk(inadditiontoverticesiandjthemselves).Forexample, D(3,6,7)isthelengthoftheshortestpathfrom6to7ifweareonlyallowedtostartat6,gothrough vertices0,1,2,and3insomeorderandendupat7.Wemightnotneedtovisitallof0,1,2and3. ToprovecorrectnessofFloydWarshall,wewilldemonstratethefollowingloopinvariant: afterthe kthiterationoftheouterloophasfinished,graph[i][j]containsD(k,i,j)foralliandj.Beforethestart ofthealgorithm,theinvariantistruebecausegraph[i][j]containsthelengthoftheshortestpathfrom itojthatonlyusesverticesiandj,withnointermediatevertices.Supposetheinvariantistrueafterk 1iterations.Thenduringiterationk,graph[i][j]willeithernotchange(iftheshortestpathdoesnot needtousevertexk),oritwillchangetograph[i][k]+graph[k][j]ifitimprovesthedistance.graph [i][k]isthelengthoftheshortestpathformitokthatusesvertices0throughk,andgraph[k][j]is thelengthoftheshortestpathfromktojthatusesvertices0throughk.Hence,graph[i][j]becomes thebestoftwoalternativesitsoldvalue(ifvertexkdoesnothelp)ortheshortestpathfromitokto j(ifvertexkdoeshelp).
CPSC 490
Ifyouareaminimalist, theparenthesesaround"&&"areunnecessary. Afterreachability()returns, graph[i][j]willbetrueifthereexistsapathfromitoj.Wewillcomebacktothereachabilityproblem laterandshowthatitcansolvedmuchfaster,buttheadvantageofthismethodisitsextremebrevity. Nowweknowhowtogetthelengthoftheshortestpaths,butwhatifwewanttorecoverthepath itself?Wecanfigureitoutfromtheresultinggraph[][]matrix.Supposethatweareatiandwewant togotoj.ThereisanedgefromitokoflengthL[i][k].Furthermore,supposethat graph[i][j]=L[i][k]+graph[k][j]. Thisguaranteesthatifwegofromitok,wewillbefollowingashortestpathtoj.Nowwesimplyscan allneighboursofiandfindsuchavertexk,andrecurseonthatvertex. Thismethodrequiresno additionalmemory,butisabitslow. Alternately,wecanstoreaparent[][]arraymuchlikeinDijkstra'sorinBellmanFord. Example3:FloydWarshallwithpathrecovery
constintInf=INT_MAX/21;//graph[i][j]=Infifnoedge intgraph[128][128],n; //aweightedgraphanditssize intparent[128][128]; voidfloydWarshall(){ for(inti=0;i<n;i++) for(intj=0;j<n;j++) if(i==j||graph[i][j]==Inf)parent[i][j]=1; elseparent[i][j]=i; for(intk=0;k<n;k++) for(inti=0;i<n;i++) for(intj=0;j<n;j++){ intnewD=graph[i][k]+graph[k][j]; if(newD<graph[i][j]){ graph[i][j]=newD; parent[i][j]=parent[k][j]; } } }
CPSC 490
IfbothAandBareouradjacencymatrixM,thenCisthesquareofM,anditsentriesare
Ifwethink ofM[u][v]asthenumberofwalks oflength1from utov,thenC[i][j]becomes the numberofwalksoflengthexactly2fromitoj.Thisisbecausewetryallintermediateverticeskand addupthenumberofwalksoflength1fromitoktimesthenumberofwalksoflength1fromktoj. ThisextendstohigherpowersofM.Ingeneral, M p [ i][ j] isthenumberofwalksoflengthexactlyp fromitoj.Notethatthesearewalks,notpaths.Nowhereisitguaranteedthatwearenotusingthe samevertextwice.Wecanextendthisdefinitiontothe0thpowerofM.Whichverticesarereachable fromvertexiviaapathoflength0?Onlyvertexiitself.Hence, M 0 istheidentitymatrix. Powersofadjacencymatrixareuseful,forexample,ifweneedtocomputethesetofverticesreachable fromsomesourcevertexsinexactlypsteps.Todothis,wecancalculate M p andfindallvfor which M p [ s][ v] isnonzero(wehaveatleastonewalkoflengthpfromstov).Inthiscase,itis bettertouseabooleanmatrixandreplaceadditionby"booleanor"(the||operator)whencomputing matrixproducts.Otherwise,evenforrelativelysmallvaluesofp,thenumberofpathscanbesohuge thatitwillcauseoverflow.Considerforinstanceacompletegraph.Itsadjacencymatrixcontainsall ones (except the zeroes on the diagonal). The p'th power of M may contain entries larger than n1p1 . Ittakes O n3 timetomultiplytwomatrices(justlookatexample4).Therefore,computingthep'th power of M usingthe naivemethod requires O pn3 time. If weare only interested in vertices reachableinpstepsfroms,thenweonlyneedtocomputethes'throwof M p .Todothis,wecan takethes'throwoftheidentitymatrix( M 0 )andmultiplyitptimesbyM.Asinglerowisa1byn matrix,soeachmultiplicationwillrequire O n2 timeforatotalof O pn2 .Pleasenotethat therearefastermethodsofmultiplyingmatrices,andindeedmuchresearchhasbeendedicatedto multiplyingmatricesefficiently.However,thesemethodsareoutofscopeforourcourse,andwewill notdiscussthemhere.