SlideShare una empresa de Scribd logo
Lecture 13: All-Pairs Shortest Paths
CLRS Section 25.1
Outline of this Lecture

 

Introduction of the all-pairs shortest path problem.

 

First solution using Dijkstra’s algorithm.
Assumes no negative weight edges
  ¥   § ¥£
£ ¦£ © ¨£ ¦¤¢

¡

Needs priority queues

A (first) dynamic programming solution.
Only assumes no negative weight cycles.
First version is
  £ ¦£ ¢
 ¥

  ¥  
£ ¦£ © § £ ¦¤¢
¥£

¡

¡

Repeated squaring reduces to

No special data structures needed.
1

 
The All-Pairs Shortest Paths Problem

§ ¦¤¥ ¢
¥£

Given a weighted digraph
with weight
function
, ( is the set of real numbers),
determine the length of the shortest path (i.e., distance) between all pairs of vertices in . Here we
assume that there are no cycles with zero or negative
cost.

¡  





 ¥ © ¨

 

a
12

d

20
6 e 3
5
3
4
17

b

a
8

c

without negative cost cycle

e
4

b

−20
4

5

4
d

10

c

with negative cost cycle

2
Solution 1: Using Dijkstra’s Algorithm
If there are no negative cost edges apply Dijkstra’s algorithm to each vertex (as the source) of the digraph.

¢
§ ¤ £¡ ¢ ¢


 ©

¥£ ¡ ¤
¤
§¡ ¢ ¡

 ©


 © §


§ ¡

§ ¡

£

¡

 ©


and

§ ¡

¡¢

¡

 

¥
¦¡ ¢

£ ¦£
¥

¡

¡ ¡

¡
§ ¡

 ©


§¤ ¢ ¡¢ ¡¢

¡

time algorithm, where



Recall that D’s algorithm runs in
This gives a

.

 

If the digraph is dense, this is an

 

With more advanced (complicated) data structures
time yielding
D’s algorithm runs in
a
final algorithm. For dense
graphs this is
time.

algorithm.

§¤ ¢ ¡

3

 ©


¡¢

¡

§ § ¡¢ ¡
¤
§ §¡ ¢ ¡


 ©

¥ ¡¢

¡
Solution 2: Dynamic Programming

(1) How do we decompose the all-pairs shortest paths
problem into subproblems?

(2) How do we express the optimal solution of a
subproblem in terms of optimal solutions to some
subsubproblems?

(3) How do we use the recursive relation from (2) to
compute the optimal solution in a bottom-up
fashion?

(4) How do we construct all the shortest paths?

4
Solution 2: Input and Output Formats

¥   £ ¡
¦¡ £ ¤¤ £ ¤£ ¢ 

Assume that the graph is represented by an
matrix with the weights of the edges:
and
and


¥ # ! §  £ ¢
¥ ! §  £ ¢

 ¡ 
 ¡ 
£  ¡ 

if
if
if

¡ § ¡

¡

¥

To simplify the notation, we assume that

,
.




¨
§  £ ¢ ¨   ¡ ©¨

¡ § ¡



Output Format: an
matrix
where
is the length of the shortest path from vertex to .


¨ 



5

.



'  
(¨ %

¡ $
Step 1: How to Decompose the Original Problem

 

Subproblems with smaller sizes should be easier
to solve.

 

An optimal solution to a subproblem should be expressed in terms of the optimal solutions to subproblems with smaller sizes.

These are guidelines ONLY.

6
Step 1: Decompose in a Natural Way

¤

 

Define
to be the length of the shortest path
from to that contains at most edges.
Let
be the
matrix
.

'

¡
  
¡ £ £ ¡

¤ 



© ¨   ¨
¡¦¡  

¡ ¥

¤

¢
¦  $

¤

¢
¦  $

 

Question: Which

for

(see next

is easiest to compute?

7



¡ § ¡

Subproblems: compute



¨
£¢  § %
¥

¤

¢
¦  $
¤ 
¢   ¨
£¡

is the true distance from to
page for a proof this conclusion).
from to





 



¤ 

©
¡£ ¨ ¨ 

Proof: We prove that any shortest path
contains at most
edges.



= True Distance from to

¡  ¡

First note that since all cycles have positive weight,
a shortest path can have no cycles (if there were a
cycle, we could remove it and lower the length of the
path).

¡  ¡

A path without cycles can have length at most
(since a longer path must contain some vertex twice,
that is, contain a cycle).

8
A Recursive Formula

k

¨

¨
©§

¦

§ 
   


¡
¤¢
¢£  ¦

¡
¤¢
¥£ 

Case 2: exactly edges
shortest path

 ¨
¢  © ¦¢ ¡
¤
¡ 

¦

¡ 

¡
¤¢
¥£  ¦

¨ 
   


¡
¤¢
¢£ 

¥

Case 1: It has at most
edges.
Then
.
Case 2: It has
edges. Let be the vertex before
on a shortest path.
Then



¨



¥ ¤ 

¡

¡

¥
© £¡
¢   ¨

¤

¢   ¨
£¡

! ¨
¢  © £ !¢ ¨  ¡ £¢   ¨ 
¤
¤

Combining the two cases,

9



¥

¡
¤¢
¥£  ¦

edges

...

¡
¤¢
¢£  ¦

Case 1: at most
shortest path

i

.
¡
¤¢
¢£  ¦



j



...

¦ ¢ ¨ 

i

of length

¤

Consider a shortest path from to

j

01 ! ¨ ¢

¤ 

!
© ¦  ¨ 
¢

)

¨

! '
'% $ #(

¡

¤

¦ ¢ ¨
10



0  !

¨ ¢
, for


£ ¡ ¡ £   ¤£ £ ¡ ¥


¤ 
¤ 

!
© ¦  ¨ 
¢

¨
)

© ¦  $
¢

! '
'% $ #(

from

© ¨
¡¦  $

¤

¨  ¡

¡ 
¢¨

¡
¤

¢
¦  $

 

  $

¤ 

¦ ¢ ¨ 

 

Compute
using

, the weight matrix.

¤ 

Bottom:

Step 3: Bottom-up Computation of
¤ 

© £  $
¨

Example: Bottom-up Computation of
Example
3
8

1

2
11

4

4
3

4

7

is just the weight matrix:
¨
©§
¨

 ¡



¨


¡ ¡
¦


11

¨
' ©¨ %

¡
¤
¥



¡



£






¥

¢

¡

¤ 

  $

¡

¤ 

  $
12


¦


£

¡ ¡
¥

¡
¥

¦



¦

¡

¨
¨

¨
©§

£

¥

¡ ¡
 
¡¡

¢

¡ ¥  $
¤

¡
¡
 ¡

given earlier and the recursive formula,
3
4

11
2

¨ ¢

0
1 !


  $

from

¤ 

¤ 

  $

With

7

4
4
3
8

¤ 

¥  $
¤

!
  ¨ 
)

1

 $ ! #(
%' '

¡ ¥   ¨ 
¤

Example: Computing
0

¤ §

  $

)

¤

!  ¨
 ! ¨ ¢ ¥ ¡
2
11

4

¡

 % ' $ ! #(
'

¤ §

  ¨
¡

4
3

4

7

given earlier and the recursive formula,
¨
©§
¨

¡

¨
¥



¡ ¡
¦



 ¡

¤

¥  $

With

¥  $

3
8

1

from

¤

Example: Computing

¡
¦

£
¥

¡ ¡

 ¡
¦

¥

 
¡¡

¡ ¡
¥

¢

¡

¡

¤ §

  $

gives the distances between any pair of vertices.

13

¤ §

  $
14

;

¡ ¥ ¡
¤

¨ §¡
¨ ¤ ¡ ¡ ¡ ¥ ¡ ¥
¤
! ¨
¢!  ¨
¢  © £¡ ¡ ¨ ¤ ¡
¤
)

¡

¢   ¨
£¡
¢

¢

if (

;

;

;
to

¡

¡

¡

 

¡

to

¡

for

¡ ¡ ¥



¡

¡ 

¡  ¡

¡

 

for

to

© ¨
¡¦  $

¡

¡ 

for

to

¤ 

¡ ¥

for

The Algorithm for Computing
Comments on Solution 2

¢
 ¡§ ¥ ¡
§ § ¡¢

¡

space; how can this be

¡

 

Algorithm uses
reduced down to

 

How can we extract the actual shortest paths from
the solution?

§  ¡¢

Running time
, much worse than the solution using Dijkstra’s algorithm. Can we improve
this?

15

¢

 
Repeated Squaring

¤ 

Observe that we are only interested to find
, all
others
are only auxiliary. Furthermore, since the graph does not have negative cycle,
, for all
.
we have

© £  $
¨

¨ $

¡
¡ ¢

¤ 



©¡£  $ ¡
¨
 © £
¨§¦¥¤¥ ¢ $

using “repeated squar

©
¨§!¥£ ¥ ¢ $ £

¡

¤ 

© ¨
¡¦  $



 ©
  §¦¥£ ¥ ¢ $

 ¤


We can calculate
ing” to find



     ¡
£¨ $

£  ¡

In particular, this implies that

16

 
 
 
£¤ $£¤ $ £¤¥ $
We use the recurrence relation:

, the weight matrix.

¤

! '
!¨
 ¡
% ' $ #¢
¡
¤
)
 
¡
¢¥ $

using

¤   £


¥ $

¤

¡

0  ¡
 !

¢

  $

compute



¨  ¡


¨
¡

¨

¡

¤

¡
¢¥

 
 

For

¤ 

Bottom:

¡

¡
 

  ¨
¡

¤
¥ £

¥ $

Given this relation we can calculate
from
in
time. We can therefore calculate all of

¤



¢

¨
£ 
$ ¡
 ©
  §¦¥£ ¥ $ £

 
¤ ¤£

¤



§ § ¡¢

§ ¡  © § ¡ ¢

  $
 
 
£¤ $ £¤¥ $
¢

¢

in

time, improving our running time.

17
The Floyd-Warshall Algorithm
Step 1 : Decomposition

©
¥
§
¨¡¦ 
¡
 
£  ¦  £   ¤£   £  ¥¤ © ¡ ¡ £

 ¢  £   ¤£ §   £ ¥  


Definition: The vertices
intermediate vertices of the path

are called the

¤ !

 

Let
be the length of the shortest path from
to such that all intermediate vertices on the path
(if any) are in set
.



£

¡
£ ¤£ ¢ 
£

 
¤¤

, i.e., no intermediate vertex.
.

'   ¨  %

matrix

¤ !

¡ § ¡
¨
©¨

is the distance from to . So our aim



for

¡

¤ !

  $

¤

¨
£  $

¨   ¨
£¡

  $

©   ¨
¡

Subproblems: compute

¡ £ £ ¡ £
 

.

18





¤

¤ !

¤

 

is to compute





Claim:

be the

¥

  ¨
¡

Let

is set to be

.

 
Step 2: Structure of shortest paths
Observation 1: A shortest path does not contain the
same vertex twice.
Proof: A path containing the
same vertex twice contains a cycle. Removing cycle
gives a shorter path.
Observation 2: For a shortest path from to such
that any intermediate vertices on the path are chosen
, there are two possibilities:
from the set





¥

.

¤ 

!

©   ¨ 

!
©   ¡
¢  © !  ! ¨ 
¤
!

 
¤¤

¡
£ ¤£ ¢ 
£

2. is a vertex on the path.
The shortest such path has length

¤ 

£

1. is not a vertex on the path,
The shortest such path has length

19

.
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
, and must be as short as possible,
in
namely they have lengths
and
.





¤ 

¤ 





¤ 

!

©   ! 

!

¥ ¡ 

!
©   ¨ 

©   ! 
¢  © !  ! ¨ 
¤
!

¡
£    £ ¢ 

Hence the path has length

.

Combining the two cases we get

20

0

¤ 

!

©   !  ¢

¤ 

!

¨
©   ¨ 
£¤  © !  
!

)

%$ #

¤ !

¡   ¨
21

.
¡

¤ 

©  ¡
 !
!

 ¨
 ¨
¢  © ! !¡ £  © ! ¡
¤
¤
©   $
¤ 

!

¡ £ £ ¡ ¡
¡ ¡
 ¨

%
$ #
 

¤ !

from


' ¨ ¨ %

¡

  $

¤ !

 

 

Compute

¤

for

Bottom:

using

, the weight matrix.

©
  $

Step 3: the Bottom-up Computation
22

'
¡
©  
' 
£  % ¤  !  ¡ ¡ ' 
'
£
¢ ' £ %  © !    ¡ '  £
'  ¤ ©  
£ %¤ !  ¢ ' £
;

;

©  
' 
£ %  ! 
'  ¤ %   
 £ ¤ !
¡

¢

¨
¦ 
¡
£ ¡    ¡ % ¤ 
£ % !  
 %  ¤ ¤ ££
% !   
% ¤ ©   
¤  ¡! ¡¢ 

¢

return

else

;

 

for
to do
for
to do
for
to do
if

;

dynamic programming

¡

¡

¡

¡

¡

¡ 

¡


¡ '  £  %  ¤ ¡£
 
¢ ¡
'  % ¨ ¡ '  % © $
£
£
¡ 

¢

;

;

¡

¡

 

Floyd-Warshall(
)
for
to do
for
to do

initialize

¡ 

¡

¡

¡£ ¨

 

The Floyd-Warshall Algorithm: Version 1
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.

.

'

¡
§ ¥ ¡¢

23

©
£

§ ¥ £ ¡
¨% ¦¤¢ 

¡
§ § ¡¢

¡
24

;

¢

;

¡

;

¡
' 
£ %  ¢ ' £ % 
§ '  £ %  '  £

¡% 
' ¡ ¡
 £ ¡
 
'   %  ¤ ££
¡£' 
£ % 
%  ¢ ' %  ¢
£
¡ 

¢

return

 

for
to do
for
to do
for
to do
if

dynamic programming

;
;

¡

¡

¡

¡

¡

¡

¡ 

¡


¡ '  £  %  ¤ ¡£
 
¢ ¡
'  % ¨ ¡ '  % 
£
¡

¡

¡£ 

¢

 

Floyd-Warshall(
)
for
to do
for
to do

initialize

¡

¡

¡£ ¨

¡ 

 

The Floyd-Warshall Algorithm: Version 2
Extracting the Shortest Paths

©
£

The predecessor pointers
can be used to
extract the final path. The idea is as follows.

'

§ % ¦£ ¢ 
¥ ¡

Whenever we discover that the shortest path from
to passes through an intermediate vertex , we set
.





¡ '  £  %  ¤ ¡£
 

If the shortest path does not pass through any inter.
mediate vertex, then

¡ '
 
£  %  ¤ ££


¢ ¡


§  £ ¢
 
'   %  ¤ ££
£

To find the shortest path from to , we consult
.
If it is nil, then the shortest path is just the edge
.
Otherwise, we recursively compute the shortest path
and the shortest path from
from to
to .

 
'   %  ¤ ¡£
£
25





 
'   %  ¤ ¡£
£
The Algorithm for Extracting the Shortest Paths
)

 

§  ¡ £ ' ¢ 
 
£  %  ¤ ¡£

if (


¢ ¡


£

Path(

)

single edge

output
;
compute the two parts of the path
else
 


 
£ '  £  %    ¤ ££
' 
£  %  ¤ ¡£ £ 

Path(
Path(

);
);

¢
¢

26
27
£

¡ ¡
£  £ ¢  ¢ ¢  ¡
¡ ¡
£  £ ¢  ¢ ¢  ¡
¤
£¡
¢  ¢ ¢  ¡
£
¡
¢  ¢ ¢  ¡
 

£

£

¡
¡
¡

¤
 
' ¦%  ¤ ¡£
' ¤£ %   
¤ ¡£
 
' £ %  ¤ ¡£
' £  %   
¤ ¡£
 
'   £ %  ¤ ¡£
' £ £%   
¤ ¡£
 
' £ £
£ £ %  ¤ ¡£

(4,6)
(6,3)
(2,5)
(5,4)

¥
¥

¥

¡

¥

¡

¡£ 
¡
£ 

¡
¥

£

¡

£

¤
§ £ ¥¢
§ ¤£ ¢
§ £ ¢
§ £  ¢
§  £ £¢
§ £ £¢
§ £ £¢
¥

£

¥

¥

¥

£

Path
Path
Path
Path
Path
Path
Path

£

¤

¥
¥

¤

£
 ¥
¤
 £ ¥
£
 ¥
£
 ¥  
 
 £ ¥
 £

 

  £
  £
  £
  £

£
£



£


£



Find the shortest path from vertex 2 to vertex 3.
Example of Extracting the Shortest Paths

Más contenido relacionado

PPT
Floyd Warshall Algorithm
PPTX
Método Bisección.pptx
PDF
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
PDF
Skiena algorithm 2007 lecture14 shortest path
PPT
The Floyd–Warshall algorithm
PPTX
Floyd Warshall algorithm easy way to compute - Malinga
PDF
All pairs shortest path algorithm
PDF
21 All Pairs Shortest Path
Floyd Warshall Algorithm
Método Bisección.pptx
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
Skiena algorithm 2007 lecture14 shortest path
The Floyd–Warshall algorithm
Floyd Warshall algorithm easy way to compute - Malinga
All pairs shortest path algorithm
21 All Pairs Shortest Path

Similar a All pairts shortest path class 18 (20)

PDF
Flyd+Warshall
PPT
Grafos Iii
PDF
R153041
PPTX
Algoritmo de Floyd - Warshall para imformatica
PPTX
Floyd
PPT
Algoritmo de Dijkstra
DOC
Greedy
PPTX
Presentación_T3_JoséJaén (1).pptx
PPTX
Algoritmo a
PDF
Enfoques
DOCX
Slideshare nelson rodriguez
PDF
grafosalgoritmosfundamentales-170507070811.pdf
PPTX
Algoritmos de Dijkstra, Warshall, Ordenación Topológica.
PDF
Folleto
DOC
Clase5 Ia
DOC
Clase5 Ia
PDF
Elprobdelarutamascortawpv1 121120102007-phpapp02
PPTX
Algoritmo de dijkstra final
Flyd+Warshall
Grafos Iii
R153041
Algoritmo de Floyd - Warshall para imformatica
Floyd
Algoritmo de Dijkstra
Greedy
Presentación_T3_JoséJaén (1).pptx
Algoritmo a
Enfoques
Slideshare nelson rodriguez
grafosalgoritmosfundamentales-170507070811.pdf
Algoritmos de Dijkstra, Warshall, Ordenación Topológica.
Folleto
Clase5 Ia
Clase5 Ia
Elprobdelarutamascortawpv1 121120102007-phpapp02
Algoritmo de dijkstra final
Publicidad

Más de Kumar (20)

PPT
Graphics devices
PPT
Fill area algorithms
PDF
region-filling
PDF
Bresenham derivation
PPT
Bresenham circles and polygons derication
PPTX
Introductionto xslt
PPTX
Extracting data from xml
PPTX
Xml basics
PPTX
XML Schema
PPTX
Publishing xml
PPTX
DTD
PPTX
Applying xml
PPTX
Introduction to XML
PDF
How to deploy a j2ee application
PDF
JNDI, JMS, JPA, XML
PDF
EJB Fundmentals
PDF
JSP and struts programming
PDF
java servlet and servlet programming
PDF
Introduction to JDBC and JDBC Drivers
PDF
Introduction to J2EE
Graphics devices
Fill area algorithms
region-filling
Bresenham derivation
Bresenham circles and polygons derication
Introductionto xslt
Extracting data from xml
Xml basics
XML Schema
Publishing xml
DTD
Applying xml
Introduction to XML
How to deploy a j2ee application
JNDI, JMS, JPA, XML
EJB Fundmentals
JSP and struts programming
java servlet and servlet programming
Introduction to JDBC and JDBC Drivers
Introduction to J2EE
Publicidad

Último (20)

PDF
Los10 Mandamientos de la Actitud Mental Positiva Ccesa007.pdf
PPTX
RESUMENES JULIO - QUIRÓFANO HOSPITAL GENERAL PUYO.pptx
DOCX
PLAN DE AREA DE CIENCIAS SOCIALES TODOS LOS GRUPOS
DOCX
Programa_Sintetico_Fase_4.docx 3° Y 4°..
PDF
Jodorowsky, Alejandro - Manual de Psicomagia.pdf
PDF
Telos 127 Generacion Al fa Beta - fundaciontelefonica
PDF
Los hombres son de Marte - Las mujeres de Venus Ccesa007.pdf
PDF
La Inteligencia Emocional - Fabian Goleman TE4 Ccesa007.pdf
PDF
TALLER DE ESTADISTICA BASICA para principiantes y no tan basicos
PDF
Iniciación Al Aprendizaje Basado En Proyectos ABP Ccesa007.pdf
PPTX
MATEMATICAS GEOMETRICA USO TRANSPORTADOR
PDF
Mi Primer Millon - Poissant - Godefroy Ccesa007.pdf
PDF
La lluvia sabe por qué: una historia sobre amistad, resiliencia y esperanza e...
PDF
Teologia-Sistematica-Por-Lewis-Sperry-Chafer_060044.pdf
PPTX
Historia-Clinica-de-Emergencia-Obstetrica 1.10.pptx
DOCX
PLANES DE área ciencias naturales y aplicadas
DOCX
TEXTO DE TRABAJO DE EDUCACION RELIGIOSA - CUARTO GRADO.docx
PDF
ciencia_tecnologia_sociedad Mitcham Carl. (1994)..pdf
PDF
Esc. Sab. Lección 7. El pan y el agua de vida.pdf
PDF
Nadie puede salvarte excepto Tú - Madame Rouge Ccesa007.pdf
Los10 Mandamientos de la Actitud Mental Positiva Ccesa007.pdf
RESUMENES JULIO - QUIRÓFANO HOSPITAL GENERAL PUYO.pptx
PLAN DE AREA DE CIENCIAS SOCIALES TODOS LOS GRUPOS
Programa_Sintetico_Fase_4.docx 3° Y 4°..
Jodorowsky, Alejandro - Manual de Psicomagia.pdf
Telos 127 Generacion Al fa Beta - fundaciontelefonica
Los hombres son de Marte - Las mujeres de Venus Ccesa007.pdf
La Inteligencia Emocional - Fabian Goleman TE4 Ccesa007.pdf
TALLER DE ESTADISTICA BASICA para principiantes y no tan basicos
Iniciación Al Aprendizaje Basado En Proyectos ABP Ccesa007.pdf
MATEMATICAS GEOMETRICA USO TRANSPORTADOR
Mi Primer Millon - Poissant - Godefroy Ccesa007.pdf
La lluvia sabe por qué: una historia sobre amistad, resiliencia y esperanza e...
Teologia-Sistematica-Por-Lewis-Sperry-Chafer_060044.pdf
Historia-Clinica-de-Emergencia-Obstetrica 1.10.pptx
PLANES DE área ciencias naturales y aplicadas
TEXTO DE TRABAJO DE EDUCACION RELIGIOSA - CUARTO GRADO.docx
ciencia_tecnologia_sociedad Mitcham Carl. (1994)..pdf
Esc. Sab. Lección 7. El pan y el agua de vida.pdf
Nadie puede salvarte excepto Tú - Madame Rouge Ccesa007.pdf

All pairts shortest path class 18

  • 1. Lecture 13: All-Pairs Shortest Paths CLRS Section 25.1 Outline of this Lecture   Introduction of the all-pairs shortest path problem.   First solution using Dijkstra’s algorithm. Assumes no negative weight edges ¥ § ¥£ £ ¦£ © ¨£ ¦¤¢ ¡ Needs priority queues A (first) dynamic programming solution. Only assumes no negative weight cycles. First version is £ ¦£ ¢ ¥ ¥ £ ¦£ © § £ ¦¤¢ ¥£ ¡ ¡ Repeated squaring reduces to No special data structures needed. 1  
  • 2. The All-Pairs Shortest Paths Problem § ¦¤¥ ¢ ¥£ Given a weighted digraph with weight function , ( is the set of real numbers), determine the length of the shortest path (i.e., distance) between all pairs of vertices in . Here we assume that there are no cycles with zero or negative cost. ¡   ¥ © ¨   a 12 d 20 6 e 3 5 3 4 17 b a 8 c without negative cost cycle e 4 b −20 4 5 4 d 10 c with negative cost cycle 2
  • 3. Solution 1: Using Dijkstra’s Algorithm If there are no negative cost edges apply Dijkstra’s algorithm to each vertex (as the source) of the digraph. ¢ § ¤ £¡ ¢ ¢ © ¥£ ¡ ¤ ¤ §¡ ¢ ¡ © © § § ¡ § ¡ £ ¡ © and § ¡ ¡¢ ¡   ¥ ¦¡ ¢ £ ¦£ ¥ ¡ ¡ ¡ ¡ § ¡ © §¤ ¢ ¡¢ ¡¢ ¡ time algorithm, where Recall that D’s algorithm runs in This gives a .   If the digraph is dense, this is an   With more advanced (complicated) data structures time yielding D’s algorithm runs in a final algorithm. For dense graphs this is time. algorithm. §¤ ¢ ¡ 3 © ¡¢ ¡ § § ¡¢ ¡ ¤ § §¡ ¢ ¡ © ¥ ¡¢ ¡
  • 4. Solution 2: Dynamic Programming (1) How do we decompose the all-pairs shortest paths problem into subproblems? (2) How do we express the optimal solution of a subproblem in terms of optimal solutions to some subsubproblems? (3) How do we use the recursive relation from (2) to compute the optimal solution in a bottom-up fashion? (4) How do we construct all the shortest paths? 4
  • 5. Solution 2: Input and Output Formats ¥ £ ¡ ¦¡ £ ¤¤ £ ¤£ ¢  Assume that the graph is represented by an matrix with the weights of the edges: and and ¥ # ! § £ ¢ ¥ ! § £ ¢ ¡ ¡ £ ¡ if if if ¡ § ¡ ¡ ¥ To simplify the notation, we assume that , . ¨ § £ ¢ ¨ ¡ ©¨ ¡ § ¡ Output Format: an matrix where is the length of the shortest path from vertex to . ¨ 5 . ' (¨ % ¡ $
  • 6. Step 1: How to Decompose the Original Problem   Subproblems with smaller sizes should be easier to solve.   An optimal solution to a subproblem should be expressed in terms of the optimal solutions to subproblems with smaller sizes. These are guidelines ONLY. 6
  • 7. Step 1: Decompose in a Natural Way ¤   Define to be the length of the shortest path from to that contains at most edges. Let be the matrix . ' ¡ ¡ £ £ ¡ ¤ © ¨  ¨ ¡¦¡   ¡ ¥ ¤ ¢ ¦  $ ¤ ¢ ¦  $   Question: Which for (see next is easiest to compute? 7 ¡ § ¡ Subproblems: compute ¨ £¢  § % ¥ ¤ ¢ ¦  $ ¤ ¢  ¨ £¡ is the true distance from to page for a proof this conclusion).
  • 8. from to   ¤ © ¡£ ¨ ¨ Proof: We prove that any shortest path contains at most edges. = True Distance from to ¡ ¡ First note that since all cycles have positive weight, a shortest path can have no cycles (if there were a cycle, we could remove it and lower the length of the path). ¡ ¡ A path without cycles can have length at most (since a longer path must contain some vertex twice, that is, contain a cycle). 8
  • 9. A Recursive Formula k ¨ ¨ ©§ ¦ § ¡ ¤¢ ¢£  ¦ ¡ ¤¢ ¥£  Case 2: exactly edges shortest path  ¨ ¢ © ¦¢ ¡ ¤ ¡ ¦ ¡ ¡ ¤¢ ¥£  ¦ ¨ ¡ ¤¢ ¢£  ¥ Case 1: It has at most edges. Then . Case 2: It has edges. Let be the vertex before on a shortest path. Then ¨ ¥ ¤ ¡ ¡ ¥ © £¡ ¢  ¨ ¤ ¢  ¨ £¡ ! ¨ ¢ © £ !¢ ¨ ¡ £¢   ¨ ¤ ¤ Combining the two cases, 9 ¥ ¡ ¤¢ ¥£  ¦ edges ... ¡ ¤¢ ¢£  ¦ Case 1: at most shortest path i . ¡ ¤¢ ¢£  ¦ j ... ¦ ¢ ¨ i of length ¤ Consider a shortest path from to j 01 ! ¨ ¢ ¤ ! © ¦  ¨ ¢ ) ¨ ! ' '% $ #( ¡ ¤ ¦ ¢ ¨
  • 10. 10 0 ! ¨ ¢ , for £ ¡ ¡ £ ¤£ £ ¡ ¥ ¤ ¤ ! © ¦  ¨ ¢ ¨ ) © ¦  $ ¢ ! ' '% $ #( from © ¨ ¡¦  $ ¤ ¨  ¡ ¡ ¢¨ ¡ ¤ ¢ ¦  $     $ ¤ ¦ ¢ ¨   Compute using , the weight matrix. ¤ Bottom: Step 3: Bottom-up Computation of
  • 11. ¤ © £  $ ¨ Example: Bottom-up Computation of Example 3 8 1 2 11 4 4 3 4 7 is just the weight matrix: ¨ ©§ ¨  ¡ ¨ ¡ ¡ ¦ 11 ¨ ' ©¨ % ¡ ¤ ¥ ¡ £ ¥ ¢ ¡ ¤   $ ¡ ¤   $
  • 12. 12 ¦ £ ¡ ¡ ¥ ¡ ¥ ¦ ¦ ¡ ¨ ¨ ¨ ©§ £ ¥ ¡ ¡   ¡¡ ¢ ¡ ¥  $ ¤ ¡ ¡  ¡ given earlier and the recursive formula, 3 4 11 2 ¨ ¢ 0 1 !   $ from ¤ ¤   $ With 7 4 4 3 8 ¤ ¥  $ ¤ !   ¨ ) 1 $ ! #( %' ' ¡ ¥   ¨ ¤ Example: Computing
  • 13. 0 ¤ §   $ ) ¤ !  ¨ ! ¨ ¢ ¥ ¡ 2 11 4 ¡ % ' $ ! #( ' ¤ §  ¨ ¡ 4 3 4 7 given earlier and the recursive formula, ¨ ©§ ¨ ¡ ¨ ¥ ¡ ¡ ¦  ¡ ¤ ¥  $ With ¥  $ 3 8 1 from ¤ Example: Computing ¡ ¦ £ ¥ ¡ ¡ ¡ ¦ ¥   ¡¡ ¡ ¡ ¥ ¢ ¡ ¡ ¤ §   $ gives the distances between any pair of vertices. 13 ¤ §   $
  • 14. 14 ; ¡ ¥ ¡ ¤ ¨ §¡ ¨ ¤ ¡ ¡ ¡ ¥ ¡ ¥ ¤ ! ¨ ¢!  ¨ ¢ © £¡ ¡ ¨ ¤ ¡ ¤ ) ¡ ¢  ¨ £¡ ¢ ¢ if ( ; ; ; to ¡ ¡ ¡   ¡ to ¡ for ¡ ¡ ¥ ¡ ¡ ¡ ¡ ¡   for to © ¨ ¡¦  $ ¡ ¡ for to ¤ ¡ ¥ for The Algorithm for Computing
  • 15. Comments on Solution 2 ¢  ¡§ ¥ ¡ § § ¡¢ ¡ space; how can this be ¡   Algorithm uses reduced down to   How can we extract the actual shortest paths from the solution? § ¡¢ Running time , much worse than the solution using Dijkstra’s algorithm. Can we improve this? 15 ¢  
  • 16. Repeated Squaring ¤ Observe that we are only interested to find , all others are only auxiliary. Furthermore, since the graph does not have negative cycle, , for all . we have © £  $ ¨ ¨ $ ¡ ¡ ¢ ¤ ©¡£  $ ¡ ¨ © £ ¨§¦¥¤¥ ¢ $ using “repeated squar © ¨§!¥£ ¥ ¢ $ £ ¡ ¤ © ¨ ¡¦  $ © §¦¥£ ¥ ¢ $ ¤ We can calculate ing” to find     ¡ £¨ $ £ ¡ In particular, this implies that 16       £¤ $£¤ $ £¤¥ $
  • 17. We use the recurrence relation: , the weight matrix. ¤ ! ' !¨  ¡ % ' $ #¢ ¡ ¤ )   ¡ ¢¥ $ using ¤ £ ¥ $ ¤ ¡ 0 ¡  ! ¢   $ compute ¨  ¡ ¨ ¡ ¨ ¡ ¤ ¡ ¢¥     For ¤ Bottom: ¡ ¡    ¨ ¡ ¤ ¥ £ ¥ $ Given this relation we can calculate from in time. We can therefore calculate all of ¤ ¢ ¨ £  $ ¡ © §¦¥£ ¥ $ £ ¤ ¤£ ¤ § § ¡¢ § ¡ © § ¡ ¢   $     £¤ $ £¤¥ $ ¢ ¢ in time, improving our running time. 17
  • 18. The Floyd-Warshall Algorithm Step 1 : Decomposition © ¥ § ¨¡¦  ¡   £ ¦  £ ¤£   £ ¥¤ © ¡ ¡ £ ¢  £ ¤£ §   £ ¥   Definition: The vertices intermediate vertices of the path are called the ¤ !   Let be the length of the shortest path from to such that all intermediate vertices on the path (if any) are in set . £ ¡ £ ¤£ ¢  £ ¤¤ , i.e., no intermediate vertex. . '   ¨ % matrix ¤ ! ¡ § ¡ ¨ ©¨ is the distance from to . So our aim for ¡ ¤ !   $ ¤ ¨ £  $ ¨  ¨ £¡   $ ©  ¨ ¡ Subproblems: compute ¡ £ £ ¡ £ . 18 ¤ ¤ ! ¤   is to compute Claim: be the ¥  ¨ ¡ Let is set to be .  
  • 19. Step 2: Structure of shortest paths Observation 1: A shortest path does not contain the same vertex twice. Proof: A path containing the same vertex twice contains a cycle. Removing cycle gives a shorter path. Observation 2: For a shortest path from to such that any intermediate vertices on the path are chosen , there are two possibilities: from the set ¥ . ¤ ! ©   ¨ ! ©  ¡ ¢ © !  ! ¨ ¤ ! ¤¤ ¡ £ ¤£ ¢  £ 2. is a vertex on the path. The shortest such path has length ¤ £ 1. is not a vertex on the path, The shortest such path has length 19 .
  • 20. 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 , and must be as short as possible, in namely they have lengths and . ¤ ¤ ¤ ! ©  ! ! ¥ ¡ ! ©   ¨ ©  ! ¢ © !  ! ¨ ¤ ! ¡ £ £ ¢  Hence the path has length . Combining the two cases we get 20 0 ¤ ! ©  ! ¢ ¤ ! ¨ ©   ¨ £¤ © !  ! ) %$ # ¤ ! ¡   ¨
  • 21. 21 . ¡ ¤ © ¡  ! !  ¨  ¨ ¢ © ! !¡ £ © ! ¡ ¤ ¤ ©   $ ¤ ! ¡ £ £ ¡ ¡ ¡ ¡  ¨ % $ #   ¤ ! from ' ¨ ¨ % ¡   $ ¤ !     Compute ¤ for Bottom: using , the weight matrix. ©   $ Step 3: the Bottom-up Computation
  • 22. 22 ' ¡ ©   ' £ % ¤ ! ¡ ¡ ' ' £ ¢ ' £ % © !   ¡ ' £ ' ¤ ©   £ %¤ ! ¢ ' £ ; ; ©   ' £ % ! ' ¤ %   £ ¤ ! ¡ ¢ ¨ ¦  ¡ £ ¡   ¡ % ¤ £ % !  % ¤ ¤ ££ % !   % ¤ ©   ¤ ¡! ¡¢ ¢ return else ;   for to do for to do for to do if ; dynamic programming ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ' £ % ¤ ¡£   ¢ ¡ ' % ¨ ¡ ' % © $ £ £ ¡ ¢ ; ; ¡ ¡   Floyd-Warshall( ) for to do for to do initialize ¡ ¡ ¡ ¡£ ¨   The Floyd-Warshall Algorithm: Version 1
  • 23. 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. . ' ¡ § ¥ ¡¢ 23 © £ § ¥ £ ¡ ¨% ¦¤¢  ¡ § § ¡¢ ¡
  • 24. 24 ; ¢ ; ¡ ; ¡ ' £ % ¢ ' £ % § ' £ % ' £ ¡% ' ¡ ¡ £ ¡   ' % ¤ ££ ¡£' £ % % ¢ ' % ¢ £ ¡ ¢ return   for to do for to do for to do if dynamic programming ; ; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ' £ % ¤ ¡£   ¢ ¡ ' % ¨ ¡ ' % £ ¡ ¡ ¡£ ¢   Floyd-Warshall( ) for to do for to do initialize ¡ ¡ ¡£ ¨ ¡   The Floyd-Warshall Algorithm: Version 2
  • 25. Extracting the Shortest Paths © £ The predecessor pointers can be used to extract the final path. The idea is as follows. ' § % ¦£ ¢  ¥ ¡ Whenever we discover that the shortest path from to passes through an intermediate vertex , we set . ¡ ' £ % ¤ ¡£   If the shortest path does not pass through any inter. mediate vertex, then ¡ '   £ % ¤ ££ ¢ ¡ § £ ¢   ' % ¤ ££ £ To find the shortest path from to , we consult . If it is nil, then the shortest path is just the edge . Otherwise, we recursively compute the shortest path and the shortest path from from to to .   ' % ¤ ¡£ £ 25   ' % ¤ ¡£ £
  • 26. The Algorithm for Extracting the Shortest Paths )   § ¡ £ ' ¢   £ % ¤ ¡£ if ( ¢ ¡ £ Path( ) single edge output ; compute the two parts of the path else     £ ' £ %   ¤ ££ ' £ % ¤ ¡£ £ Path( Path( ); ); ¢ ¢ 26
  • 27. 27 £ ¡ ¡ £  £ ¢  ¢ ¢ ¡ ¡ ¡ £  £ ¢  ¢ ¢ ¡ ¤ £¡ ¢  ¢ ¢ ¡ £ ¡ ¢  ¢ ¢ ¡   £ £ ¡ ¡ ¡ ¤   ' ¦% ¤ ¡£ ' ¤£ %   ¤ ¡£   ' £ % ¤ ¡£ ' £  %   ¤ ¡£   '   £ % ¤ ¡£ ' £ £%   ¤ ¡£   ' £ £ £ £ % ¤ ¡£ (4,6) (6,3) (2,5) (5,4) ¥ ¥ ¥ ¡ ¥ ¡ ¡£  ¡ £  ¡ ¥ £ ¡ £ ¤ § £ ¥¢ § ¤£ ¢ § £ ¢ § £  ¢ §  £ £¢ § £ £¢ § £ £¢ ¥ £ ¥ ¥ ¥ £ Path Path Path Path Path Path Path £ ¤ ¥ ¥ ¤ £ ¥ ¤ £ ¥ £ ¥ £ ¥   £ ¥ £     £   £   £   £ £ £ £ £ Find the shortest path from vertex 2 to vertex 3. Example of Extracting the Shortest Paths