You are on page 1of 13

Lecture 18: Floyd-Warshall Algorithm

The Floyd-Warshall Algorithm.

1
Recall the All-Pairs Shortest Paths Problem

   

Given a weighted digraph with a weight

 
function , where  is the set of real num-
bers, determine the length of the shortest path (i.e.,

distance) between all pairs of vertices in . Here we
assume that there are no cycle with zero or negative
cost.

a 20 b a −20 b
6 e 3 e
12 8 4 4 5
5 3 4
4
17 10
d c d c

without negative cost cycle with negative cost cycle

2
Solutions Covered in the Previous Lecture



time  .

Solution 2: Dynamic programming solution, based on

a natural decomposition of the problem. Running


time  .

Our task: develop another dynamic programming al-

gorithm, the Floyd-Warshall algorithm, with time com-
 

plexity  .
It also illustrate that there could be more than one way
of developing a dynamic programming algorithm.

3
Solution 3: the Input and Output Format

Like the previous dynamic programming algorithm, we

  
assume that the graph is represented by an
matrix with the weights of the edges:
 \$%
if #
  & \$'
 \$  )\$'
* 
 ! " # if # ( and # ,
+  \$  ) \$'
,* 
if # ( and # .

    ./
Output Format: an distance - 10 where
/ \$
 is the distance from vertex # to .

4
Step 1: The Floyd-Warshall Decomposition

 6575858
Definition: The vertices 243 2  2 9<;>= are called the
:
 @   585857
6
intermediate vertices of the path ? 2 = 2:3 2A9CB .

/ED)FHG
Let  be the length of the shortest path from
\$
# to such that any intermediate vertices on the
 KLM565M56N%O
path (if any) are chosen from the set I'J .
/ DQP:G
 is defined to be  , i.e., no intermediate ver-
tex.
DQFHG    .R/%D)FHG
Let - be the matrix  0 .

/EDTSG \$
 is the distance from # to . So our aim is to
DUSG
compute - .

DQFHG N   6VMV6VMWX5
Subproblems: compute - for J
"

Question: What is the easiest subproblem?

5
Step 2: Structure of shortest paths

Observation 1:
A shortest path does not contain the same vertex twice.
Proof: A path that contains the same vertex twice,
contains a cycle. Removing the cycle gives a shorter
path.

\$
Observation 2: For a shortest path from # to such
that any intermediate vertices on the path are chosen
WKLM56565MN%O
from the set I'J , there are two possibilities:
N
1. is not a vertex on the path,
/YDQF ;>= G
The shortest such path has length  .
N
2. is a vertex on the path.
/YDQF ;>= G /ED)F ;>= G
The shortest such path has length  F Z F  .

6
Step 2: Structure of shortest paths

\$
Consider a shortest path from # to containing the
N N
vertex . It consists of a subpath from # to and a
N \$
subpath from to .
Each of them only contains intermediate vertices in
M585858 N [ O
IJ J , and must be as short as possible,
/ D)F ;>= G / D)F ;>= G
namely  F and F  .
/ QD F ;>= G / QD F ;>= G
Hence the path has length  F Z F  .

Combining the two cases we get

/ QD FHG  \ ]8^ / QD F ;>= G  / DQF ;>= G / )D F ;_= G O`5
 I  F Z F

7
Step 3: the Bottom-up Computation

DQP:G  .
Bottom: - aM0 , the weight matrix.

DQFHG DQF ;>= G

Compute - from - using
/EDQFHG  \ 8] ^ bc/EDQF ;>= G d/ED)F ;_= G /EDQF ;>= G
  F Z F  e
N   585858f
M
for J .

8
The Floyd-Warshall Algorithm

f
Floyd-Warshall( )
g  
for # J to do
\$  
for J to do
g /h. ) \$  . &\$
# 0 # 0;
/h. &\$  
?EiAj # 0 )# k ;
l
N  
for J to do
 
for # J to do
\$  
for J to do
d/h. mN /h.nNop\$ /h. ) \$

if # 0 Z 0q # 0
g /h. & \$  h / .  N /h.nNop\$
# 0 # 0 Z 0;
/h. p\$  N
?EiAj # 0 ;
l
/h. 575rX 585s
return J J 0;
l

9
Comments on the Floyd-Warshall Algorithm

 

 NE

superscript in the code.

This does not affect the correctness
of the algorithm.
Why???

.{z|d}
The predecessor pointer tvuxwAy 0 can be used
to extract the final path (see next page).

10
Extracting the Shortest Paths

.z`d}
The predecessor pointers tvu~wAy 0 can be used to
extract the final path. The idea is as follows.

Whenever we discover that the shortest path from #

\$ N
to passes through an intermediate vertex , we set
/h. p\$  N
?EiAj # 0 .

If the shortest path does not pass through any inter-

/h. &\$  
mediate vertex, then ?%iAj # 0 )# k .

\$ /h. )\$
To find the shortest path from # to , we consult ?%iAj # 0 .
 )\$'

If it is nil, then the shortest path is just the edge # .

Otherwise, we recursively compute the shortest path
/h. p\$ /h. &\$
from # to ?EiAj # 0 and the shortest path from ?EiAj # 0
\$
to .

11
The Algorithm for Extracting the Shortest Paths

Q\$
Path( # )
g /h. &\$  
if (?EiAj # 0 #)k ) single edge
 &\$'

output # ;
else compute the two parts of the path
g
 /h. p\$
Path( # ?EiAj # 0 );
/h. p\$ ) \$
Path(?%ij # 0 );
l
l

12
Example of Extracting the Shortest Paths

Find the shortest path from vertex 2 to vertex 3.

KL575 WKL 
/h.Km  
Path ?EiAj 0
KL575r 585 WKLd
/h.K   
Path ?EiAj 0
KL575R 585s585 WKL
/h.K  
Path ?EiAj 0 #)k  T?EY (2,5)
K 585r 585  d
/h.n   
Path ?EiAj 0 )# k  T?EY (5,4)
Kh 585  
/h.Rm  
Path m
?EiAj 0
Kh 585R 585 /h.R  
Path ?EiAj 0 #)k  T?EY (4,6)
KhY 585  
/h.m  
Path ?EiAj 0 )# k  T?EY (6,3)
KhYo

13