All-Pairs Shortest Paths (26.0/25)

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
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.
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
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
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
CS420 lecture twelve Shortest Paths wim bohm cs csu.
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.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Shortest Paths Definitions Single Source Algorithms
Algorithms All pairs shortest path
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 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
More Dynamic Programming Floyd-Warshall Algorithm.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Graph (II) Shortest path, Minimum spanning tree GGuy
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
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.
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.
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.
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.
All-Pairs Shortest Paths
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
1 Chapter 6 : Graph – Part 2 교수 : 이상환 강의실 : 113,118 호, 324 호 연구실 : 과학관 204 호 Home :
Shortest Paths.
Shortest Paths C B A E D F Shortest Paths
Shortest Paths and Minimum Spanning Trees
Lecture 13 Shortest Path.
Algorithm Analysis Fall 2017 CS 4306/03
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Chapter 25: All-Pairs Shortest Paths
Warshall’s and Floyd’sAlgorithm
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Shortest Paths C B A E D F Shortest Paths
Shortest Paths.
Lecture 7 Shortest Path Shortest-path problems
CS200: Algorithm Analysis
CSCE 411 Design and Analysis of Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
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
Shortest Paths and Minimum Spanning Trees
Floyd’s Algorithm (shortest-path problem)
CSCE 411 Design and Analysis of Algorithms
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Advanced Algorithms Analysis and Design
All pairs shortest path problem
Algorithms (2IL15) – Lecture 7
Algorithms: Design and Analysis
Presented by-Kapil Kumar Cse-iii rd year
Shortest Paths.
Lecture 21: Matrix Operations and All-pair Shortest Paths
Negative-Weight edges:
Single-Source Shortest Path & Minimum Spanning Trees
Directed Graphs (Part II)
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

All-Pairs Shortest Paths (26.0/25) HW: problem 26-1, p. 576/25-1, p. 641 Directed graph G = (V,E), weight E   Goal: Create n  n matrix of s-p distances (u,v) Running Bellman-Ford once from each vertex O( ) = O( ) on dense graphs Adjacency-matrix representation of graph: n  n matrix W = (wij) of edge weights assume wii = 0 i, s-p to self has no edges in absence of negative cycles

Dynamic Programming (26.1/25.1) dij(m) = weight of s-p from i to j with  m edges dij(0) = 0 if i = j and dij(0) =  if i  j dij(m) = mink{dik(m-1) + wkj} Runtime = O( n4) because n-1 passes each computing n2 d’s in O(n) time  m-1 j i  m-1

Matrix Multiplication (26.1/25.1) Similar: C = A  B, two n  n matrices cij = k aik  bkj O(n3) operations replacing: ‘‘ + ’’  ‘‘ min ’’ ‘‘  ’’  ‘‘ + ’’ gives cij= mink {aik + bkj} D(m) = D(m-1) ‘‘ ’’ W identity matrix is D(0) Cannot use Strassen’s because no subtraction Time is still O(n  n3 ) = O(n4 ) Repeated squaring: W2n = Wn  Wn Compute W, W2 , W4 ,..., W2k , k= log n, O(n3 log n)

Floyd-Warshall Algorithm (26.2/25.2) Also dynamic programming but faster (by log n) cij(m) = weight of s-p from i to j with intermediate vertices in the set {1, 2, ..., m}  (i, j)= cij(n) DP: compute cij(n) in terms of smaller cij(n-1) cij(0) = wij cij(m) = min {cij(m) , cim(m-1) + cmj(m-1) } intermediate nodes in {1, 2, ..., m} m cmj(m-1) cim(m-1) j i cij(m-1)

Floyd-Warshall Algorithm (26.2/25.2) Difference from previous: we do not check all possible intermediate vertices. Code: for m=1..n do for i=1..n do for j = 1..n do cij(m) = min {cij(m-1) , cim(m-1) + cmj(m-1) } Runtime O(n3 ) Transitive Closure G* of graph G: (i,j)  G* iff  path from i to j in G Adjacency matrix, elements on {0,1} Floyd-Warshall with ‘‘ min ’’  ‘‘OR’’ , ‘‘+’’  ‘‘ AND ’’ Useful in many problems

Johnson’s Algorithm (26.3/25.3) Johnson’s = Bellman-Ford + Dijkstra’s (or Thorup’s) Re-weighting of the graph G=(V,E,w): assign weights to all vertices h: V   change weight of edges w’(u,v) = w(u,v) + h(u) - h(v) then for any u,v: ’(u, v) = (u,v) + h(u) - h(v) Addition of auxiliary vertex s: V  V + s E  V + {(s,v), v  V} , w(s,v) = 0 Run Bellman-Ford: find h(v) = (s,v) (or negat. cycle) w’(u,v)  0 since h(v)  h(u) + w(u,v) and w’(u,v) = w(u,v) + h(u) - h(v)  0

Johnson’s Algorithm (26.3/25.3) Since all weights are nonnegative, apply Dijkstra’s for each source and find all modified distances ’(u, v) Restore actual distances from modified using (u, v) = ’(u,v) - h(u) + h(v) Runtime: O(VE) = V times O(E) - M.Thorup’s algorithm O(V(E+V log V)) = V times Dijkstra’s with Fibonacci heaps O(VE log V) = V times Dijkstra’s with binary heaps (faster for sparse graphs) To find s-p for a single pair = s-p from single source = all pairs shortest paths.