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

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

Introduction to Algorithms 6.046J/18.401J/SMA5503
Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
Epp, section 10.? CS 202 Aaron Bloomfield
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 13 Instructor: Paul Beame.
Single Source Shortest Paths
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
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.
Shortest Paths Algorithm Design and Analysis Week 7 Bibliography: [CLRS] – chap 24 [CLRS] – chap 25.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
Introduction to Algorithms 6.046J/18.401J/SMA5503
Chapter 25: All-Pairs Shortest-Paths
Discussion #34 1/17 Discussion #34 Warshall’s and Floyd’s Algorithms.
 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
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.
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.
Shortest Paths Definitions Single Source Algorithms
Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and.
All-Pairs Shortest Paths
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
More Dynamic Programming Floyd-Warshall Algorithm.
5/27/03CSE Shortest Paths CSE Algorithms Shortest Paths Problems.
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.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
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
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 Chapter Equivalence, Order, and Inductive Proof.
1 Section 4.1 Properties of Binary Relations A binary relation R over a set A is a subset of A  A. If (x, y)  R we also write x R y. Example. Some sample.
All-Pairs Shortest Paths
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Shortest Paths.
Algorithm Analysis Fall 2017 CS 4306/03
All-Pairs Shortest Paths (26.0/25)
Chapter 25: All-Pairs Shortest Paths
Lecture 7 All-Pairs Shortest Paths
Shortest Paths.
Lecture 7 Shortest Path Shortest-path problems
CS200: Algorithm Analysis
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Analysis and design of algorithm
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Algorithms
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
Honors Track: Competitive Programming & Problem Solving Avoiding negative edges Steven Ge.
All pairs shortest path problem
Algorithms (2IL15) – Lecture 7
Single-Source All-Destinations Shortest Paths With Negative Costs
Shortest Paths.
Lecture 21: Matrix Operations and All-pair Shortest Paths
Single-Source All-Destinations Shortest Paths With Negative Costs
Directed Graphs (Part II)
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

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

2 Shortest paths Single-source shortest paths Nonnegative edge weights Dijkstras algorithm: O(E + V lg V) General Bellman-Ford: O(VE) DAG One pass of Bellman-Ford: O(V + E) All-pairs shortest paths Nonnegative edge weights Dijkstras algorithm |V| times: O(VE + V 2 lg V) General Three algorithms today. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19.

3 All-pairs shortest paths Input: Digraph G = (V, E), where |V| = n, with edge-weight function w : E. Output: n × n matrix of shortest-path lengths (i, j) for all i, j V. IDEA #1: Run Bellman-Ford once from each vertex. Time = O(V 2 E). Dense graph O(V 4 ) time. Good first try! © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19.

4 Dynamic programming Consider the n × n adjacency matrix A = (a ij ) of the digraph, and define d ij (m) = weight of a shortest path from i to j that uses at most m edges. Claim: We have d ij (0) = 0 if i = j, if i j; and for m = 1, 2, …, n – 1, d ij (m) = min k {d ik (m–1) + a kj }. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19.

5 Proof of claim d ij (m) = min k {d ik (m–1) + a kj }. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Relaxation! for k 1 to n do if d ij > d ik + a kj then d ij d ik + a kj Note: No negative-weight cycles implies (i, j) = d ij (n–1) = d ij (n) = d ij (n+1) = … m – 1 edges ksks

6 Matrix multiplication Compute C = A · B, where C, A, and B are n × n matrices Time = (n 3 ) using the standard algorithm. What if we map + min and · +? c ij = min k {a ik + b kj }. Thus, D (m) = D (m–1) × A. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Identity matrix = I == D 0 = (d ij (0) ).

7 Matrix multiplication (continued) © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. The (min, +) multiplication is associative, and with the real numbers, it forms an algebraic structure called a closed semiring. Consequently, we can compute D (1) = D (0) · A = A 1 D (2) = D (1) · A = A 2 D (n–1) = D (n–2) · A = A n–1, yielding D (n–1) = ( (i, j)). Time = (n·n 3 ) = (n 4 ). No better than n × B-F.

8 Improved matrix multiplication algorithm © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Repeated squaring: A 2k = A k × A k. Compute A 2, A 4, …, A 2. O(lg n) squarings Note: A n–1 = A n = A n+1 =. Time = (n 3 lg n). To detect negative-weight cycles, check the diagonal for negative values in O(n) additional time.

9 Floyd-Warshall algorithm © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Also dynamic programming, but faster! Define c ij (k) = weight of a shortest path from i to j with intermediate vertices belonging to the set {1, 2, …, k}. Thus, (i, j) = c ij (n). Also, c ij (0) = a ij.

10 Floyd-Warshall recurrence © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. c ij (k) = min k {c ij (k–1), c ik (k–1) + c kj (k–1) } intermediate vertices in {1, 2, …, k}

11 Pseudocode for Floyd- Warshall for k 1 to n do for i 1 to n do for j 1 to n do if c ij > c ik + c kj then c ij c ik + c kj relaxation Notes: Okay to omit superscripts, since extra relaxations cant hurt. Runs in (n 3 ) time. Simple to code. Efficient in practice. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19.

12 Transitive closure of a directed graph © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Compute t ij = 1 if there exists a path from i to j, 0 otherwise. IDEA: Use Floyd-Warshall, but with (, ) instead of (min, +): t ij (k) = t ij (k–1) (t ik (k–1) t kj (k–1) ). Time = (n 3 ).

13 Graph reweighting © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Theorem. Given a label h(v) for each v V, reweight each edge (u, v) E by ŵ(u, v) = w(u, v) + h(u) – h(v). Then, all paths between the same two vertices are reweighted by the same amount. Proof. Let p = v 1 v 2 v k be a path in the grap Then, we have

14 Johnsons algorithm © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. 1.Find a vertex labeling h such that ŵ(u, v) 0 for all (u, v) E by using Bellman-Ford to solve the difference constraints h(v) – h(u) w(u, v), or determine that a negative-weight cycle exists. Time = O(VE). 2.Run Dijkstras algorithm from each vertex using ŵ. Time = O(VE + V 2 lg V). 3.Reweight each shortest-path length ŵ(p) to produce the shortest-path lengths w(p) of the original graph. Time = O(V 2 ). Total time = O(VE + V 2 lg V).