Docsity

Notes on The Floyd-Warshall Algorithm | COP 3503, Study notes of Computer Science

Partial preview of the text

Download Notes on The Floyd-Warshall Algorithm | COP 3503 and more Study notes Computer Science in PDF only on Docsity!

Lecture 15: The Floyd-Warshall

Algorithm

CLRS section 25.

Outline of this Lecture

Recalling the all-pairs shortest path problem.

Recalling the previous two solutions.

The Floyd-Warshall Algorithm.

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 b

d c

e

20

(^12 ) 4 17

3 3 8

โˆ’

5

10

4 4 4

a b

d c

e

without negative cost cycle with negative cost cycle

6

Solution 3: the Input and Output Format

As in 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       where   is the distance from vertex to.

Step 1: The Floyd-Warshall Decomposition

Definition: The vertices         are called the intermediate vertices of the path          .

Let  be the length of the shortest path from to such that all intermediate vertices on the path (if any) are in set         .    is set to be^ ^ , i.e., no intermediate vertex. Let  ^ be the matrix      .

Claim:   ! is the distance from to. So our aim is to compute   !^.

Subproblems: compute  ^ for     #"$"#"^  

Step 2: Structure of shortest paths

Consider a shortest path from to containing the vertex

. It consists of a subpath from to

and a subpath from

to. Each subpath can only contain intermediate vertices

in 

  

, and must be as short as possible,

namely they have lengths  ^

^ 

and  ^

^ 

Hence the path has length

 ^ 

Combining the two cases we get

 ^ 

 ^ ^ 

 ^ 



Step 3: the Bottom-up Computation

Bottom:  ^     , the weight matrix.

Compute  ^ from  ^ ^  using

 ^ 

  ^ 

for         .

Comments on the Floyd-Warshall Algorithm

The algorithmโ€™s running time is clearly  .

The predecessor pointer      can be used

to extract the final path (see later ).

Problem: the algorithm uses   space.

It is possible to reduce this down to   space by keeping only one matrix instead of . Algorithm is on next page. Convince yourself that it works.

The Floyd-Warshall Algorithm: Version 2

Floyd-Warshall(  ^ ) for   to do initialize for  ^  to^ do         ;       ^ ^ ;

for    to do dynamic programming for   to do for   to do if                                ;        ;



return          ^  ;

The Algorithm for Extracting the Shortest Paths

Path(  ^ )

if (       ) single edge output   ; else compute the two parts of the path

Path(       ); Path(          ); 

Example of Extracting the Shortest Paths

Find the shortest path from vertex 2 to vertex 3.   Path                Path   (^)     (^)           Path                   ^ (2,5) Path                ^ (5,4) Path                   Path                 ^ (4,6) Path               ^ (6,3)