Presentation is loading. Please wait.

Presentation is loading. Please wait.

All Pairs Shortest Path Algorithms Aditya Sehgal Amlan Bhattacharya.

Similar presentations


Presentation on theme: "All Pairs Shortest Path Algorithms Aditya Sehgal Amlan Bhattacharya."— Presentation transcript:

1 All Pairs Shortest Path Algorithms Aditya Sehgal Amlan Bhattacharya

2 Floyd Warshal Algorithm All-pair shortest path on a graph Negative edges may be present Negative weight cycles not allowed Uses dynamic programming Therefore bottom up solution

3 Problem Description…… A graph G(V,E,w) be a weighted graph with vertices v 1, v 2, …………v n. Find the shortest path between each pair of vertices v i, v j, where i,j <= n and find out the all pairs shortest distance matrix D

4 Principle on which it is based…….. For 2 vertices v i, v j in V, out of all paths between them, consider the ones which their intermediate vertices in { v 1, v 2, …..v k } If p k i,j be the minimum cost path among them, the cost being d (k) i,j If v k is not in the shortest path then p k i,j = p k-1 i,j

5 Principles cont……. If v k is in p k i,j, then we can p k i,j break into 2 paths - from v i to v k and from v k to v j Each of them uses vertices in the set { v 1, v 2, ……….v k-1 }

6 Recurrence equation ……… d (k) i,j = { w(v i, v j ) if k =0 ; min { d (k-1) i,j, d (k-1) i,k + d (k-1) k,j } if k>0 } The output of the algorithm is the matrix D (n) = (d (n) i,j )

7 Algorithm…… procedure FLOYD_ALL_PAIRS (A) begin D(0) = A ; for k = 1 to n do for i = 1 to n do for j = 1 to n do d (k) i,j = min { d (k-1) i,j, d (k-1) i,k + d (k-1) k,j }; end FLOYD_ALL_PAIRS

8 Complexity of the sequential algorithm…….. Running time O (n 3 ) Space complexity O (n 2 )

9 An example graph….. 2 13 5 4 -4 3 6 -5 4 2 8 7 1

10 How it works ………. 038m-4 m0m17 D (0) = m40mm 2m-50m mmm60

11 How it works ………. 038m-4 m0m17 D (1) = m40mm 25-50m mmm60

12 How it works ………. 0384-4 m0m17 D (2) = m40511 25-50-2 mmm60

13 How it works ………. 0384-4 m0m17 D (3) = m40511 2-1-50-2 mmm60

14 How it works ………. 03-14-4 30-41-1 D (4) = 74053 2-1-50-2 85160

15 How it works ………. 01-32-4 30-41-1 D (5) = 74053 2-1-50-2 85160

16 Parallel formulation……… If p is the number of processors available, the matrix D (k) is partitioned into p parts For computation, each process needs some elements from the k th row and column in the matrix D (k-1) Use 2-D or 1-D block mapping

17 2-D block mapping

18 2-D Block mapping cont…… Each block size is (n / p 0.5 ) by (n / p 0.5 ) k th iteration, P i,j needs some part of k th row and column of the D k-1 matrix Thus at this time p 0.5 processors containing a part of the k th row sends it to the p 0.5 - 1 processes in the same column Similarly, p 0.5 processors containing a part of the k th column sends it to the p 0.5 -1 processes in the same row

19 Communication patterns …..

20 Algorithm …….

21 Analysis(computation)…….. Each process is assigned n / p 0.5 * n / p 0.5 i.e n 2 / p numbers. In each iteration, the computation time is O (n 2 / p ) For n iterations the computation time is O (n 3 / p )

22 Analysis(communication)…….. After a broadcast, a synchronization time of O(n log p) is required Each process broadcasts n / p 0.5 elements Thus total communication time takes O((n log p) / p 0.5 ) Thus for n iterations time O((n 2 log p) / p 0.5 )

23 Total parallel run time…… T p = O((n 2 log p) / p 0.5 ) + O (n 3 / p ) S = O( n 3 ) / T p E = 1 / (1 + O((p 0.5 log p)/n) ) Number of processors used efficiently is O(n 2 log 2 n ) Isoeffiency function = O (p 1.5 log 3 p )

24 How to improve on this ……. Waiting for the k th row elements by each processor after all the processors complete the k-1 th iteration Start as soon as it finishes the k-1 th iteration and it has got the relevant parts of the D (k-1) matrix Known as Pipelined 2-D Block mapping

25 How it is carried out …….. P i,j which has elements of the k th row after the k-1 th iteration sends the part of the D (k-1) matrix to processors P i,j+1, P i,j-1. Similarly P i,j which has elements of the k th column after the k-1 th iteration sends the part of the D (k-1) matrix to processors P i-1,i, P i+1,j Each of these values are stored and forwarded to the all the processors in the same row and column. The forwarding is stopped when mesh boundaries are reached

26 Pipelined 2 D Block mapping ……

27 Total parallel run time…… T p = O(n 3 / p ) + O (n ) S = O( n 3 ) / O(n 3 / p ) + O (n ) E = 1/ (1 + O(p/n 2 ) Number of processors used efficiently O(n 2 ) Isoeffiency function = O (p 1.5 )

28 Comparison……. AlgorithmMax Processes for E = O(1) Parallel Run time Isoefficiency function Djikstra source- partitioned O(n) O(n 2 )O(p 3 ) Djikstra source- parallel O(n 2 / log n)O(n log n)O(p 1.5 log 1.5 p) Floyd 1 D BlockO(n/ log n)O(n 2 log n)O(p 3 log 3 p) Floyd 2-D BlockO(n 2 / log 2 n)O(n log 2 n)O(p 1.5 log 3 p) Floyd pipelined 2-D Block O(n 2 )O(n)O(p 1.5 )


Download ppt "All Pairs Shortest Path Algorithms Aditya Sehgal Amlan Bhattacharya."

Similar presentations


Ads by Google