0% found this document useful (0 votes)
55 views3 pages

Graph Part3

The document discusses algorithms for finding all-pairs shortest paths and counting walks in a graph. It describes Floyd-Warshall's algorithm for finding shortest paths between all pairs of vertices in O(n^3) time using dynamic programming. It also explains how to use matrix multiplication to compute the number of walks between pairs of vertices for different path lengths.

Uploaded by

mlheese
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views3 pages

Graph Part3

The document discusses algorithms for finding all-pairs shortest paths and counting walks in a graph. It describes Floyd-Warshall's algorithm for finding shortest paths between all pairs of vertices in O(n^3) time using dynamic programming. It also explains how to use matrix multiplication to compute the number of walks between pairs of vertices for different path lengths.

Uploaded by

mlheese
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CPSC 490

Graph Theory: Finding all-pairs shortest paths and counting walks

All-pairs shorte s t path with Floyd- Warshall


Someteimes,wehaveagraphandwanttofindtheshortestpathfromutovforallpairsofvertices (u,v).Notethatwealreadyknowhowtodothisin O n3 timewecanrunthequadraticversion ofDijkstra'salgorithmfromeachofthenvertices.However,thereisa4linealgorithmthatwilldothe samejob. Example1:FloydWarshall
intgraph[128][128],n; //aweightedgraphanditssize voidfloydWarshall(){ for(intk=0;k<n;k++) for(inti=0;i<n;i++) for(intj=0;j<n;j++) graph[i][j]=min(graph[i][j],graph[i][k]+graph[k][j]); } intmain{ //initializethegraph[][]adjacencymatrixandn //graph[i][i]shouldbezeroforalli. //graph[i][j]shouldbe"infinity"ifedge(i,j)doesnotexist //otherwise,graph[i][j]istheweightoftheedge(i,j) floydWarshall(); //nowgraph[i][j]isthelengthoftheshortestpathfromitoj }

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

Graph Theory: Finding all-pairs shortest paths and counting walks

Byinduction,theloopinvariantholdsduringeachiterationoftheouterloop.Hence,aftertheouter loophasfinished,graph[i][j]containsthelengthoftheshortestpathfromitojthatisallowedtouse allexistingverticesfrom0ton1.ThisshowsthecorrectnessofFloydWarshall. Whatifwehaveanunweightedgraphandaresimplyinterestedinthequestion,"Isthereapathfrom itoj?"WecanuseFloydWarshalltosolvethisquestioneasily. Example2:GraphreachabilitywithFloydWarshall


boolgraph[128][128],n; intreachability(){ for(intk=0;k<n;k++) for(inti=0;i<n;i++) for(intj=0;j<n;j++) graph[i][j]=graph[i][j]||(graph[i][k]&&graph[k][j]); }

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

Graph Theory: Finding all-pairs shortest paths and counting walks

parent[i][j]containsavertexnumber.Themeaningofparent[i][j]isthis:"Forsomeshortestpath fromitoj,thevertexrightbeforejisparent[i][j]."Weinitializeallentriesparent[i][j]toiwhenwe haveanedge,andifnotwesetitto1.Theupdateprocessoftheparentarraybasicallymeanthe following:"Ifgoingfromitojthroughk(i k j)isanimprovement,thenwesettheparentofjin thenewshortestpathtotheparentofthepathk j."Therestofthecodeisthesameasbefore.To recoverthepathfromitoj,simplyrecurseonparent[i][j]'sentries.

Pow er s of the adjace n c y matrix


Supposethatwehaveanunweightedgraphrepresentedasanadjacencymatrixofzeroesandones.It isasquarematrix,sowecanmultiplyitbyitself.Whatwillthatgiveus?Matrixmultiplicationworks likethis:ifwehavenbynmatricesAandB,thenC=ABiscomputedbythefollowingalgorithm. Example4:Matrixmultiplication
for(inti=0;i<n;i++) for(intj=0;j<n;j++){ C[i][j]=0; for(intk=0;k<n;k++) C[i][j]+=A[i][k]*B[k][j]; }

IfbothAandBareouradjacencymatrixM,thenCisthesquareofM,anditsentriesare

C[ i][ j]= M[ i][ k]M[ k][ j] .


k

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy