1 The Floyd-Warshall Algorithm Andreas Klappenecker.

Slides:



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

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
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.
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.
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.
1 Representing Graphs. 2 Adjacency Matrix Suppose we have a graph G with n nodes. The adjacency matrix is the n x n matrix A=[a ij ] with: a ij = 1 if.
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.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
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:
Midterm 3 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
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).
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
All-Pairs Shortest Paths
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.
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
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.
Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
All-Pairs Shortest Paths & Essential Subgraph 01/25/2005 Jinil Han.
MA/CSSE 473 Day 28 Dynamic Programming Binomial Coefficients Warshall's algorithm Student questions?
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.
Parallel Programming: All-Pairs Shortest Path CS599 David Monismith Based upon notes from multiple sources.
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.
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
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.
Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest.
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.
Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from.
1 Chapter 6 : Graph – Part 2 교수 : 이상환 강의실 : 113,118 호, 324 호 연구실 : 과학관 204 호 Home :
All Pairs Shortest Path Algorithms Aditya Sehgal Amlan Bhattacharya.
All-Pairs SPs on DG Run Dijkstra;s algorithm for each vertex or
CS330 Discussion 6.
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Analysis and design of algorithm
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
CSE 417: Algorithms and Computational Complexity
Negative-Weight edges:
Directed Graphs (Part II)
All-Pairs Shortest Paths
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

1 The Floyd-Warshall Algorithm Andreas Klappenecker

All-Pairs Shortest Path Problem Suppose we are given a directed graph G=(V,E) and a weight function w: E->R. We assume that G does not contain cycles of weight 0 or less. The All-Pairs Shortest Path Problem asks to find the length of the shortest path between any pair of vertices in G. 2

Quick Solutions If the weight function is nonnegative for all edges, then we can use Dijkstra’s single source shortest path algorithm for all vertices to solve the problem. This yields an O(n 3 ) algorithm on graphs with n vertices (on dense graphs and with a simple implementation). 3

Quick Solution For arbitrary weight functions, we can use the Bellman-Ford algorithm applied to all vertices. This yields an O(n 4 ) algorithm for graphs with n vertices. 4

Floyd-Warshall We will now investigate a dynamic programming solution that solved the problem in O(n 3 ) time for a graph with n vertices. This algorithm is known as the Floyd- Warshall algorithm, but it was apparently described earlier by Roy. 5

Representation of the Input We assume that the input is represented by a weight matrix W= (w ij ) i,j in E that is defined by w ij = 0 if i=j w ij = w(i,j) if i  j and (i,j) in E w ij =  if i  j and (i,j) not in E 6

Format of the Output If the graph has n vertices, we return a distance matrix (d ij ), where d ij the length of the path from i to j. 7

Intermediate Vertices Without loss of generality, we will assume that V={1,2,…,n}, i.e., that the vertices of the graph are numbered from 1 to n. Given a path p=(v 1, v 2,…, v m ) in the graph, we will call the vertices v k with index k in {2,…,m-1} the intermediate vertices of p. 8

Key Definition The key to the Floyd-Warshall algorithm is the following definition: Let d ij (k) denote the length of the shortest path from i to j such that all intermediate vertices are contained in the set {1,…,k}. 9

Remark 1 A shortest path does not contain any vertex twice, as this would imply that the path contains a cycle. By assumption, cycles in the graph have a positive weight, so removing the cycle would result in a shorter path, which is impossible. 10

Remark 2 Consider a shortest path p from i to j such that the intermediate vertices are from the set {1,…,k}. If the vertex k is not an intermediate vertex on p, then d ij (k) = d ij (k-1) If the vertex k is an intermediate vertex on p, then d ij (k) = d ik (k-1) + d kj (k-1) Interestingly, in either case, the subpaths contain merely nodes from {1,…,k-1}. 11

Remark 2 Therefore, we can conclude that d ij (k) = min{d ij (k-1), d ik (k-1) + d kj (k-1) } 12

Recursive Formulation If we do not use intermediate nodes, i.e., when k=0, then d ij (0) = w ij If k>0, then d ij (k) = min{d ij (k-1), d ik (k-1) + d kj (k-1) } 13

The Floyd-Warshall Algorithm Floyd-Warshall(W) n = # of rows of W; D (0) = W; for k = 1 to n do for i = 1 to n do for j = 1 to n do d ij (k) = min{d ij (k-1), d ik (k-1) + d kj (k-1) }; od; return D (n) ; 14

Time and Space Requirements The running time is obviously O(n 3 ). However, in this version, the space requirements are high. One can reduce the space from O(n 3 ) to O(n 2 ) by using a single array d. 15