Skip to main content
All-Pairs Shortest Path
         Theory and Algorithms


   Carlos Andres Theran Suarez
Program Mathematics and Scientific Computing
        University of Puerto Rico
            Carlos.theran@upr.edu

                October – 2011
             Mayaguez-Puerto Rico


                        Dr Marko Schütz
Introduction

• In this section we consider de problem of finding
  shortest path between all pair of vertices in a directed
  graph  = (, ).

     – With a weight function :  → ℝ
         = − ( , + ) where  ∈    ∈ ℕ.
                =


     For this goal we are going to use the adjacency-matrix of a
     graph.
 •   The input: is a       =  which is
     adjacency-matrix of a  = ,  .
 •   The output: is the    matrix of SPL  ,  , ∀ ∈ .
Recall

• Single-Source shortest paths.
   • Nonnegative edge weight.
   – Dijkstra’s algorithm: Running time Array  
                           Running time Binary heap (  +  log )
                           Running time Fibonacci heap ( +  log )


   • General.
   – Bellman-Ford: Running time  


   • UDG.
   – Breadth for search: Running time   +
What do you think?

Can we solve all-pair shortest paths by running a
single source-paths algorithms?
What do you think?

Can we solve all-pair shortest paths by running a
single source-paths algorithms?

All-pair shortest paths.
• Nonnegative edge weight
   Dijkstra’s algorithm: Running time Array
What do you think?

Can we solve all-pair shortest paths by running a
single source-paths algorithms?

All-pair shortest paths.
• Nonnegative edge weight
   Dijkstra’s algorithm: Running time Array  
                         Running time Binary heap (  +  log )
What do you think?

Can we solve all-pair shortest paths by running a
single source-paths algorithms?

All-pair shortest paths.
• Nonnegative edge weight
   Dijkstra’s algorithm: Running time Array  
                         Running time Binary heap (  +  log )
                         Running time Binary heap   +  log
What do you think?

Can we solve all-pair shortest paths by running a
single source-paths algorithms?

All-pair shortest paths.
• Nonnegative edge weight
   Dijkstra’s algorithm: Running time Array  
                         Running time Binary heap (  +  log )
                         Running time Binary heap   +  log 
• General.
   Bellman-Ford: Running time
What do you think?

Can we solve all-pair shortest paths by running a
single source-paths algorithms?

All-pair shortest paths.
• Nonnegative edge weight
   Dijkstra’s algorithm: Running time Array  
                         Running time Binary heap (  +  log )
                         Running time Binary heap   +  log 
• General.
   Bellman-Ford: Running time   
   In a dense graph
Predecessor Matrix

Let  =  the predecessor Matrix, where

                  =          
 =
                 .   .

Now we define the predecessor subgraph of G for  as
, = (, , , ).


Where , =  ∈ :  ≠  ∪ 
          , =      ,  :  ∈ , − {}
Predecessor Matrix
Outline

1. Present a dynamic programming algorithms based on
   matrix multiplication to solve the problem.

2. Dynamic programming algorithms called Floyd-Warshall
   algorithms.

3. Unlike the others algorithms, Johnson's algorithms used
   adjacency-list representation of a graph.
Shortest path and matrix multiplication

1. The structure of shortest path.
Let suppose that we have a shortest path  form vertix  to
vertex , and suppose that  have at most  < ∞ edge.

• If  =  then  have weight 0.

• If  ≠  then  ↝′  → , where ′ has at most  −  edge,
  by lemma 24.1 ′ is a shortest path from   .
  So  ,  =  ,  +  .
Shortest path and matrix multiplication (cont.)

2. A recursive solution.
Let  () be the minimum weight of any path from vertex  to vertex
 that contains at most  edge.
           ()        = 
•           =
                    ∞   ≠ 


•  () = min(      −
                                        , min  (−) +  )
                                       1≤≤
                                  (−)
                   = min             + 
                      1≤≤


  ,  =  (−) =  () =  (+) = ⋯
Shortest path and matrix multiplication (cont.)

3. Computing shortest-path weight bottom up
Input  = ( ).                We compute  ,  , … , − .
              = ( () )   = , , … ,  − .
Shortest path and matrix multiplication (cont.)

• Now we can see the relation to the matrix multiplication.
Let  =  ∗  the matrix product of . For ,  = , … , .
We have  =   ∗  .
                   =


If we set; (−) →                   → 
            →                        → +
           +→∗

                                           ←  +  ∗
Shortest path and matrix multiplication (cont.)




Computing the sequence of  −  matrix
                     () = () ∗  = 
                    () = () ∗  = 
                                   ⋮
               (−) = (−) ∗  = −
Shortest path and matrix multiplication (cont.)




• Improving the running time.

Our goal, is to compute − matrices, let go to see that we
can compute − with only log(  − ) matrix product.
Shortest path and matrix multiplication (cont.)




() = 
() = () =  ∗ 
() = () =  ∗ 
() = () =  ∗ 
⋮
      log − )         log − )        log − −        log − −
(             = (             =                ∗
Shortest path and matrix multiplication (cont.)
The Floyd-Warshall algorithm
The algorithm consider a intermediate vertices of a shortest
path.
1. The structure of a shortest path.
Intermediate vertex  =<  ,  , … ,  > in a any vertex of 
other than  or  , so it can be the set  , … , − .
Let assume that the vertex of  are  = , , … ,  and a
subset , , … ,  for some .

• If  ∉  of path , then all the vertices intermediate 
  are in the set , , … ,  −  . Thus, a shortest path from
  vertex  to vertex  with all intermediate vertices in the set
   , , … ,  −  is also a shortest path form  to  with all
   in the set , , … ,  .
The Floyd-Warshall algorithm (cont)

• If  ∈  of path , we break  down into  ↝  ↝ .
   is a shortest path from  to , so  ∉  of  , thus  is
  a shortest path form  to  with all  in the set
   , , … ,  −  . Similarly  is a shortest path form  to 
  with all  in the set , , … ,  −  .
The Floyd-Warshall algorithm (cont)

3. A recursive solution.


                                                          = 
          ()
           =                −              −              −
                     min            ,            +              ≥ 


Since for every path, all intermediate vertices are in the set
 , , … ,  , matriz () = ( () ) gives the final answer:
 () =  ,  .
The Floyd-Warshall algorithm (cont)

• input: A    matrix 
• output: A    matrix () of shortest path weight.


•  () = min    −
                                     ,    −
                                                        +    −
The Floyd-Warshall algorithm (cont)
The Floyd-Warshall algorithm (cont)
4. Constructing a Shortest path
We compute the predecessor matrix  just as the Floyd-warshall algorithm
compute the matrices () .
so  = () = ( () ).

Recursive formulation.

                                               −              −              −
                    ()               ≤            + 
 () =                                  −              −              −
                    ()               >            + 


         ()
                                =    = ∞.
          =
                                   ≠    < ∞.
Johnson's algorithm for sparse graphs.


• It is asymtoticaly better than repeated squaring of matrices
  or the Floyd-Warshall algoritm.

• It use a subroutine both Dijkstra’s algorithm and Bellman-
  Ford algorithm.

• Johnson's algorithm use the technique of reweighting.
Johnson's algorithm for sparse graphs (cont.).

Reweighting
If  has a negative weight edge but no negative weight cycle,
we compute a new set of nonnegative edge weight  that
allow as to use Dijkstra’s algorithm.

The new set of edge must satisfy two condition.
1. () is a shortest path form    ⇔ () is a shortest
   path form    .
2. For all edges (, ), the new weight (, ) is
   nonnegative.
Johnson's algorithm for sparse graphs (cont.).



• Lemma
Give a weighted, directed graph  = (, ) with weight
funtion :  → ℝ be any funtion mapping vertices to real
numbers. For each edge (, ) ∈ , define.

               ,  =  ,  +   −   .
Johnson's algorithm for sparse graphs (cont.).
• Producing no negative weight by reweighting
Johnson's algorithm for sparse graphs (cont.).
Johnson's algorithm for sparse graphs (cont.).