Algorithms All pairs shortest path

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
8.3 Representing Relations Connection Matrices Let R be a relation from A = {a 1, a 2,..., a m } to B = {b 1, b 2,..., b n }. Definition: A n m  n connection.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
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.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
Chapter 8, Part I Graph Algorithms.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
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
Dynamic Programming Reading Material: Chapter 7..
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CS 261 – Data Structures Graphs. Used in a variety of applications and algorithms Graphs represent relationships or connections Superset of trees (i.e.,
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
Dynamic Programming Dynamic Programming algorithms address problems whose solution is recursive in nature, but has the following property: The direct implementation.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 16 All shortest paths algorithms Properties of all shortest paths Simple algorithm:
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).
Data Structures and Algorithms
1 Dynamic Programming Jose Rolim University of Geneva.
Dynamic Programming 2 Mani Chandy
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Network Optimization Chapter 3 Shortest Path Problems.
Chapter 5 Dynamic Programming 2001 년 5 월 24 일 충북대학교 알고리즘연구실.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
1 Closures of Relations: Transitive Closure and Partitions Sections 8.4 and 8.5.
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.
Parallel Programming: All-Pairs Shortest Path CS599 David Monismith Based upon notes from multiple sources.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
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.
Relation. Combining Relations Because relations from A to B are subsets of A x B, two relations from A to B can be combined in any way two sets can be.
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.
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 Closures of Relations Based on Aaron Bloomfield Modified by Longin Jan Latecki Rosen, Section 8.4.
All Pairs Shortest Path Algorithms Aditya Sehgal Amlan Bhattacharya.
Chapter 8 Dynamic Programming
All-Pairs SPs on DG Run Dijkstra;s algorithm for each vertex or
Parallel Graph Algorithms
All-Pairs Shortest Paths (26.0/25)
Data Structures and Algorithms
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
Unit 4: Dynamic Programming
Dynamic Programming.
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Floyd-Warshall Algorithm
Floyd’s Algorithm (shortest-path problem)
Dynamic Programming.
Advanced Algorithms Analysis and Design
All pairs shortest path problem
Dynamic Programming.
Parallel Graph Algorithms
All-Pairs Shortest Paths
COSC 3101A - Design and Analysis of Algorithms 12
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Algorithms All pairs shortest path Floyd’s Algorithm All pairs shortest path CS333 / class 22

All pairs shortest path Algorithms All pairs shortest path The problem: find the shortest path between every pair of vertices of a graph The graph: may contain negative edges but no negative cycles A representation: a weight matrix where W(i,j)=0 if i=j. W(i,j)=¥ if there is no edge between i and j. W(i,j)=“weight of edge” Note: we have shown principle of optimality applies to shortest path problems CS333 / class 22

The weight matrix and the graph Algorithms The weight matrix and the graph 1 v1 v2 3 9 5 1 3 2 v5 3 2 v4 v3 4 CS333 / class 22

Algorithms The subproblems How can we define the shortest distance di,j in terms of “smaller” problems? One way is to restrict the paths to only include vertices from a restricted subset. Initially, the subset is empty. Then, it is incrementally increased until it includes all the vertices. CS333 / class 22

Algorithms The subproblems Let D(k)[i,j]=weight of a shortest path from vi to vj using only vertices from {v1,v2,…,vk} as intermediate vertices in the path D(0)=W D(n)=D which is the goal matrix How do we compute D(k) from D(k-1) ? CS333 / class 22

The Recursive Definition: Algorithms The Recursive Definition: Case 1: A shortest path from vi to vj restricted to using only vertices from {v1,v2,…,vk} as intermediate vertices does not use vk. Then D(k)[i,j]= D(k-1)[i,j]. Case 2: A shortest path from vi to vj restricted to using only vertices from {v1,v2,…,vk} as intermediate vertices does use vk. Then D(k)[i,j]= D(k-1)[i,k]+ D(k-1)[k,j]. Shortest path using intermediate vertices {V1, . . . Vk } Vk Vj Vi Shortest Path using intermediate vertices { V1, . . . Vk -1 } CS333 / class 22

The recursive definition Algorithms The recursive definition Since D(k)[i,j]= D(k-1)[i,j] or D(k)[i,j]= D(k-1)[i,k]+ D(k-1)[k,j]. We conclude: D(k)[i,j]= min{ D(k-1)[i,j], D(k-1)[i,k]+ D(k-1)[k,j] }. Shortest path using intermediate vertices {V1, . . . Vk } Vk Vj Vi Shortest Path using intermediate vertices { V1, . . . Vk -1 } CS333 / class 22

The pointer array P Used to enable finding a shortest path Algorithms The pointer array P Used to enable finding a shortest path Initially the array contains 0 Each time that a shorter path from i to j is found the k that provided the minimum is saved (highest index node on the path from i to j) To print the intermediate nodes on the shortest path a recursive procedure that print the shortest paths from i and k, and from k to j can be used CS333 / class 22

Floyd's Algorithm Using n+1 D matrices Algorithms Floyd's Algorithm Using n+1 D matrices Floyd//Computes shortest distance between all pairs of //nodes, and saves P to enable finding shortest paths 1. D0  W // initialize D array to W [ ] 2. P  0 // initialize P array to [0] 3. for k  1 to n 4. do for i  1 to n 5. do for j  1 to n 6. if (Dk-1[ i, j ] > Dk-1 [ i, k ] + Dk-1 [ k, j ] ) 7. then Dk[ i, j ]  Dk-1 [ i, k ] + Dk-1 [ k, j ] 8. P[ i, j ]  k; 9. else Dk[ i, j ]  Dk-1 [ i, j ] CS333 / class 22

Example 4 5 2 -3 1 3 W = D0 = 1 5 4 2 3 1 2 3 -3 2 P =  Algorithms 5 2  -3 1 3 W = D0 = 1 5 4 2 3 1 2 3 -3 2 P = CS333 / class 22

k = 1 Vertex 1 can be intermediate node Algorithms 4 5 2  -3 1 3 1 2 3 5 -3 4 D0 = k = 1 Vertex 1 can be intermediate node 4 5 2 7  -3 1 3 D1[2,3] = min( D0[2,3], D0[2,1]+D0[1,3] ) = min (, 7) = 7 D1[3,2] = min( D0[3,2], D0[3,1]+D0[1,2] ) = min (-3,) = -3 D1 = 1 2 3 P = CS333 / class 22

k = 2 Vertices 1, 2 can be intermediate Algorithms 4 5 2 7  -3 1 3 1 2 3 5 -3 4 D1 = k = 2 Vertices 1, 2 can be intermediate 4 5 2 7 -1 -3 1 3 D2[1,3] = min( D1[1,3], D1[1,2]+D1[2,3] ) = min (5, 4+7) = 5 D2[3,1] = min( D1[3,1], D1[3,2]+D1[2,1] ) = min (, -3+2) = -1 D2 = 1 2 3 P = CS333 / class 22

k = 3 Vertices 1, 2, 3 can be intermediate Algorithms 4 5 2 7 -1 -3 1 3 1 2 3 5 -3 4 D2 = k = 3 Vertices 1, 2, 3 can be intermediate 2 5 7 -1 -3 1 3 D3[1,2] = min(D2[1,2], D2[1,3]+D2[3,2] ) = min (4, 5+(-3)) = 2 D3[2,1] = min(D2[2,1], D2[2,3]+D2[3,1] ) = min (2, 7+ (-1)) D3 = 3 1 2 P = CS333 / class 22

Floyd's Algorithm: Using 2 D matrices Algorithms Floyd's Algorithm: Using 2 D matrices Floyd 1. D  W // initialize D array to W [ ] 2. P  0 // initialize P array to [0] 3. for k  1 to n // Computing D’ from D 4. do for i  1 to n 5. do for j  1 to n 6. if (D[ i, j ] > D[ i, k ] + D[ k, j ] ) 7. then D’[ i, j ]  D[ i, k ] + D[ k, j ] 8. P[ i, j ]  k; 9. else D’[ i, j ]  D[ i, j ] 10. Move D’ to D. CS333 / class 22

Can we use only one D matrix? Algorithms Can we use only one D matrix? D[i,j] depends only on elements in the kth column and row of the distance matrix. We will show that the kth row and the kth column of the distance matrix are unchanged when Dk is computed This means D can be calculated in-place CS333 / class 22

The main diagonal values Algorithms The main diagonal values Before we show that kth row and column of D remain unchanged we show that the main diagonal remains 0 D(k)[ j,j ] = min{ D(k-1)[ j,j ] , D(k-1)[ j,k ] + D(k-1)[ k,j ] } = min{ 0, D(k-1)[ j,k ] + D(k-1)[ k,j ] } = 0 Based on which assumption? CS333 / class 22

The kth column kth column of Dk is equal to the kth column of Dk-1 Algorithms The kth column kth column of Dk is equal to the kth column of Dk-1 Intuitively true - a path from i to k will not become shorter by adding k to the allowed subset of intermediate vertices For all i, D(k)[i,k] = = min{ D(k-1)[i,k], D(k-1)[i,k]+ D(k-1)[k,k] } = min { D(k-1)[i,k], D(k-1)[i,k]+0 } = D(k-1)[i,k] CS333 / class 22

The kth row kth row of Dk is equal to the kth row of Dk-1 Algorithms The kth row kth row of Dk is equal to the kth row of Dk-1 For all j, D(k)[k,j] = = min{ D(k-1)[k,j], D(k-1)[k,k]+ D(k-1)[k,j] } = min{ D(k-1)[ k,j ], 0+D(k-1)[k,j ] } = D(k-1)[ k,j ] CS333 / class 22

Floyd's Algorithm using a single D Algorithms Floyd's Algorithm using a single D Floyd 1. D  W // initialize D array to W [ ] 2. P  0 // initialize P array to [0] 3. for k  1 to n 4. do for i  1 to n 5. do for j  1 to n 6. if (D[ i, j ] > D[ i, k ] + D[ k, j ] ) 7. then D[ i, j ]  D[ i, k ] + D[ k, j ] 8. P[ i, j ]  k; CS333 / class 22

Printing intermediate nodes on shortest path from q to r Algorithms Printing intermediate nodes on shortest path from q to r path(index q, r) if (P[ q, r ]!=0) path(q, P[q, r]) println( “v”+ P[q, r]) path(P[q, r], r) return; //no intermediate nodes else return Before calling path check D[q, r] < , and print node q, after the call to path print node r 3 1 2 P = 1 2 3 5 -3 4 CS333 / class 22

Algorithms Example CS333 / class 22

The final distance matrix and P Algorithms The final distance matrix and P The values in parenthesis are the non zero P values. CS333 / class 22

The call tree for Path(1, 4) Algorithms The call tree for Path(1, 4) Path(1, 4) Path(6, 4) Path(1, 6) Print v6 P(1, 6)=0 Path(6, 3) Print v3 Path(3, 4) P(3, 4)=0 P(6, 3)=0 The intermediate nodes on the shortest path from 1 to 4 are v6, v3. The shortest path is v1, v6, v3, v4. CS333 / class 22