All pairs shortest path problem

Slides:



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

Fibonacci Numbers F n = F n-1 + F n-2 F 0 =0, F 1 =1 – 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 … Straightforward recursive procedure is slow! Why? How slow? Lets.
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.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis.
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.
Chapter 25: All-Pairs Shortest-Paths
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
 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 Processing & Analysis of Geometric Shapes Shortest path problems Shortest path problems The discrete way © Alexander & Michael Bronstein, ©
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
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.
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.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
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
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
All-Pairs Shortest Paths
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.
1 Numerical geometry of non-rigid shapes Shortest path problems Shortest path problems Lecture 2 © Alexander & Michael Bronstein tosca.cs.technion.ac.il/book.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
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
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.
All-Pairs Shortest Paths
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Algorithm Analysis Fall 2017 CS 4306/03
All-Pairs SPs on DG Run Dijkstra;s algorithm for each vertex or
Algorithms and Data Structures Lecture XIII
Shortest Path Problems
Shortest Path Problems
CS330 Discussion 6.
All-Pairs Shortest Paths (26.0/25)
Chapter 25: All-Pairs Shortest Paths
Lecture 7 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.
Unit-5 Dynamic Programming
CS200: Algorithm Analysis
Lecture 6 Shortest Path Problem.
Analysis and design of algorithm
Chapter 13 Graph Algorithms
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
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
Lecture 13 Algorithm Analysis
Dynamic Programming.
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Presented by-Kapil Kumar Cse-iii rd year
Shortest Path Problems
Dynamic Programming.
Graph Algorithms: Shortest Path
Negative-Weight edges:
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

All pairs shortest path problem

Motivation Solve an all-pairs shortest-paths problem by running a single-source shortest-paths algorithm |V| times, once for each vertex as the source. Dijkstra's algorithm linear-array implementation of the min-priority queue The binary min-heap implementation of the min-priority queue implement the min-priority queue with a Fibonacci heap A better method to find shortest path?

Assumption Use an adjacency-matrix representation input is an n × n matrix W representing the edge weights of an n-vertex directed graph G = (V, E). output of the all-pairs shortest-paths algorithms presented in this chapter is an n × n matrix D = (dij), where entry dij contains the weight of a shortest path from vertex i to vertex j.

Dynamic-programming algorithm based on matrix multiplication The Floyd-Warshall algorithm

Shortest paths and matrix multiplication Dynamic-programming algorithm 1. Characterize the structure of an optimal solution. 2. Recursively define the value of an optimal solution. 3. Compute the value of an optimal solution in a bottom-up fashion. 4. Constructing an optimal solution from computed information

Observation of the structure of a shortest path All subpaths of a shortest path are shortest paths (Lemma 24.1) Consider a shortest path p from vertex i to vertex j, and suppose that p contains at most m edges. If vertices i and j are distinct, then we decompose path p into , where path p′ now contains at most m-1 edges. By Lemma 24.1, p′ is a shortest path from i to k, and so

A recursive solution to the all-pairs shortest-paths problem Let be the minimum weight of any path from vertex i to vertex j that contains at most m edges. When m = 0, there is a shortest path from i to j with no edges if and only if i = j.

For , we compute as the minimum of and the minimum weight of any path from i to j consisting of at most m edges, obtained by looking at all possible predecessors k of j.

Computing the shortest-path weights bottom up EXTEND-SHORTEST-PATHS(L, W) 1 n ← rows[L] 2 let be an n × n matrix 3 for i ← 1 to n 4 do for j ← 2 to n 5 do 6 for k ← 1 to n 7 do 8 return

Relation with matrix multiplication Compute the matrix product C = A · B of two n × n matrices A and B. Then, for i, j = 1, 2,..., n, we compute If we make the substitutions in

Relation with matrix multiplication If we make these changes to EXTEND-SHORTEST-PATHS and also replace ∞ by 0, we obtain the straightforward Θ(n3)-time procedure for matrix multiplication: MATRIX-MULTIPLY(A, B) 1 n ← rows[A] 2 let C be an n × n matrix 3 for i ← 1 to n 4 do for j ← 1 to n 5 do 6 for k ← 1 to n 7 do 8 return C

All-pairs shortest-paths problem We compute the shortest-path weights by extending shortest paths edge by edge. Letting A · B denote the matrix "product" returned by EXTEND-SHORTEST-PATHS(A, B), we compute the sequence of n - 1 matrices As we argued above, the matrix contains the shortest-path weights. The following procedure computes this sequence in time.

As we argued above, the matrix contains the shortest-path weights As we argued above, the matrix contains the shortest-path weights. The following procedure computes this sequence in time. SLOW-ALL-PAIRS-SHORTEST-PATHS(W) 1 n ← rows[W] 2 L(1) ← W 3 for m ← 2 to n - 1 4 do ← EXTEND-SHORTEST-PATHS( , W) 5 return

Improved algorithm Running time , much worse than the solution using Dijkstra’s algorithm. Can we improve this? Observe that we are only interested to find , all others are only auxiliary. We have , thus

Improved algorithm We can calculate using “repeated squaring” to find FASTER-ALL-PAIRS-SHORTEST-PATHS(W) 1 n ← rows[W] 2 ← W 3 m ← 1 4 while m < n - 1 5 do ← EXTEND-SHORTEST-PATHS( , ) m ← 2m Return The running time of FASTER-ALL-PAIRS-SHORTEST-PATHS is

The Floyd-Warshall algorithm Use different dynamic-programming formulation to solve the all-pairs shortest-paths problem on a directed graph G = (V, E). The resulting algorithm, known as the Floyd-Warshall algorithm, runs in time. The algorithm considers the "intermediate" vertices of a shortest path

The structure of a shortest path Observation 1: A shortest path does not contain the same vertex twice. Proof: A path containing the same vertex twice contains a cycle. Removing cycle gives a shorter path. Observation 2: For a shortest path from to such that any intermediate vertices on the path are chosen from the set , there are two possibilities: 1. k is not a vertex on the path, The shortest such path has length 2. k is a vertex on the path. where an intermediate vertex of a simple path p = v1, v2,..., vl is any vertex of p other than v1 or vl, that is, any vertex in the set {v2, v3,..., vl-1}.

The structure of a shortest path Consider a shortest path from i to j containing the vertex k. It consists of a subpath from i to k and a subpath from k to j. Each subpath can only contain intermediate vertices in , and must be as short as possible, namely they have lengths and Hence the path has length where an intermediate vertex of a simple path p = v1, v2,..., vl is any vertex of p other than v1 or vl, that is, any vertex in the set {v2, v3,..., vl-1}.

Combining the two cases we get

The Bottom-up Computation Bottom: , the weight matrix. Compute from using for k = 1, …, n

Floyd-Warshall Algorithm FLOYD-WARSHALL(W) 1 n ← rows[W] 2 ← W 3 for k ← 1 to n 4 do for i ← 1 to n 5 do for j ← 1 to n 6 do 7 return D(n)