# all-pairs shortest paths

Post on 02-Jan-2016

29 views

Embed Size (px)

DESCRIPTION

All-Pairs Shortest Paths. Find the distance between every pair of vertices in a weighted graph G. From A one may reach C faster by reaching by traveling from A to B and then from B to C. All-Pairs Shortest Paths. We can make n calls to Dijkstra’s algorithm O(n(n+m)log n) time. - PowerPoint PPT PresentationTRANSCRIPT

All-Pairs Shortest PathsFind the distance between every pair of vertices in a weighted graph G.

All-Pairs Shortest PathsWe can make n calls to Dijkstras algorithm O(n(n+m)log n) timeAlternatively ... Use dynamic programming: The Floyds AlgorithmO(n3) time.

Floyds Shortest Path AlgorithmIdea #1: Number the vertices 1, 2, , n.Idea #2: Shortest path between vertex i and vertex j without passing through any other vertex is weight(edge(i,j)).Let it be D0(i,j).Idea #3: If Dk(i,j) is the shortest path between vertex i and vertex j using vertices numbered 1, 2, , k, as intermediate vertices, then Dn(i,j) is the solution to the shortest path problem between vertex i and vertex j.

Floyds Shortest Path AlgorithmAssume that we have Dk-1(i,j) for all i, and j.We wish to compute Dk(i,j).What are the possibilities? Choose between a path through vertex k. Path Length: Dk(i,j) = Dk-1(i,k) + Dk-1(k,j). (2) Skip vertex k altogether. Path Length: Dk(i,j) = Dk-1(i,j).

Floyds Shortest Path AlgorithmkjiUses only verticesnumbered 1,,k-1Uses only verticesnumbered 1,,k-1Uses only vertices numbered 1,,k(compute weight of this edge)

Floyds Shortest Path AlgorithmExample:4135211182438D0

083180423401111082180

Floyds Shortest Path AlgorithmExample:4135211182438D1

08318049234011191082180

Floyds Shortest Path AlgorithmExample:4135211182438D2

083110804923401119108102180

Floyds Shortest Path AlgorithmExample:4135211182438D3

0731470452340111510242120

Floyds Shortest Path AlgorithmExample:4135211182438D4

0621360452240111510232120

Floyds Shortest Path AlgorithmExample:4135211182438D5

0521350342230111410232120

Floyds Shortest Path AlgorithmAlgorithm AllPair(G) {assumes vertices 1,,n} for all vertex pairs (i,j) if i = jd0[i,i] 0else if (i,j) is an edge in Gd0[i,j] weight of edge (i,j)else d0[i,j] + for k 1 to n do for i 1 to n do for j 1 to n do dk[i,j] min(dk-1[i,j], dk-1[i,k]+dk-1[k,j])O(n+m)orO(n2)

O(n3)

Floyds Shortest Path AlgorithmObservation: dk [i,k] = dk-1[i,k] dk[k,j] = dk-1[k,j]

Floyds Shortest Path AlgorithmAlgorithm AllPair(G) {assumes vertices 1,,n} for all vertex pairs (i,j) if i = jd[i,i] 0else if (i,j) is an edge in Gd[i,j] weight of edge (i,j)else d[i,j] + for k 1 to n do for i 1 to n do for j 1 to n do d[i,j] min(d[i,j], d[i,k]+d[k,j])

Floyds Shortest Path AlgorithmAlgorithm AllPair(G) {assumes vertices 1,,n} for all vertex pairs (i,j) path[i,j] nullif i = jd[i,i] 0else if (i,j) is an edge in Gd[i,j] weight of edge (i,j)else d[i,j] + for k 1 to n do for i 1 to n do for j 1 to n do if (d[i,k]+d[k,j] < d[i,j]) path[i,j] vertex k d[i,j] d[i,k]+d[k,j]

Floyds Shortest Path AlgorithmCan be applied to Directed GraphsCan accept ve edges as long as there are no ve cycles.

Transitive ClosureTransitive Relation: A relation R on a set A is called transitive if and only if for any a, b, and c in A, whenever R , and R , R

Transitive ClosureGiven a graph G, the transitive closure of G is the G* such thatG* has the same vertices as Gif G has a path from u to v (u v), G* has a edge from u to vBADCEGG*The transitive closure providesreachability information about a graph.

Computing the Transitive ClosureWe can perform BFS starting at each vertexO(n(n+m))Alternatively ... Use dynamic programming: Warshalls Algorithm

Floyd-Warshall Transitive ClosureIdea #1: Number the vertices 1, 2, , n.Idea #2: Consider paths that use only vertices numbered 1, 2, , k, as intermediate vertices:kjiUses only verticesnumbered 1,,k-1Uses only verticesnumbered 1,,k-1Uses only vertices numbered 1,,k(add this edge if its not already in)

Warshalls Transitive ClosureExample:4132A0

0101101001011010

Warshalls Transitive ClosureExample:4132A1

0101101101011110

Warshalls Transitive ClosureExample:4132A2

0111101111011110

Warshalls Transitive ClosureExample:4132A3

0111101111011110

Warshalls Transitive ClosureExample:4132A4

0111101111011110

Warshalls AlgorithmWarshalls algorithm numbers the vertices of G as v1 , , vn and computes a series of graphs G0, , GnG0=G Gk has a edge (vi, vj) if G has a path from vi to vj with intermediate vertices in the set {v1 , , vk} We have that Gn = G*In phase k, graph Gk is computed from Gk - 1Running time: O(n3), assuming areAdjacent is O(1) (e.g., adjacency matrix)Algorithm FloydWarshall(G)Input graph GOutput transitive closure G* of Gi 1for all v G.vertices()denote v as vii i + 1G0 Gfor k 1 to n doGk Gk - 1 for i 1 to n (i k) dofor j 1 to n (j i, k) doif Gk - 1.areAdjacent(vi, vk) Gk - 1.areAdjacent(vk, vj) if Gk.areAdjacent(vi, vj) Gk.insertEdge(vi, vj , k)return Gn

Shortest Path Algorithm for DAGsUse Dijkstras algorithmTime: O((n+m) log n)Use Topological sort based algorithmTime: O(n+m).

Directed Graph With ve weightORDPVDMCODFWSFOLAXLGAHNL$170-$100$277-$200-$100$210$224$246$68$511$28$2610

Directed Graph With ve weightORD4PVD3MCO1DFW5SFO8LAX6LGA2HNL7$170-$100$277-$200-$100$210$224$246$68$511$28$2610

Directed Graph With ve weightORD4PVD3MCO1DFW5SFO8LAX6LGA2HNL7$170-$100$277-$200-$100$210$224$246$68$511$28$2610$210$224$224

Directed Graph With ve weightORD4PVD3MCO1DFW5SFO8LAX6LGA2HNL7$170-$100$277-$200-$100$210$224$246$68$511$28$2610$210$224$261

Directed Graph With ve weightORD4PVD3MCO1DFW5SFO8LAX6LGA2HNL7$170-$100$277-$200-$100$210$224$246$68$511$28$2610$210$224$238

Directed Graph With ve weightORD4PVD3MCO1DFW5SFO8LAX6LGA2HNL7$170-$100$277-$200-$100$210$224$246$68$511$28$2610$210$224$238$408

Directed Graph With ve weightORD4PVD3MCO1DFW5SFO8LAX6LGA2HNL7$170-$100$277-$200-$100$210$224$246$68$511$28$2610$210$224$238$408$208$308

Directed Graph With ve weightORD4PVD3MCO1DFW5SFO8LAX6LGA2HNL7$170-$100$277-$200-$100$210$224$246$68$511$28$2610$210$224$238$408$208$308

Directed Graph With ve weightORD4PVD3MCO1DFW5SFO8LAX6LGA2HNL7$170-$100$277-$200-$100$210$224$246$68$511$28$2610$210$224$238$408$208$268$719

Directed Graph With ve weightORD4PVD3MCO1DFW5SFO8LAX6LGA2HNL7$170-$100$277-$200-$100$210$224$246$68$511$28$2610$210$224$238$408$208$268$719

Directed Graph With ve weightORD4PVD3MCO1DFW5SFO8LAX6LGA2HNL7$170-$100$277-$200-$100$210$224$246$68$511$28$2610$210$224$238$408$208$268$719

DAG-based AlgorithmWorks even with negative-weight edgesUses topological orderDoesnt use any fancy data structuresIs much faster than Dijkstras algorithmRunning time: O(n+m).Algorithm DagDistances(G, s)for all v G.vertices()v.parent nullif v = sv.distance 0else v.distance Perform a topological sort of the verticesfor u 1 to n do {in topological order}for each e G.outEdges(u)w G.opposite(u,e)r getDistance(u) + weight(e)if r < getDistance(w)w.distance r w.parent u