All-Pairs Shortest Paths Given an n-vertex directed weighted graph, find a shortest path from vertex i to vertex j for each of the n 2 vertex pairs (i,j).

Slides:



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

Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis.
Reachability as Transitive Closure
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
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
 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.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Floyd’s Algorithm (shortest-path problem) Section 8.2.
Dynamic Programming Reading Material: Chapter 7..
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
Single-Source All-Destinations Shortest Paths With General Weights Directed weighted graph. Edges may have negative cost. No cycle whose cost is < 0.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Algorithms All pairs shortest path
All-Pairs Shortest Paths
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.
Parallel Programming – Graph Algorithms David Monismith CS599 Notes are primarily based upon Introduction to Parallel Programming, Second Edition by Grama,
Using Dijkstra’s Algorithm to Find a Shortest Path from a to z 1.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
1 Network Optimization Chapter 3 Shortest Path Problems.
Zvi Kohavi and Niraj K. Jha 1 Memory, Definiteness, and Information Losslessness of Finite Automata.
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
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.
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.
Graph Algorithms Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar Adapted for 3030 To accompany the text ``Introduction to Parallel Computing'',
Parallel Programming: All-Pairs Shortest Path CS599 David Monismith Based upon notes from multiple sources.
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 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
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.
IS 2610: Data Structures Graph April 12, Graph Weighted graph – call it networks Shortest path between nodes s and t in a network  Directed simple.
1 Chapter 6 : Graph – Part 2 교수 : 이상환 강의실 : 113,118 호, 324 호 연구실 : 과학관 204 호 Home :
All Pairs Shortest Path Algorithms Aditya Sehgal Amlan Bhattacharya.
Algorithm Analysis Fall 2017 CS 4306/03
Parallel Graph Algorithms
Shortest Path Problems
CS330 Discussion 6.
Shortest Path Graph represents highway system Edges have weights
All-Pairs Shortest Paths (26.0/25)
Single-Source All-Destinations Shortest Paths With Negative Costs
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Single-Source All-Destinations Shortest Paths With Negative Costs
Shortest Path Problems
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Floyd-Warshall Algorithm
Shortest Path Algorithms
Floyd’s Algorithm (shortest-path problem)
Dynamic Programming.
Advanced Algorithms Analysis and Design
All pairs shortest path problem
Single-Source All-Destinations Shortest Paths With Negative Costs
Chapter 24: Single-Source Shortest Paths
Dynamic Programming.
Chapter 24: Single-Source Shortest Paths
Parallel Graph Algorithms
Single-Source All-Destinations Shortest Paths With Negative Costs
Directed Graphs (Part II)
All-Pairs Shortest Paths
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

All-Pairs Shortest Paths Given an n-vertex directed weighted graph, find a shortest path from vertex i to vertex j for each of the n 2 vertex pairs (i,j)

Dijkstra’s Single Source Algorithm Use Dijkstra’s algorithm n times, once with each of the n vertices as the source vertex

Performance Time complexity is O(n 3 ) time. Works only when no edge has a cost < 0.

Floyd’s Algorithm Time complexity is  (n 3 ) time. Works so long as there is no cycle whose length is < 0. When there is a cycle whose length is < 0, some shortest paths aren’t finite.  If vertex 1 is on a cycle whose length is -2, each time you go around this cycle once you get a 1 to 1 path that is 2 units shorter than the previous one. Simpler to code, smaller overheads.

Decision Sequence First decide the highest intermediate vertex (i.e., largest vertex number) on the shortest path from i to j. If the shortest path is i, 2, 6, 3, 8, 5, 7, j the first decision is that vertex 8 is an intermediate vertex on the shortest path and no intermediate vertex is larger than 8. Then decide the highest intermediate vertex on the path from i to 8, and so on. i j

A Triple (i,j,k) denotes the problem of finding the shortest path from vertex i to vertex j that has no intermediate vertex larger than k. (i,j,n) denotes the problem of finding the shortest path from vertex i to vertex j (with no restrictions on intermediate vertices). i j

Cost Function Let c(i,j,k) be the length of a shortest path from vertex i to vertex j that has no intermediate vertex larger than k. i j

c(i,j,n) c(i,j,n) is the length of a shortest path from vertex i to vertex j that has no intermediate vertex larger than n. No vertex is larger than n. Therefore, c(i,j,n) is the length of a shortest path from vertex i to vertex j

c(i,j,0) c(i,j,0) is the length of a shortest path from vertex i to vertex j that has no intermediate vertex larger than 0.  Every vertex is larger than 0.  Therefore, c(i,j,0) is the length of a single-edge path from vertex i to vertex j

Recurrence For c(i,j,k), k > 0 The shortest path from vertex i to vertex j that has no intermediate vertex larger than k may or may not go through vertex k..If this shortest path does not go through vertex k, the largest permissible intermediate vertex is k-1. So the path length is c(i,j,k-1). i j < k

Recurrence For c(i,j,k) ), k > 0 Shortest path goes through vertex k. i j k We may assume that vertex k is not repeated because no cycle has negative length. Largest permissible intermediate vertex on i to k and k to j paths is k-1.

Recurrence For c(i,j,k) ), k > 0 i j k i to k path must be a shortest i to k path that goes through no vertex larger than k-1. If not, replace current i to k path with a shorter i to k path to get an even shorter i to j path.

Recurrence For c(i,j,k) ), k > 0 i j k Similarly, k to j path must be a shortest k to j path that goes through no vertex larger than k-1. Therefore, length of i to k path is c(i,k,k-1), and length of k to j path is c(k,j,k-1). So, c(i,j,k) = c(i,k,k-1) + c(k,j,k-1).

Recurrence For c(i,j,k) ), k > 0 Combining the two equations for c(i,j,k), we get c(i,j,k) = min{c(i,j,k-1), c(i,k,k-1) + c(k,j,k-1)}. We may compute the c(i,j,k)s in the order k = 1, 2, 3, …, n. i j

Floyd’s Shortest Paths Algorithm for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) c(i,j,k) = min{c(i,j,k-1), c(i,k,k-1) + c(k,j,k-1)}; Time complexity is O(n 3 ).  More precisely  (n 3 ).   (n 3 ) space is needed for c(*,*,*).

Space Reduction c(i,j,k) = min{c(i,j,k-1), c(i,k,k-1) + c(k,j,k-1)} When neither i nor j equals k, c(i,j,k-1) is used only in the computation of c(i,j,k). column k row k (i,j) So c(i,j,k) can overwrite c(i,j,k-1).

Space Reduction c(i,j,k) = min{c(i,j,k-1), c(i,k,k-1) + c(k,j,k-1)} When i equals k, c(i,j,k-1) equals c(i,j,k).  c(k,j,k) = min{c(k,j,k-1), c(k,k,k-1) + c(k,j,k-1)} = min{c(k,j,k-1), 0 + c(k,j,k-1)} = c(k,j,k-1) So, when i equals k, c(i,j,k) can overwrite c(i,j,k-1). Similarly when j equals k, c(i,j,k) can overwrite c(i,j,k-1). So, in all cases c(i,j,k) can overwrite c(i,j,k-1).

Floyd’s Shortest Paths Algorithm for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) c(i,j) = min{c(i,j), c(i,k) + c(k,j)};  Initially, c(i,j) = c(i,j,0).  Upon termination, c(i,j) = c(i,j,n).  Time complexity is  (n 3 ).   (n 2 ) space is needed for c(*,*).

Building The Shortest Paths Let kay(i,j) be the largest vertex on the shortest path from i to j. Initially, kay(i,j) = 0 (shortest path has no intermediate vertex). for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (c(i,j) > c(i,k) + c(k,j)) {kay(i,j) = k; c(i,j) = c(i,k) + c(k,j);}

Example Initial Cost Matrix c(*,*) = c(*,*,0)

Final Cost Matrix c(*,*) = c(*,*,n)

kay Matrix

Shortest Path Shortest path from 1 to Path length is 14.

Build A Shortest Path The path is kay(1,7) = kay(1,8) = kay(1,5) =

Build A Shortest Path The path is kay(1,4) = kay(4,5) = kay(4,2) =

Build A Shortest Path The path is kay(2,5) = kay(5,8) = kay(8,7) =

Build A Shortest Path The path is kay(8,6) = kay(6,7) =

Output A Shortest Path void outputPath(int i, int j) {// does not output first vertex (i) on path if (i == j) return; if (kay[i][j] == 0) // no intermediate vertices on path printf("%d ", j); else {// kay[i][j] is an intermediate vertex on the path outputPath(i, kay[i][j]); outputPath(kay[i][j], j); } }

Time Complexity Of outputPath O(number of vertices on shortest path)