1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm.

Slides:



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

Reachability as Transitive Closure
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.
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.
1 Review of some graph algorithms Graph G(V,E) (Chapter 22) –Directed, undirected –Representation Adjacency-list, adjacency-matrix Breadth-first search.
Chapter 25: All-Pairs Shortest-Paths
 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.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Warshall’s and Floyd’sAlgorithm Dr. Ying Lu RAIK 283: Data Structures.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
CS420 lecture twelve Shortest Paths wim bohm cs csu.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Dynamic Programming Reading Material: Chapter 7..
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.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
1 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental Algorithms.
Graph Algorithms: Part 1
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.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 16 All shortest paths algorithms Properties of all shortest paths Simple algorithm:
Midterm 3 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Algorithms All pairs shortest path
All-Pairs Shortest Paths
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
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.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Data Structures Using C++ 2E
More Dynamic Programming Floyd-Warshall Algorithm.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 28 Weighted Graph Applications. 2 Objectives F To represent weighted edges using adjacency matrices and priority queues (§28.2). F To model.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 5 Dynamic Programming 2001 년 5 월 24 일 충북대학교 알고리즘연구실.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Graph Algorithms. Definitions and Representation An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite.
Week 4 Single Source Shortest Paths All Pairs Shortest Path Problem.
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
All-Pairs Shortest Paths & Essential Subgraph 01/25/2005 Jinil Han.
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.
Introduction to Algorithms Jiafen Liu Sept
Parallel Programming: All-Pairs Shortest Path CS599 David Monismith Based upon notes from multiple sources.
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.
1 Ch20. Dynamic Programming. 2 BIRD’S-EYE VIEW Dynamic programming The most difficult one of the five design methods Has its foundation in the principle.
All-Pairs Shortest Paths
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Chapter 9: Graphs.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
All Pairs Shortest Path Algorithms Aditya Sehgal Amlan Bhattacharya.
All-Pairs SPs on DG Run Dijkstra;s algorithm for each vertex or
Floyd-Warshall Algorithm
Advanced Algorithms Analysis and Design
All pairs shortest path problem
Negative-Weight edges:
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm

2 Problem definition: Real problem:When to make a table of distances between all pairs of cities for a road atlas,how can we compute the distances? Model the problem: Given a weighted,directed graph G=(V,E) with a weight function w:E R that maps edges to real-valued weights. For every pair of vertices u,v V, find a shortest(least weight) path from u to v, where the weight of a path is the sum of the weights of its constituent edges.

3 Solve the problem by Single- Source shortest paths algorithm: If all edge weights are nonnegative, we can use Dijkstra’s algorithm. If negative-weight edges are allowed, then we use Bellman-Ford algorithm instead. Can you analysis the complexity of the two solutions ? Is it possible for us to find a more efficient algorithm?

4 Preliminary knowledge: In this chapter, we use an adjacency-matrix representation: w ij = if i=j the weight of directed edge (i,j) if i j and (i,j) E if i j and (i,j) E The output of the algorithm presented in this chapter is an n*n matrix D=(d ij ),where entry d ij contains the weight of a shortest path from vertex i to vertex j. That is, if we let denotes the shortest-path weight from vertex i to vertex j, then d ij = at termination.

5 Continue: To solve the all-pairs shortest-paths problem on an input adjacency matrix, we need to compute not only the shortest path weights but also a predecessor matrix = ( ij ), where ij is NIL if either i=j or there is no path from i to j, and otherwise ij is some predecessor of j on a shortest path from i. The subgraph induced by the ith row of the matrix should be a shortest-paths tree with root i. For each vertex i V,we define the predecessor subgraph of G for i as G,i =(V,i,E,i ) V,i ={j V: i,j NIL} {i} E,i ={( ij, j): j V,i and ij NIL}

6 Shortest paths and matrix multiplication: This section presents a dynamic-programming algorithm for the all-pairs shortest-paths problem on a directed graph G=(V,E). Each major loop of the dynamic program will invoke an operation that is very similar to matrix multiplication, so that the algorithm will look like repeated matrix multiplication. We shall start by developing a (V 4 )-time algorithm for the all-pairs shortest-paths problem and the improve its running time to (V 3 lgV).

7 Review: the steps to develop a dynamic-programming algorithm Characterize the structure of an optimal solution. Recursively define the value of an optimal solution. Computing the value of an optimal solution in a bottom-up fashion. Constructing an optimal solution from computed information.

8 The structure of a shortest path: Consider a shortest path p from vertex i to vertex j, and suppose that p contains at most m edges.Assuming that there are no negative-weight cycles,m is finite.If i=j,then p has weight 0 and no edges.If vertices i and j are distinct, then we decompose path p into i k j,where path p’ now contains at most m-1 edges.Moreover, p’ is a shortest path from i to k. Thus, we have +w kj.

9 A recursive solution to the all- pairs shortest-paths problem: Now,let d ij (m) be the minimum weight of any path from vertex i to vertex j that contains at most m edges.When m=0,there is a shortest path from i to j with no edges if and only if i=j.For m>=1,we compute d ij (m) as the minimum of d ij (m-1) (the weight of the shortest path from i to j consisting of at most m-1 edges) and the minimum weight of any path from i to j consisting of at most m edges,obtained by looking at all possible predecessors k of j.Thus,we recursively define: d ij (m) =min ( d ij (m-1), {d ik (m-1) +w kj })= {d ik (m-1) +w kj }.

10 Continue: If the graph contains no negative-weight cycles,then all shortest paths are simple and thus contain at most n-1 edges.A path from vertex i to vertex j with more than n-1 edges cannot have less than a shortest path from i to j.The actual shortest-path weights are therefore given by =d ij (n-1) =d ij (n) =d ij (n+1) =...

11 Computing the shortest-path weights bottom up: We compute the shortest-path weights by extending shortest paths edge by edge.Letting A*B denote the matrix “product” returned by EXTEND-SHORTEST- PATH(A,B).We compute the sequence of n-1 matrices: D (1) =D (0) *W=W, D (2) =D (1) *W=W 2, … D (n-1) =D (n-2) *W=W n-1. As we argued above, the matrix D (n-1) =W n-1 contains the shortest-path weights.So we get the algorithm.

12 Continue: SLOW-ALL-PAIRS-SHORTEST-PATH(W) n rows[W] D (1) W for m 2 to n-1 do D (m) EXTEND-SHORTEST-PATHS(D (m-1),W) return D (n-1)

13 Continue: EXTEND-SHORTEST-PATH(D,W) n rows[D] let D’=(d ij ’) be a n*n matrix for i 1 to n do for j 1 to n do d ij ’ for k 1 to n do d ij ’ min(d ij ’,d ik +w kj ) return D’

14 Example: Figure 1 6

15 D (1) =D (2) = D (3) = D (4) =

16 Improving the running time: Our goal,however, is not to compute all the D (m) matrices: we are interested in matrix D (n-1).Recall that in the absence of negative-weight cycles, D (m) =D (n-1),for all integers m>=n-1.So, we can compute D (n-1) with only matrix products by computing the sequence: D (1) =W, D (2) =W 2 =W*W D (4) = W 2 * W 2 … D (2 ) =W 2 =W 2 -1 * W 2 -1

17 Continue: FASTER-ALL-PAIRS-SHORTEST-PATHS(W) n rows[W] D (1) W while n-1>m do D (2m) EXTEND-SHORTEST-PATHS(D (m),D (m) ) m 2m return D (m)

18 Floyd-Warshall algorithm: In this section, we shall use a different dynamic- programming formulation to solve the all-pairs shortest- paths problem on a directed graph G=(V,E).The resulting algorithm, known as the Floyd-Warshall algorithm, runs in (V 3 ) time. As before, negative-weight edges may be present, but we shall assume that there are no negative- weight cycles.

19 The structure of a shortest path: In the Floyd-Warshall algorithm, we use a different characterization of the structure of a shortest path than we used in the matrix-multiplication-based all-pairs algorithms.The algorithm considers the “intermediate” vertices of a shortest path, where an intermediate vertex of a simple path p= is any vertex of p other than v 1 or v l, that is, any vertex in the set {v 2,v 3,…,v l-1 }

20 Continue: Let the vertices of G be V={1,2,…,n}, and consider a subset {1,2,…,k} of vertices for some k.For any pair of vertices i,j V,consider all paths from i to j whose intermediate vertices are all drawn from {1,2,…,k},and let p be a minimum-weight path from among them.The Floyd- Warshall algorithm exploits a relationship between path p and shortest paths from i to j with all intermediate vertices in the set {1,2,…,k-1}.

21 Continue: The relationship depends on whether or not k is an intermediate vertex of path p. If k is not an intermediate vertex of path p, then all intermediate vertices of path p are in the set {1,2,…,k-1}. Thus, a shortest path from vertex i to vertex j with all intermediate vertices in the set {1,2,…,k-1} also a shortest path from i to j with all intermediate vertices in the set {1,2,…,k}. If k is an intermediate vertex of path p,then we break p down into i k j as shown Figure 2.p1 is a shortest path from i to k with all intermediate vertices in the set {1,2,…,k-1}, so as p2.

22 i j k p1 p2 All intermediate vertices in {1,2,…,k-1} P:all intermediate vertices in {1,2,…,k} Figure2 Path p is a shortest path from vertex i to vertex j,and k is the highest-numbered intermediate vertex of p. Path p1, the portion of path p from vertex i to vertex k,has all intermediate vertices in the set {1,2,…,k-1}.The same holds for path p2 from vertex k to vertex j.

23 A recursive solution to the all- pairs shortest paths problem: Let d ij (k) be the weight of a shortest path from vertex i to vertex j with all intermediate vertices in the set {1,2,…,k}. A recursive definition is given by d ij (k) = w ij if k=0, min(d ij (k-1),d ik (k-1) +d kj (k-1) ) if k 1. The matrix D (n) =(d ij (n) ) gives the final answer-d ij (n) = for all i,j V-because all intermediate vertices are in the set {1,2,…,n}.

24 Computing the shortest-path weights bottom up: FLOYD-WARSHALL(W) n rows[W] D (0) W for k 1 to n do for i 1 to n do for j 1 to n d ij (k) min(d ij (k-1),d ik (k-1) +d kj (k-1) ) return D (n)

25 Example: Figure 3 6

26 D (0) = (0) = D (1) = (1) =

27 D (2) = (2) = D (3) = (3) =

28 D (4) = (4) = D (5) = (5) =

29 The End: