Chapter 25: All-Pairs Shortest-Paths

Slides:



Advertisements
Similar presentations
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
CS138A Single Source Shortest Paths Peter Schröder.
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm.
Shortest Paths Algorithm Design and Analysis Week 7 Bibliography: [CLRS] – chap 24 [CLRS] – chap 25.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
CS420 lecture twelve Shortest Paths wim bohm cs csu.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental Algorithms.
CSE Shortest Paths1 Midterm page 1 The worst-case complexity of an algorithm is... a function from N (or size of instance) to N.  (n lg n) is...
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.
Graph Algorithms Shortest path problems [Adapted from K.Wayne]
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Shortest Paths Definitions Single Source Algorithms
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 16 All shortest paths algorithms Properties of all shortest paths Simple algorithm:
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
All-Pairs Shortest Paths
Lecture 8 Shortest Path Shortest-path problems
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
1 Dynamic programming algorithms for all-pairs shortest path and longest common subsequences We will study a new technique—dynamic programming algorithms.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
Topological Sorting and Least-cost Path Algorithms.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
5/27/03CSE Shortest Paths CSE Algorithms Shortest Paths Problems.
Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
All-pairs Shortest Paths. p2. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i.
Minimum weight spanning trees
Introduction to Algorithms Jiafen Liu Sept
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Lecture 13 Algorithm Analysis
All-Pairs Shortest Paths
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Shortest paths Given: A single source vertex (given s) in a weighted, directed graph. Want to compute a shortest path for each possible destination. (Single.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
Algorithm Analysis Fall 2017 CS 4306/03
Minimum Spanning Tree Shortest Paths
All-Pairs Shortest Paths (26.0/25)
Chapter 25: All-Pairs Shortest Paths
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Floyd-Warshall Algorithm
Lecture 13 Algorithm Analysis
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
All pairs shortest path problem
Graph Algorithms: Shortest Path
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Chapter 25: All-Pairs Shortest-Paths

Some Algorithms When no negative edges Using Dijkstra’s algorithm: O(V3) Using Binary heap implementation: O(VE lg V) Using Fibonacci heap: O(VE + V2log V) When no negative cycles Floyd-Warshall [1962]: O(V3) time When negative cycles Using Bellman-Ford algorithm: O(V2 E) = O(V4 ) Johnson [1977]: O(VE + V2log V) time based on a clever combination of Bellman-Ford and Dijkstra

Notations G = (V, E) with w: E -> R Input form: matrix W= (wij ) wij =0 if i = j , wij = the weight of the directed edge if i≠j and (i, j) є E, Otherwise wij = ∞ Output: D = (dij), dij = δ(i,j) the shortest weight path from i to j

A dynamic programming approach 1. characterize the structure of an optimal solution, 2. recursively define the value of an optimal solution, 3. compute the value of an optimal solution in a bottom-up fashion.

The structure of an optimal solution Consider a shortest path p from vertex i to vertex j, and suppose that p contains at most m edges. Assume that there are no negative-weight cycles. Hence m ≤ n-1 is finite.

The structure of an optimal solution If i = j, then p has weight 0 and no edge. If i≠j, we decompose p into i ~ k -> j where p’ contains at most m-1 edges. Moreover, p’ is a shortest path from i to k and δ(i,j) = δ(i,k) + wkj

Recursive solution to the all-pairs shortest-path problem Define: lij(m) = minimum weight of any path from i to j that contains at most m edges. 0 if i = j lij(0) = ∞ if i ≠ j Then lij(m) = min{ lij(m-1), min1≤k≤n {lik(m-1) + wkj}} = min1≤k≤n {lik(m-1) + wkj} (why?)

Recursive solution to the all-pairs shortest-path problem Since shortest path from i to j contains at most n-1 edges, δ(i,j) = lij(n-1) = lij(n) = lij(n+1) = … Computing the shortest-path weight bottom up: Compute L(1) ,L(2) ,…., L(n-1) where L(m)=(lij (m)) Note that L(1) = W.

EXTENDED-SHORTEST-PATHS(L, W) Given matrices L(m-1) and W return L(m) n <- L.row Let L’ = (l’ij) be a new n x n matrix for i = 1 to n for j = 1 to n l’ij = ∞ for k = 1 to n l’ij = min(l’ij, lik + wkj) return L’

Example: W = 1 4 5 2 3 -4 7 6 -5 8

Matrix Multiplication Let l(m-1) -> a w -> b l(m) -> c min -> + + -> ‧ time complexity : O(n3) Cij = k=1 to n aik‧bkj lij(m) = min1≤k≤n {lik(m-1) + wkj}

SLOW-ALL-PAIRS-SHORTEST-PATHS(W) L(1) = L(0) ‧ W = W L(2) = L(1) ‧ W = W2 L(3) = L(2) ‧ W = W3 ‧ L(n-1) = L(n-2) ‧W = Wn-1

SLOW-ALL-PAIRS-SHORTEST-PATHS(W) 1 n = W.rows 2 L(1) = W for m = 2 to n-1 let L(m) be a new n x n matrix L(m) = EXTENDED-SHORTEST- PATHS( L(m-1), W ) return L(n-1) Time complexity : O(n4)

Improving the running time L(1) = W L(2) = W2 =W.W L(4) =W4 = W2.W2 . i.e., using repeating squaring! Time complexity: O(n3lg n)

FASTER-ALL-PAIRS-SHORTEST-PATHS FASTER-ALL-PAIRS-SHORTEST-PATHS(W) 1. n =W.row 2. L(1) =W 3. m =1 4. while m < n-1 5. let L(2m) be a new n x n matrix 6. L(2m) = Extend-Shorest-Path(L(m), L(m)) 7. m = 2m 8. return L(m)

The Floyd-Warshall algorithm A different dynamic programming formulation ‧The structure of a shortest path: Let V(G)={1,2,…,n}. For any pair of vertices i, j єV, consider all paths from i to j whose intermediate vertices are drawn from {1, 2,…,k}, and let p be a minimum weight path among them.

The structure of a shortest path If k is not in p, then all intermediate vertices are in {1, 2,…,k-1}. If k is an intermediate vertex of p, then p can be decomposed into i ~ k ~ j where p1 is a shortest path from i to k with all the intermediate vertices in {1,2,…,k-1} and p2 is a shortest path from k to j with all the intermediate vertices in {1,2,…,k-1}.

A recursive solution to the all-pairs shortest path Let dijk =the weight of a shortest path from vertex i to vertex j with all intermediate vertices in the set {1,2,…,k}. dij(k) = wij if k = 0 = min(dij(k-1), dik(k-1) + dkj(k-1)) if k ≥ 1 D(n) = (dij(n)) if the final solution!

FLOYD-WARSHALL(W) 1. n = W.rows 2. D(0)= W 3. for k = 1 to n 4. Let D(k) = (dij(k)) be a new n x n matrix 5. for i = 1 to n 6. for j = 1 to n 7. dij(k) = min (dij(k-1), dik(k-1) + dkj(k-1)) 8. return D(n) Complexity: O(n3)

Constructing a shortest path πij(k) : is the predecessor of the vertex j on a shortest path from vertex i with all intermediate vertices in the set {1,2,…,k}. πij(0) = NIL if i=j or wij = ∞ = i if i ≠j and wij < ∞ πij(k) = πij(k-1) if dij(k-1) ≤ dik(k-1) + dkj(k-1) = πkj(k-1) if dij(k-1) > dik(k-1) + dkj(k-1)

Example

Transitive closure of a directed graph Given a directed graph G = (V, E) with V = {1,2,…, n} The transitive closure of G is G*= (V, E*) where E*={(i, j)| there is a path from i to j in G}. Modify FLOYD-WARSHALL algorithm: tij(0) = 0 if i≠j and (i,j) ∉ E 1 if i=j or (i,j) є E for k ≥ 1 tij(k) = tij(k-1) (tik(k-1) tkj(k-1))

TRANSITIVE-CLOSURE(G) n = |G.V| Let T(0) = (tij(0)) be a new n x n matrix 3 for i = 1 to n 4 for j =1 to n 5 if i == j or (i, j) ∈ G.E 6 tij(0) = 1 7 else tij(0) = 0 for k =1 to n Let T(k) = (tij(k)) be a new n x n matrix for i = 1 to n 11 for j =1 to n 12 tij(k) = tij(k-1) (tik(k-1)  tkj(k-1)) 123 return T(n) Time complexity: O(n3)

Example ① ② ④ ③

Some Algorithms When no negative edges Using Dijkstra’s algorithm: O(V3) Using Binary heap implementation: O(VE lg V) Using Fibonacci heap: O(VE + V2log V) When no negative cycles Floyd-Warshall [1962]: O(V3) time When negative cycles Using Bellman-Ford algorithm: O(V2 E) = O(V4 ) Johnson [1977]: O(VE + V2log V) time based on a clever combination of Bellman-Ford and Dijkstra

Johnson’s algorithm for sparse graphs If all edge weights in G are nonnegative, we can find all shortest paths in O(V2lg V + VE) by using Dijkstra’s algorithm with Fibonacci heap Bellman-Ford algorithm takes O(VE) Using reweighting technique

Reweighting technique If G has negative-weighted edge, we compute a new set of nonnegative weight that allows us to use the same method. The new set of edge weight ŵ satisfies: 1. For all pairs of vertices u, v єV, a shortest path from u to v using weight function w is also a shortest path from u to v using the weight function ŵ 2. ∀(u,v) є E(G), ŵ(u,v) is nonnegative

Lemma: (Reweighting doesn’t change shortest paths) Given a weighted directed graph G = (V, E) with weight function w:E→R , let h:V →R be any function mapping vertices to real numbers. For each edge (u,v) є E, ŵ(u,v) = w(u,v) + h(u) – h(v) Let P=<v0,v1,…,vk> be a path from vertex v0 to vk Then w(p) = δ(v0,vk) if and only if ŵ(p) = (v0,vk) Also, G has a negative-weight cycle using weight function w iff G has a negative weight cycle using weight function ŵ. 

Proof = (w(vi-1 ,vi) + h(vi-1)-h(vi)) = w(vi-1 ,vi) + h(v0)-h(vk) ŵ(p) = w(p) + h(v0) – h(vk) ŵ(p) = ŵ(vi-1 ,vi) = (w(vi-1 ,vi) + h(vi-1)-h(vi)) = w(vi-1 ,vi) + h(v0)-h(vk) = w(p) + h(v0) – h(vk)

Proof Because h(v0) and h(vk) do not depend on the path, if one path from v0 to vk is shorter than another using weight function w, then it is also shorter using ŵ. Thus, w(p) = δ(v0,vk) if and only if ŵ(p) = (v0,vk)

Proof G has a negative-weight cycle using w iff G has a negative-weight cycle using ŵ.  Consider any cycle C=<v0,v1,…,vk> with v0=vk . Then ŵ(C) = w(C) + h(v0) – h(vk) = w(C) .

Producing nonnegative weight by reweighting Given a weighted directed graph G = (V, E) We make a new graph G’= (V’,E’), V’ = V ∪ {s}, E’ = E ∪{(s,v): v є V} and w(s,v) = 0, for all v in V Let h(v) = δ(s, v) for all v V’ We have h(v) ≤ h(u) + w(u, v) (why?) ŵ(u, v) = w(u, v) + h(u) – h(v) ≥ 0

Example: -4 1 7 2 6 -5 8 3 4 5

-4 1 7 2 6 -5 8 3 4 -1 5 S

ŵ(u, v) = w(u, v) + h(u) – h(v) h(v) = δ(s, v) ŵ(u, v) = w(u, v) + h(u) – h(v) 10 2 13 4 -1 -5 -4 3 5 1 S

JOHNSON algorithm 1 Computing G’, where G’.V = G.V ∪ {s} and G’.E= G.E ∪{(s, v): v є G.V} and w(s, v) = 0. 2 if BELLMAN-FORD(G’, w, s)= FALSE 3 print “the input graph contains negative weight cycle” 4 else for each vertex v є G’.V 5 set h(v) to be the value of δ(s, v) computed by the BF algorithm 6 for each edge (u, v) є G’.E, ŵ(u, v) = w(u, v) + h(u) – h(v)

JOHNSON algorithm 7 Let D = (duv) be a new n x n matrix 8 for each vertex u є G.V run DIJKSTRA (G, ŵ, u) to compute (u, v) for all v є V[G] . 10 for each vertex v є G.V 11 duv = (u, v) + h(v) – h(u) 12 return D Complexity: O(V2lgV + VE)

10 2 13 4 3 5 1 2/1 2/-3 2/2 0/-4 0/0 10 2 13 4 3 5 1 0/0 2/3 0/-4 0/1 2/-1 10 2 13 4 3 5 1 0/0 0/4 2/7 2/3 0/5

10 2 13 4 3 5 1 2/2 0/-1 0/-5 0/0 2/-2 10 2 13 4 3 5 1 2/5 4/8 0/0 2/1 2/6

Homework Practice at home: Exercises: 25.1-5, 25.1-6, 25.1-7 Exercises: 25.2-8 (Due: Jan. 4) Practice at home : 25.3-3 Exercises: 25.3-5 (Due: Jan. 4)

Quiz 5 Please show the ordering of the following vertices produced by Topological Sort Algorithm under-shorts pants belt shirt tie socks shoes jacket