# All Pairs Shortest Path

Document Sample

```					  Dept. of Computer Science
& Information Engineering

All Pairs Shortest Path

Instructor: Yao-Ting Huang

Bioinformatics Laboratory,
Department of Computer Science & Information Engineering,
National Chung Cheng University.              1
Dept. of Computer Science
& Information Engineering

Quiz
A
8                    4
-2
7            1
B             C                D
3            9       5
-2
F                    G

   Give execution steps and complexity of the algorithm.
   Find a DFS tree starting from A.
   Find a minimum spanning tree with Prim’s algorithm.
   Find single-source shortest paths starting from A.
2
Dept. of Computer Science
& Information Engineering

Bellman-Ford Algorithm
BellmanFord()
for each v  V                           u   5          v
d[v] = ;
       -2         
d[s] = 0;                        6
for i=1 to |V|-1
for each edge (u,v)  E                         -3
Relax(u,v, w(u,v));    0       8                      7
for each edge (u,v)  E                           -4
if (d[v] > d[u] +         s               2
w(u,v))                          7
return “FALSE”;
                  
9
x              y
Relax(u,v,w): if (d[v] >
d[u]+w) then d[v]=d[u]+w
3
Dept. of Computer Science
& Information Engineering

Single-source shortest paths in DAG
DAG-SHORTEST-PATHS(G, w, s)
1 topological sort the vertices of G
2 INITIALIZE-SINGLE-SOURCE(G, s)
3 for each vertex u taken in topological sorted order
4          do for each vertex v  adj [ u ]
5              do RELAX(u, v, w)
Time complexity O(V+E)
6                           1
u
r               s         t                           v        x
5             2         7             -1           -2
               0          2          6                5        4
4
4
3                                  2
Dept. of Computer Science
& Information Engineering
u              v
1
Dijkstra’s Algorithm          10
              
9
Dijkstra(G)                 0      2 3        4           6
for each v  V           s             7
5
d[v] = ;                                  
2
d[s] = 0; S = ; Q = V;         x              y

while (Q  )
u = ExtractMin(Q);
S = S U {u};
if (d[v] > d[u]+w(u,v))       Relaxation
d[v] = d[u]+w(u,v);        Step

5
Dept. of Computer Science
& Information Engineering

   You have to output shortest paths to the
destination if no negative cycles.
 You  can output the destination is reachable from the
negative cycles.
 You are encouraged to give detail routing path of
passing negative cycles.

6
Dept. of Computer Science
& Information Engineering

Shortest Path Problems
   Single-source Shortest Problem
   Dijkstra (negative edge is not allowed)
   Bellman-Ford (negative cycle is allowed)
   Single-destination shortest-path
   All-pairs shortest path Problem

7
Dept. of Computer Science
& Information Engineering

Outline
   All-Pairs Shortest Path
   The Floyd-Warshall algorithm
   The structure of a shortest path
   A recursive solution
   Computing weights                  Dynamic Programming

   Constructing Shortest Path
   Analysis
   Transitive closure of a directed graph

8
Dept. of Computer Science
& Information Engineering

All-Pair Shortest Paths Algorithm

   Given a weighted, directed graph G=(V, E), we
want to compute the shortest paths from u to v
for all vertex pairs u, v ε V.
 How     to apply previous algorithms (e.g., Bellman-ford)
to solve this problem?

9
Dept. of Computer Science
& Information Engineering

All pair shortest Path Problem
   The easiest way!
   Iterate Dijkstra’s and Bellman-Ford |V| times!
   Dijkstra:
   O(VlgV + E) -> O(V2lgV + VE)                  O(V3)
On dense
graph
   Bellman-Ford:
   O(VE)                  -> O(V2E)              O(V4)

   Faster-All-Pairs-Shortest-Paths:
   O(V3lgV)

10
Dept. of Computer Science
& Information Engineering

All pair shortest Path Problem

If i = j, then p has weight 0 and no edge.
p'
If i  j , we decompose p into i k  j where p' contains at most
~

m-1 edges.
Moreover, p' is a shortest path from i to k and  ( i , j )   ( i , k )  wkj .

11
Dept. of Computer Science
& Information Engineering

All pair shortest Path Problem
A recursive solution to the all-pairs shortest-path problem.

Define:
(m
lij ) = minimum weight of any path from i to j that contains at

most m edges.

 0 if i  j
lij0)
(

 if i  j
Then

lijm )  min{lijm 1) , min1 k  n {likm 1)  wkj }}
(            (                       (

 min1 k  n {likm 1)  wkj }
(                               , since wjj=0.
12
Dept. of Computer Science
& Information Engineering

All pair shortest Path Problem
Since the shortest path from i to j contains at most m-1 edges,

 (i, j )  lijn 1)  lijn)  lijn 1)  
(          (       (

Computing the shortest-path weight bottom up:
( n 1)
where L  (lij ) .
(1) ( 2)                 ( m)  ( m)
Compute L , L ,, L
Note that L(1)  W = the weight matrix of edges.

13
Dept. of Computer Science
& Information Engineering

Slow All pair shortest Path Algorithm
EXTENDED-SHORTEST-PATHS(L, w)
1 n  row[L ]


2 Let L  (lij ) be an n  n matrix

3 for i  1 to n
4        do for j  1 to n

5                 
do lij  

6               for k  1 to n

7                   do   lij  min(lij , lik  wkj )
8 return       L                                        14
Dept. of Computer Science
& Information Engineering

Slow All pair shortest Path
Algorithm

L  L W  W
(1)        ( 0)

L  L W  W
( 2) (1)    2

L  L W  W
(3)  ( 2)   3


( n 1)          ( n  2)            n 1
L                  L          W  W
15
Dept. of Computer Science
& Information Engineering

Slow All pair shortest Path
Algorithm
SLOW-ALL-PAIRS-SHORTEST-PATHS(W)
1 n  row[ W ]
2 L(1)  W
3 for m  2 to n-1
4 do L(m)  EXTENDED-SHORTEST-PATHS( L( m1) ,W )
( n 1)
5 return L
16
Dept. of Computer Science
& Information Engineering

Slow All pair shortest Path Algorithm
SLOW-ALL-PAIRS-SHORTEST-PATHS(W)
1 n  row[ W ]
2 L W(1)

3 for m  2 to n-1
4 do L
(m)
 EXTENDED-SHORTEST-PATHS( L
( m1)
,W )
( n 1)
5 return L
17
Dept. of Computer Science
& Information Engineering

Slow All pair shortest Path Algorithm
EXTENDED-SHORTEST-PATHS(L, w)

2                     1 n  row[L ]


2 Let L  (lij ) be an n  n matrix
3                      4

1
8
3   3 for i  1 to n

7                         4    do for j  1 to n

-4                        1       -5   5              
do lij  
2

5                    4            6               for k  1 to n
6
7          do   lij  min(lij , lik  wkj )
8 return    L                            18
Dept. of Computer Science
& Information Engineering

Slow All pair shortest Path Algorithm
2
EXTENDED-SHORTEST-PATHS(L, w)
3                   4
1 n  row[L ]
8
1                                 3
7                                     
2 Let L  (lij ) be an n  n matrix
-4                  1        -5
2                    3 for i  1 to n
5
6
4            4    do for j  1 to n

 0 3 8   4                                
do lij  
                             5
 0  1 7                    6               for k  1 to n
L(1)    4 0   
                                             lij  min(lij , lik  wkj )
 2  5 0                   7          do
   6 0                                L                             19
                             8 return
Dept. of Computer Science
& Information Engineering

Slow All pair shortest Path Algorithm
 0 3 8   4
            
2                           0  1 7 
4        L(1)    4 0   
3                                                
 2  5 0  
8                      6 0 
1                                        3                      
7
1       -5             0 3     8 2  4
-4              2                                             
3 0 4 1 7 
5                 4            L( 2)     4     0 5 11 
6                                             
 2  1  5 0  2
8  1 6 0 
                
20
Dept. of Computer Science
& Information Engineering

Slow All pair shortest Path Algorithm
 0 3  3 2  4
                
2                            3 0  4 1 1
4        L(3)     7 4     0 5 11 
3                                                      
 2  1  5 0  2
1
8
3             8 5     1 6 0 
                
7
1       -5              0 1  3 2  4
-4               2                                           
 3 0  4 1 1
5
6
4            L( 4)    7 4     0 5 3 
                
 2  1  5 0  2
8 5     1 6 21 
              0 
Dept. of Computer Science
& Information Engineering

Slow All pair shortest Path Algorithm
SLOW-ALL-PAIRS-SHORTEST-PATHS(W)
1 n  row[ W ]
2 L(1)  W
3 for m  2 to n-1
4 do L  EXTENDED-SHORTEST-PATHS( L
(m)                          ( m1)
,W )
( n 1)
5 return L

Time complexity: O( n4 ) .

22
Dept. of Computer Science
& Information Engineering

   Do you think this algorithm can detect
negative cycle as well?
   How to improve it’s time complexity?

23
Dept. of Computer Science
& Information Engineering

Improvement
   Hint:
 給你一塊圓形蛋糕和一支刀子，假設你只可以劃
四刀，請問這塊蛋糕最多可以切成幾塊？

24
Dept. of Computer Science
& Information Engineering

Fast All pair shortest Path Algorithm
Improving the running time:
L(1)  W
L( 2 )    W 2  W W
L( 4 )  W 4  W 2  W 2

( 2  log( n1)  )    2  log( n1) 
L                    W

i.e., using repeating squaring!

Time complexity: O(n 3 log n) .
25
Dept. of Computer Science
& Information Engineering

Why???
   One way is to prove the associative relation of
this function.
 See        Exercise 25.1-4.
   A more intuitive way.
 L2 = extend(L1, L1)
 L4 = extend(L2, L2) ???

 What if the optimal solution is from L3.

 L3 = extend(L2, L1) is included into the consideration
of L4 = extend(L2, L2), since L1 is included in L2
          
lij  min( lij , lik  lkj )     26
Dept. of Computer Science
& Information Engineering

Fast All pair shortest Path Algorithm
FASTER-ALL-PAIRS-SHORTEST-PATHS(W)
1 n  row [ W ]

2 L W
(1)

3 m 1
4 while n  1  m

L( 2m)  EXTENDED-SHORTEST-PATHS( L
( m)
5        do                                          , L( m ) )
6         m  2m

7 return L(m)
27
Dept. of Computer Science
& Information Engineering

Notes On Homework #2
   Now you have more choices.
 Bellman-Ford.

 Topological              sort.
 Dijkstra.

   All pairs-shortest paths maybe helpful to find
the route from negative cycles to destination
originally unreachable.

28

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 141 posted: 1/26/2011 language: English pages: 28
Description: Analysis and Design of Algorithms
About if any file u wil find copyright contact me it will be remove in 3 to 4 buisnees days. add me on sanjaydudeja007@gmail.com or visit http://www.ohotech.com/