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.

Slides:



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

* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
8.3 Representing Relations Connection Matrices Let R be a relation from A = {a 1, a 2,..., a m } to B = {b 1, b 2,..., b n }. Definition: A n m  n connection.
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.
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.
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
Chapter 25: All-Pairs Shortest-Paths
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
 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.
Lecture 21: Matrix Operations and All-pair Shortest Paths Shang-Hua Teng.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
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.
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.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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:
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 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
Dynamic Programming – Part 2 Introduction to Algorithms Dynamic Programming – Part 2 CSE 680 Prof. Roger Crawfis.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
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).
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.
Algorithms LECTURE 14 Shortest Paths II Bellman-Ford algorithm
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.
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.
All-Pairs Shortest Paths
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
CSC317 Shortest path algorithms
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
All-Pairs SPs on DG Run Dijkstra;s algorithm for each vertex or
All-Pairs Shortest Paths (26.0/25)
Chapter 25: All-Pairs Shortest Paths
CS200: Algorithm Analysis
Analysis and design of algorithm
Floyd-Warshall Algorithm
Shortest Path Algorithms
Dynamic Programming.
Advanced Algorithms Analysis and Design
All pairs shortest path problem
Dynamic Programming.
Lecture 21: Matrix Operations and All-pair Shortest Paths
All Pairs Shortest Path Examples While the illustrations which follow only show solutions from vertex A (or 1) for simplicity, students should note that.
Directed Graphs (Part II)
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

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 pair of vertices in V Can we do this with algorithms we’ve already seen? All-Pairs Shortest Paths (Ch. 25) Solution 1: run Dijkstra’s algorithm V times, once with each v  V as the source node (requires no negative-weight edges in E) If G is dense with an array implementation of Q O(V  V 2 ) = O (V 3 ) time If G is sparse with a binary heap implementation of Q O(V  ((V + E) logV)) = O(V 2 logV + VElogV) time

All-Pairs Shortest Paths Solution 2: run the Bellman-Ford algorithm V times (negative edge weights allowed), once from each vertex. O(V 2 E), which on a dense graph is O(V 4 ) Solution 3: Use an algorithm designed for the APSP problem. E.g., Warshall's and Floyd's Algorithms introduces a dynamic programming technique that uses adjacency matrix representation of G = (V, E)

Warshall's Transitive Closure Algorithm The transitive closure of a directed graph with n vertices (nodes) can be defined as the n-by-n boolean matrix T={t ij }, in which the element in the i th row and the j th column is 1 if there exists a nontrivial directed path from the i th node to the j th node; otherwise t ij is 0. A non-trivial directed path is a directed path of a positive length. The boolean matrix representing the graph has 1 in its i th row and j th column iff there is a directed edge from i to j A =

Warshall's Transitive Closure Algorithm Input: Adjacency matrix A of G as matrix of 1s and 0's Output: Transitive Closure or reachability matrix R (n) of G Solution for R (n) : Define r ij (k) as the element in the ith row and jth column to be 1 iff there is a path between vertices i and j using only vertices numbered ≤ k. R (0) = A, original adjacency matrix (1's are direct outgoing edges) R (n) the matrix we want to compute R (k) ’s elements are: R (k) [i, j] = r ij (k) = r ij (k-1)  r ik (k-1)  r kj (k-1) Assumes vertices are numbered 1 to |V|, |V| = n and there are no edge weights. Finds a series of boolean matrices R (0), …, R (n)

Warshall's Algorithm R (0) = A = Matrix R (0) contains the nodes reachable in one hop To create R (1), we use R (0). There is a 1 in row 3, col 1 and row 1, col 2, (meaning there is a directed edge from 3 to 1 and from 1 to 2) so put a 1 in position 3,2. Also, there is a 1 in row 3, col 1, and row 1, col 3, (meaning there is a directed edge from 3 to 1 and from 1 to 3) so put a 1 in position 3,3

Warshall's Algorithm R (1) = Matrix R (1) contains the nodes reachable in one hop or in 2 hops on paths that go through vertex 1. For R (2), there is no change because 1 can get to 3 through 2 but there is already a direct path between 1 and 3.

Warshall's Algorithm R (2) = Matrix R (2) contains the nodes reachable in one hop or in multiple hops on paths that go through vertices 1 or 2. For R (3), there is a 1 in row 1, col 3 and row 3, col 1, so put a 1 in position 1,1. Also, there is a 1 in row 2, col 3 and row 3, col 1, so put a 1 in position 2,1. Also, there is a 1 in row 2, col 3 and row 3, col 2, so put a 1 in position 2,2.

Warshall's Algorithm R (3) = Matrix R (3) contains the vertices reachable in one hop or on multi-hop paths that go through vertices 1, 2, and 3.

Warshall's Algorithm Input: Adjacency matrix A of graph G Output: The transitive closure of the digraph Warshall (A[1…n,1…n]) 1. R (0) = A 2.for k = 1 to n 3. for i = 1 to n 4. for j = 1 to n 5. R ij (k) = R ij (k-1) V R ik (k-1)  R kj (k-1) 6. return R (n) If an element r ij is 1 in R (k-1), it remains 1 in R(k). If an element r ij is 0 in R (k-1), it becomes a 1 iff the element in row i and column k and the element in row k, column j are both 1's in R (k-1).

Floyd's APSP Algorithm Input: Adjacency matrix A Output: Shortest path matrix D (n) and predecessor matrix  (n) Relies on the Optimal Substructure Property: All sub-paths of a shortest path are shortest paths. Observation: When G contains no negative-weight cycles, all shortest paths consist of at most n – 1 edges Solution for D: (distance matrix) Define D (k) [i, j] = d ij (k) as the minimum weight of any path from vertex i to vertex j, such that all intermediate vertices are in {1, 2, 3,..., k} D (0) = A, original adjacency matrix (only paths are single edges) D (n) the matrix we want to compute D (k) ’s elements are: D (k) [i, j] = d ij (k) = min(d ij (k-1), d ik (k-1) + d kj (k-1) ) Assumes vertices are numbered 1 to |V|

Recursive Solution for D (k) jik d ij (k-1) d ik (k-1) d kj (k-1) The only intermediate nodes on the paths from i to j, i to k, or k to j are in the set of vertices {1, 2, 3,..., k-1}. If k is included in shortest i to j path, then a shortest path has been found that includes k. If k is not included in shortest i to j path, then the shortest path still only includes vertices in the set 1…k-1. D (k) [i, j] = d ij (k) = min(d ij (k-1), d ik (k-1) + d kj (k-1) )

Floyd's APSP Algorithm Use adjacency matrix A for G = (V, E): w(i, j)if (i,j)  E A[i, j] = a ij = 0 if i = j  if i ≠ j and (i, j)  E   08  9   04  6 

Floyd's APSP Algorithm Use adjacency matrix  to keep track of predecessors: π (0) ij = i if i ≠ j and w(i,j) <  Øif i = j or w(i, j) =  Ø 11 ØØ ØØ 2 Ø 2 ØØØ 33 4 ØØØ 4 Ø 5 Ø 5 Ø π ij is predecessor of j on some shortest path from i

Floyd's APSP Algorithm Input: Distance matrix D of graph G with no negative- weight cycles Output: Distance matrix of shortest paths and predecessor matrix Floyd-APSP(D[1…n, 1…n]) 1. for k = 1 to n 2. for i = 1 to n 3. for j = 1 to n 4. if d ij (k) > d ik (k-1) + d kj (k-1) 5. d ij (k) = d ik (k-1) + d kj (k-1) 6. π ij (k) = π kj (k-1) 7. else π ij (k) = π ij (k-1) 8. return D (n)

Operation of F-APSP Algorithm  0 D (0) = A =  11 2  2 3  ∏ (0) = 71

Operation of F-APSP Algorithm  11 2  2 31  ∏ (1) = D (1) = 6 2

Operation of F-APSP Algorithm  12 2  2 31  ∏ (2) = D (2) = 5 3

Operation of F-APSP Algorithm  12 2  2 31  ∏ (3) = D (3) Print-APSP( , i, j) 1. if i = j 2. print i 3. else if π ij = NIL 4. print "no path from " i " to " j " exists" 5. else Print-APSP( , i, π ij ) 6. print j

Operation of F-APSP Algorithm  0 D (0) = A = D (1) D (2) 2  3  D (3) 3  1  2 1  2  3

F-APSP Algorithm   0  71  40   06 2  -5  0 D (0) = A =   0  71  40   D (1) = 5  1  2, 5  1 

 0  71   D (2) = 1  2  5 F-APSP Algorithm  2  4, 3  2  5

 0  71   D (3) = F-APSP Algorithm 5  3 

 0  71   D (4) = F-APSP Algorithm 1  4 

D (5) = F-APSP Algorithm 1  4  5  3, 1  4  5  3  2 2  5  1, 2  5  3, 2  5  1  4 3  2  5  1, 3  2  5  1  4 4  5  1, 4  5  3  2, 4  5 

Running Time of Floyd's-APSP Lines 3 – 6: |V 3 | time for triply-nested for loops Overall running time = =  (V 3 ) The code is tight, with no elaborate data structures and so the constant hidden in the  -notation is small.