Title: All%20Pairs%20Shortest%20Path%20Algorithms
1All Pairs Shortest Path Algorithms
- Aditya Sehgal
- Amlan Bhattacharya
2Floyd 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
3Problem Description
- A graph G(V,E,w) be a weighted graph with
vertices v1, v2, vn. - Find the shortest path between each pair of
vertices vi , vj, where i,j lt n and find out
the all pairs shortest distance matrix D
4Principle on which it is based..
- For 2 vertices vi, vj in V, out of all paths
between them, consider the ones which their
intermediate vertices in v1, v2, ..vk - If pki,j be the minimum cost path among them,
the cost being d(k)i,j - If vk is not in the shortest path then
- pki,j pk-1i,j
5Principles cont.
- If vk is in pki,j , then we can pki,j break
into 2 paths - from vi to vk and - from vk to vj
- Each of them uses vertices in the set
- v1, v2, .vk-1
6Recurrence equation
- d(k)i,j w(vi, vj) if k 0
-
- min d(k-1)i,j, d(k-1)i,k d(k-1)k,j if
kgt0 -
-
- The output of the algorithm is the matrix
- D(n) (d(n)i,j )
7Algorithm
- 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
8Complexity of the sequential algorithm..
- Running time O (n3)
- Space complexity O (n2)
9An example graph..
2
4
3
1
3
8
2
7
1
-5
-4
5
4
6
10How it works .
- 0 3 8 m -4
- m 0 m 1 7
- D(0) m 4 0 m m
- 2 m -5 0 m
- m m m 6 0
-
11How it works .
- 0 3 8 m -4
- m 0 m 1 7
- D(1) m 4 0 m m
- 2 5 -5 0 m
- m m m 6 0
-
12How it works .
- 0 3 8 4 -4
- m 0 m 1 7
- D(2) m 4 0 5 11
- 2 5 -5 0 -2
- m m m 6 0
-
13How it works .
- 0 3 8 4 -4
- m 0 m 1 7
- D(3) m 4 0 5 11
- 2 -1 -5 0 -2
- m m m 6 0
-
14How it works .
- 0 3 -1 4 -4
- 3 0 -4 1 -1
- D(4) 7 4 0 5 3
- 2 -1 -5 0 -2
- 8 5 1 6 0
-
15How it works .
- 0 1 -3 2 -4
- 3 0 -4 1 -1
- D(5) 7 4 0 5 3
- 2 -1 -5 0 -2
- 8 5 1 6 0
-
16Parallel 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 kth row and column in the matrix D(k-1) - Use 2-D or 1-D block mapping
172-D block mapping
182-D Block mapping cont
- Each block size is (n / p0.5 ) by (n / p0.5 )
- kth iteration , Pi,j needs some part of kth
row and column of the Dk-1 matrix - Thus at this time p0.5 processors containing a
part of the kth row sends it to the p0.5 - 1
processes in the same column - Similarly , p0.5 processors containing a part
of the kth column sends it to the - p0.5 -1 processes in the same row
19Communication patterns ..
20Algorithm .
21Analysis(computation)..
- Each process is assigned n / p0.5 n / p0.5
- i.e n2 / p numbers. In each iteration , the
computation time is O (n2 / p ) - For n iterations the computation time is
- O (n3 / p )
22Analysis(communication)..
- After a broadcast, a synchronization time of O(n
log p) is required - Each process broadcasts n / p0.5 elements
- Thus total communication time takes
- O((n log p) / p0.5 )
- Thus for n iterations time
- O((n2log p) / p0.5 )
23Total parallel run time
-
- Tp O((n2log p) / p0.5 ) O (n3 / p )
- S O( n3 ) / Tp
-
- E 1 / (1 O((p0.5 log p)/n) )
-
- Number of processors used efficiently is
- O(n2 log2n )
- Isoeffiency function O (p1.5 log3 p )
24How 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
25How it is carried out ..
- Pi,j which has elements of the k th row after
the k-1th iteration sends the part of the D(k-1)
matrix to processors Pi,j1, Pi,j-1 . - Similarly Pi,j which has elements of the k th
column after the k-1th iteration sends the part
of the D(k-1) matrix to processors Pi-1,i,
Pi1,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
26Pipelined 2 D Block mapping
27Total parallel run time
-
- Tp O(n3/ p ) O (n )
- S O( n3 ) / O(n3/ p ) O (n )
-
- E 1/ (1 O(p/n2 )
-
- Number of processors used efficiently O(n2 )
- Isoeffiency function O (p1.5 )
28Comparison.
Algorithm Max Processes for E O(1) Parallel Run time Isoefficiency function
Djikstra source- partitioned O(n) O(n2) O(p3)
Djikstra source- parallel O(n2/ log n) O(n log n) O(p1.5 log1.5 p)
Floyd 1 D Block O(n/ log n) O(n2 log n) O(p3 log3 p)
Floyd 2-D Block O(n2/ log2 n) O(n log2 n) O(p1.5 log3 p)
Floyd pipelined 2-D Block O(n2) O(n) O(p1.5)