All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2

Slides:



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

Chapter 9: Graphs Shortest Paths
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
CS138A Single Source Shortest Paths Peter Schröder.
1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
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.
Lecture 21: Matrix Operations and All-pair Shortest Paths Shang-Hua Teng.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Warshall’s and Floyd’sAlgorithm Dr. Ying Lu RAIK 283: Data Structures.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
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.
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.
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.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.
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 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).
All-Pairs Shortest Paths
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
Shortest Paths1 C B A E D F
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
More Dynamic Programming Floyd-Warshall Algorithm.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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).
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.
Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths.
Introduction to Algorithms Jiafen Liu Sept
Parallel Programming: All-Pairs Shortest Path CS599 David Monismith Based upon notes from multiple sources.
Shortest Path Graph Theory Basics Anil Kishore.
Algorithms LECTURE 14 Shortest Paths II Bellman-Ford algorithm
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.
All-Pairs Shortest Paths
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
1 Chapter 6 : Graph – Part 2 교수 : 이상환 강의실 : 113,118 호, 324 호 연구실 : 과학관 204 호 Home :
Shortest Path Problems
CS330 Discussion 6.
All-Pairs Shortest Paths (26.0/25)
Chapter 25: All-Pairs Shortest Paths
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
CS200: Algorithm Analysis
Analysis and design of algorithm
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
Advanced Algorithms Analysis and Design
All pairs shortest path problem
Negative-Weight edges:
Chapter 9: Graphs Shortest Paths
All-Pairs Shortest Paths
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2

All-pairs shortest paths Given a weighted directed graph, G(V, E) with a weight function w that maps edges to real-valued weights. w(u,v) denote the weight of an edge (u,v) For every pair of vertices u, v, find a shortest path from u to v, where the weight of a path is the sum of the weights of the edges along the path. Run Dijkstra’s algorithm V times? O(V E log V) if no negative weights

All-pairs shortest paths If we allow negative edge weights but no negative-weight cycles Cannot use Dijkstra’ algorithm Run Bellman-Ford (the one we did not cover) once for each vertex, which leads to a running time of for a dense graph.

All-pairs shortest paths Instead, we will use a dynamic programming solution: Floyd-Warshall Algorithm Running time: Let denote the weight of the shortest path from u to v

Input: adjacency matrix representing G Assume an adjacency matrix representation Assume vertices are numbered 1,2,…,n The input is a n x n matrix representing the edge weights of an n-vertex directed graph

Output? n x n matrix Entry will contain the weight of the shortest path from vertex i to vertex j, that is

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

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

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

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

DP Formulation Intermediate vertices: on a path any vertex other than the first and the last vertices is called and intermediate vertex. Let vertices of G be V={1,2,….,n} Consider a subset {1,2,…,k} of these vertices for some k Subproblems: limit the set of intermediate vertices on a path from i to j to subset {1,2,…,k} The path is allowed to pass through only vertices 1 through k

DP Formulation In the original problem: For any pair of vertices i and j, the intermediate vertices could be drawn from the set {1,…,n} Define to be the shortest path from i to j such that intermediate vertices on the path are drawn from the set {1,2,…,k} Matrix

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

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

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

DP Formulation How to reduce to smaller problems? i.e. how to compute assuming we have already computed ?

DP Formulation How to reduce to smaller problems? i.e. how to compute assuming we have already computed ? Two cases: Case 1: Vertex k is NOT among the intermediate vertices on the shortest path from i to j

DP Formulation Case 2: Vertex k is an intermediate vertex on the shortest path from i to j First take the shortest path from i to k using intermediate vertices from the set {1,2,…,k-1} Then take the shortest path from k to j using intermediate vertices from the set {1,2,…,k-1} k j i

DP Formulation Base case: Recursive definition (for k > 0)

Floyd-Warshall Algorithm Floyd-Warshall(W) { n = rows[W] for k=1 to n do for i=1 to n do for j=1 to n do return }

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

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

Example 2 1 5 3 4 8 -4 6 -5 7 1 2 3 4 5 3 8 4 -4 ∞ 1 7 5 11 2 -5 -2 6 1 2 3 4 5

Example 2 1 5 3 4 8 -4 6 -5 7 1 2 3 4 5 3 8 4 -4 ∞ 1 7 5 11 2 -1 -5 -2 6 1 2 3 4 5

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

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

Space Requirement Do we need n matrices, each of n x n? One n x n matrix for D is enough! In phase k, it is okay to overwrite the D from the previous phase (k-1) Why?

Floyd-Warshall(W) { n = rows[W] for i=1 to n do for j = 1 to n do D[i,j] = W[i,j] for k=1 to n do for j=1 to n do if (D[i,k]+D[k,j] < D[i,j]) D[i,j]=D[i,k}+D[k,j] return D }

Homework: How to extract the path? How should we modify the algorithm to store additional information which then can be used to extract the path?