SlideShare une entreprise Scribd logo
1  sur  14
Télécharger pour lire hors ligne
Lecture 15: The Floyd-Warshall
Algorithm
CLRS section 25.2

Outline of this Lecture

Recalling the all-pairs shortest path problem.
 

Recalling the previous two solutions.

The Floyd-Warshall Algorithm.

1

 

 
The All-Pairs Shortest Paths Problem
© ¨¦¤¢
§ ¥ £

¡

 

Given a weighted digraph
with a weight
function
, where 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 cycle 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
Solutions Covered in the Previous Lecture

Solution 1: Assume no negative edges.
Run Dijkstra’s algorithm, times, once with each
vertex as source.
with more sophisticated data
structures.
 

¢

©

 

¢

¦ ¤
§¥£

©

¡

¨
©

¢

 

¢

 

¡

Solution 2: Assume no negative cycles.
Dynamic programming solution, based on a natural decomposition of the problem.
.
using “ repeated squaring”.
©

 

¦ ¤
¥£

¢

 

¢

¡

©



 

¢

¡

This lecture: Assume no negative cycles.
develop another dynamic programming algorithm, the
.
Floyd-Warshall algorithm, with time complexity
Also illustrates that there can be more than one way
of developing a dynamic programming algorithm.
©

¢

 

3

¢

¡
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:

 

 

§

  © ¥  ¢

 © ¥  ¢


!
¤£¡
¤
¡

¡

 ¡ 
  ¡ 

¥¦



¥  ¢ © ¦¨ §
©

¡

¤£¢
¡

Output Format: an
distance
is the distance from vertex to .

§


¥

and
and

 

if
if
if

,
.

where

4

 



 

 

 

¤£¡
Step 1: The Floyd-Warshall Decomposition
are called the
.

£
 

¥

¨ ¨ ¨

¥

¥

¡   §  

 ¡
§ ¥£
¨¦¤ 

¥

¨ ¨ ¨

©

¥

¥

Definition: The vertices
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
.



¨

¨

¥

¨

#
$¥

!


, i.e., no intermediate vertex.
.



matrix

! 0¤£  ¡ 


 
¤£¢
¡

 

 

is the distance from to . So our aim

4 4 4
565¥
!

¥

©

¡

%

for

5

¥

 






 1
32

¤£¡



Subproblems: compute

 

.

¨

 1
32

 


¤£¡

 

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



'
(% ¥

.

£
 ¨¥ 0
§ 
¡

¤£¡

¡
 ¨¥ 
§ 

¨

¨

¥

#
$¥

!


%
%

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

6

.
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
.

%





%



 ¨¥ 
§ 

%

'

¡
 ¨¥ 0
§ 

£

 ¨¥ 
§ 

£

¡

¡
 ¨¥ 
§ 

%
$¥ ¨ ¨ ¨ ¥

!

 

!


Hence the path has length

.

Combining the two cases we get
¨

¦

7

 § ¥ 0


£

¡

¡
 ¨¥ 
§ 
¥

 ¨¥ 
§ 

¤£¡

¥

£
¤¢

¡

¡

 


¤£¡
8
¢

£
 ¨¥ 
§ 
¡

¡
¤£¡
 § ¥ 0 ¥  ¨¥ 

§ 
 ¨¥ 
§ 



.

 

¥

£ ¢

¨ ¨ ¨

¥



!

¤£¡



 )
¤
¡

¡

%

 

¡

¡

¡

 


from

! ¤£¡


for

Compute
 

Bottom:

using

, the weight matrix.

 

Step 3: the Bottom-up Computation
9

!
¥ % 

¥  

 ¨¥ 
§ 
 ¨¥ 
§ 

©
!

!
 § 
¥    ¨¥ 
!  § § %
% ¥   ¨¥ 0


¥ %   § ¥ 0

¡

! !
!
! ¥   
¡ ¥  
!   ¤©
£ ¡
¡ ¥ 
!
¡ ¥  0
  
! % ¥    ¨¥   
§

;

;

;


!

¡

 

 

¨ ¨

¨ ¨

 1

§
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.

10

 

 
! !
!
¥

!
£ ¡
  ¤©
!
! § %  ¡ ¥ !  

¥
%
! %  ¥   © ¥ !  ¥ %  ¡  ¥ !  ¥     ¢

%
 

 

¨ ¨

¨ ¨

§
return

11

;

;

¡

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 .

12



!  ¥ 

£ ¡
¤¢©
The Algorithm for Extracting the Shortest Paths

¥
¦

 


¥  ! ¢

¥  

©

 

if (

)
¡

¥

Path(

)

single edge

£ ¡
¤¢©

output
;
compute the two parts of the path
else

!  £ ©
¡
! ¥  ¥  £ ¡
 ¥   ¤¢© ¥ 

 

Path(
Path(

);
);

13

§
§
14

(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

Contenu connexe

Tendances

Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentationSubid Biswas
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithmtaimurkhan803
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's AlgorithmArijitDhali
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's AlgorithmTamzida_Azad
 
Ford fulkerson
Ford fulkersonFord fulkerson
Ford fulkersonbat coder
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treeoneous
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmGaurav Kolekar
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmA. S. M. Shafi
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest pathArafat Hossan
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )Sazzad Hossain
 

Tendances (20)

Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
(floyd's algm)
(floyd's algm)(floyd's algm)
(floyd's algm)
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Ford fulkerson
Ford fulkersonFord fulkerson
Ford fulkerson
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Strongly connected components
Strongly connected componentsStrongly connected components
Strongly connected components
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Kruskal's algorithm
Kruskal's algorithmKruskal's algorithm
Kruskal's algorithm
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 

En vedette

All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Floyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaFloyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaMalinga Perera
 
Flyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest pathFlyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest pathMadhumita Tamhane
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithmgsp1294
 
Vogel's Approximation Method & Modified Distribution Method
Vogel's Approximation Method & Modified Distribution MethodVogel's Approximation Method & Modified Distribution Method
Vogel's Approximation Method & Modified Distribution MethodKaushik Maitra
 
PUBLIC KEY ENCRYPTION
PUBLIC KEY ENCRYPTIONPUBLIC KEY ENCRYPTION
PUBLIC KEY ENCRYPTIONraf_slide
 

En vedette (8)

All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Floyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaFloyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - Malinga
 
Flyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest pathFlyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest path
 
Floyd warshal
Floyd warshalFloyd warshal
Floyd warshal
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
 
Vogel's Approximation Method & Modified Distribution Method
Vogel's Approximation Method & Modified Distribution MethodVogel's Approximation Method & Modified Distribution Method
Vogel's Approximation Method & Modified Distribution Method
 
PUBLIC KEY ENCRYPTION
PUBLIC KEY ENCRYPTIONPUBLIC KEY ENCRYPTION
PUBLIC KEY ENCRYPTION
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 

Similaire à Floyd warshall-algorithm

Quaternionic rigid meromorphic cocycles
Quaternionic rigid meromorphic cocyclesQuaternionic rigid meromorphic cocycles
Quaternionic rigid meromorphic cocyclesmmasdeu
 
Single source shortestpath
Single source shortestpathSingle source shortestpath
Single source shortestpathJananiJ19
 
Lesson_11_Low-level_Flight_Control hexa copt best ppt.pdf
Lesson_11_Low-level_Flight_Control hexa copt best ppt.pdfLesson_11_Low-level_Flight_Control hexa copt best ppt.pdf
Lesson_11_Low-level_Flight_Control hexa copt best ppt.pdfBorchalaDareje
 
Code_Saturne流体解析入門講習会 講習会資料
Code_Saturne流体解析入門講習会 講習会資料Code_Saturne流体解析入門講習会 講習会資料
Code_Saturne流体解析入門講習会 講習会資料mmer547
 
Smoothed Particle Galerkin Method Formulation.pdf
Smoothed Particle Galerkin Method Formulation.pdfSmoothed Particle Galerkin Method Formulation.pdf
Smoothed Particle Galerkin Method Formulation.pdfkeansheng
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScriptSimon Willison
 
Vehicle Routing Problem using PSO (Particle Swarm Optimization)
Vehicle Routing Problem using PSO (Particle Swarm Optimization)Vehicle Routing Problem using PSO (Particle Swarm Optimization)
Vehicle Routing Problem using PSO (Particle Swarm Optimization)Niharika Varshney
 
Process Algebras and Petri Nets are Discrete Dynamical Systems
Process Algebras and Petri Nets are Discrete Dynamical SystemsProcess Algebras and Petri Nets are Discrete Dynamical Systems
Process Algebras and Petri Nets are Discrete Dynamical SystemsFacultad de Informática UCM
 
Nelson maple pdf
Nelson maple pdfNelson maple pdf
Nelson maple pdfNelsonP23
 
Improving EV Lateral Dynamics Control Using Infinity Norm Approach with Close...
Improving EV Lateral Dynamics Control Using Infinity Norm Approach with Close...Improving EV Lateral Dynamics Control Using Infinity Norm Approach with Close...
Improving EV Lateral Dynamics Control Using Infinity Norm Approach with Close...Valerio Salvucci
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emadKazi Emad
 
Generalised 2nd Order Active Filter Prototype_B
Generalised 2nd Order Active Filter Prototype_BGeneralised 2nd Order Active Filter Prototype_B
Generalised 2nd Order Active Filter Prototype_BAdrian Mostert
 
MinFill_Presentation
MinFill_PresentationMinFill_Presentation
MinFill_PresentationAnna Lasota
 
Contrastive Divergence Learning
Contrastive Divergence LearningContrastive Divergence Learning
Contrastive Divergence Learningpenny 梁斌
 

Similaire à Floyd warshall-algorithm (20)

Graddivcurl1
Graddivcurl1Graddivcurl1
Graddivcurl1
 
4900514.ppt
4900514.ppt4900514.ppt
4900514.ppt
 
Finding Dense Subgraphs
Finding Dense SubgraphsFinding Dense Subgraphs
Finding Dense Subgraphs
 
Quaternionic rigid meromorphic cocycles
Quaternionic rigid meromorphic cocyclesQuaternionic rigid meromorphic cocycles
Quaternionic rigid meromorphic cocycles
 
Single source shortestpath
Single source shortestpathSingle source shortestpath
Single source shortestpath
 
golf
golfgolf
golf
 
Lesson_11_Low-level_Flight_Control hexa copt best ppt.pdf
Lesson_11_Low-level_Flight_Control hexa copt best ppt.pdfLesson_11_Low-level_Flight_Control hexa copt best ppt.pdf
Lesson_11_Low-level_Flight_Control hexa copt best ppt.pdf
 
Code_Saturne流体解析入門講習会 講習会資料
Code_Saturne流体解析入門講習会 講習会資料Code_Saturne流体解析入門講習会 講習会資料
Code_Saturne流体解析入門講習会 講習会資料
 
Smoothed Particle Galerkin Method Formulation.pdf
Smoothed Particle Galerkin Method Formulation.pdfSmoothed Particle Galerkin Method Formulation.pdf
Smoothed Particle Galerkin Method Formulation.pdf
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScript
 
Vehicle Routing Problem using PSO (Particle Swarm Optimization)
Vehicle Routing Problem using PSO (Particle Swarm Optimization)Vehicle Routing Problem using PSO (Particle Swarm Optimization)
Vehicle Routing Problem using PSO (Particle Swarm Optimization)
 
Graphics c
Graphics cGraphics c
Graphics c
 
Process Algebras and Petri Nets are Discrete Dynamical Systems
Process Algebras and Petri Nets are Discrete Dynamical SystemsProcess Algebras and Petri Nets are Discrete Dynamical Systems
Process Algebras and Petri Nets are Discrete Dynamical Systems
 
Nelson maple pdf
Nelson maple pdfNelson maple pdf
Nelson maple pdf
 
Improving EV Lateral Dynamics Control Using Infinity Norm Approach with Close...
Improving EV Lateral Dynamics Control Using Infinity Norm Approach with Close...Improving EV Lateral Dynamics Control Using Infinity Norm Approach with Close...
Improving EV Lateral Dynamics Control Using Infinity Norm Approach with Close...
 
MUMS: Transition & SPUQ Workshop - Gradient-Free Construction of Active Subsp...
MUMS: Transition & SPUQ Workshop - Gradient-Free Construction of Active Subsp...MUMS: Transition & SPUQ Workshop - Gradient-Free Construction of Active Subsp...
MUMS: Transition & SPUQ Workshop - Gradient-Free Construction of Active Subsp...
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emad
 
Generalised 2nd Order Active Filter Prototype_B
Generalised 2nd Order Active Filter Prototype_BGeneralised 2nd Order Active Filter Prototype_B
Generalised 2nd Order Active Filter Prototype_B
 
MinFill_Presentation
MinFill_PresentationMinFill_Presentation
MinFill_Presentation
 
Contrastive Divergence Learning
Contrastive Divergence LearningContrastive Divergence Learning
Contrastive Divergence Learning
 

Plus de Malinga Perera

Prepaire a statement of cost
Prepaire a statement of costPrepaire a statement of cost
Prepaire a statement of costMalinga Perera
 
Financial statement analysis
Financial statement analysisFinancial statement analysis
Financial statement analysisMalinga Perera
 
Example income and-spending
Example  income and-spendingExample  income and-spending
Example income and-spendingMalinga Perera
 
Example cost volume-profit (cvp) analysis
Example cost volume-profit (cvp) analysisExample cost volume-profit (cvp) analysis
Example cost volume-profit (cvp) analysisMalinga Perera
 
4 the theory of the firm and the cost of production
4   the theory of the firm and the cost of production4   the theory of the firm and the cost of production
4 the theory of the firm and the cost of productionMalinga Perera
 
3 demand, supply and the market
3   demand, supply and the market3   demand, supply and the market
3 demand, supply and the marketMalinga Perera
 
2 net present value ex
2   net present value ex2   net present value ex
2 net present value exMalinga Perera
 
2 cash flow and financial statement analysis
2   cash flow and financial statement analysis2   cash flow and financial statement analysis
2 cash flow and financial statement analysisMalinga Perera
 
1 introduction to economics
1   introduction to economics1   introduction to economics
1 introduction to economicsMalinga Perera
 

Plus de Malinga Perera (20)

Oligopoly
OligopolyOligopoly
Oligopoly
 
Prepaire a statement of cost
Prepaire a statement of costPrepaire a statement of cost
Prepaire a statement of cost
 
Financial statement analysis
Financial statement analysisFinancial statement analysis
Financial statement analysis
 
Example
ExampleExample
Example
 
Example monopolistic
Example monopolisticExample monopolistic
Example monopolistic
 
Example income and-spending
Example  income and-spendingExample  income and-spending
Example income and-spending
 
Example cost volume-profit (cvp) analysis
Example cost volume-profit (cvp) analysisExample cost volume-profit (cvp) analysis
Example cost volume-profit (cvp) analysis
 
Example 2
Example 2Example 2
Example 2
 
Example 1
Example 1Example 1
Example 1
 
Example (2)
Example (2)Example (2)
Example (2)
 
Eoq questions
Eoq questionsEoq questions
Eoq questions
 
8 income and spending
8   income and spending8   income and spending
8 income and spending
 
7 standard costing
7   standard costing7   standard costing
7 standard costing
 
6 macroeconomics
6   macroeconomics6   macroeconomics
6 macroeconomics
 
4 the theory of the firm and the cost of production
4   the theory of the firm and the cost of production4   the theory of the firm and the cost of production
4 the theory of the firm and the cost of production
 
3 demand, supply and the market
3   demand, supply and the market3   demand, supply and the market
3 demand, supply and the market
 
2 net present value ex
2   net present value ex2   net present value ex
2 net present value ex
 
2 example ia
2   example ia2   example ia
2 example ia
 
2 cash flow and financial statement analysis
2   cash flow and financial statement analysis2   cash flow and financial statement analysis
2 cash flow and financial statement analysis
 
1 introduction to economics
1   introduction to economics1   introduction to economics
1 introduction to economics
 

Dernier

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 

Dernier (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

Floyd warshall-algorithm

  • 1. Lecture 15: The Floyd-Warshall Algorithm CLRS section 25.2 Outline of this Lecture Recalling the all-pairs shortest path problem.   Recalling the previous two solutions. The Floyd-Warshall Algorithm. 1    
  • 2. The All-Pairs Shortest Paths Problem © ¨¦¤¢ § ¥ £ ¡   Given a weighted digraph with a weight function , where 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 cycle 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. Solutions Covered in the Previous Lecture Solution 1: Assume no negative edges. Run Dijkstra’s algorithm, times, once with each vertex as source. with more sophisticated data structures.   ¢ ©   ¢ ¦ ¤ §¥£ © ¡ ¨ © ¢   ¢   ¡ Solution 2: Assume no negative cycles. Dynamic programming solution, based on a natural decomposition of the problem. . using “ repeated squaring”. ©   ¦ ¤ ¥£ ¢   ¢ ¡ ©   ¢ ¡ This lecture: Assume no negative cycles. develop another dynamic programming algorithm, the . Floyd-Warshall algorithm, with time complexity Also illustrates that there can be more than one way of developing a dynamic programming algorithm. © ¢   3 ¢ ¡
  • 4. 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:     § © ¥ ¢ © ¥ ¢ ! ¤£¡ ¤ ¡ ¡ ¡ ¡ ¥¦ ¥ ¢ © ¦¨ § © ¡ ¤£¢ ¡ Output Format: an distance is the distance from vertex to . § ¥ and and   if if if , . where 4       ¤£¡
  • 5. Step 1: The Floyd-Warshall Decomposition are called the . £   ¥ ¨ ¨ ¨ ¥ ¥ ¡   §   ¡ § ¥£ ¨¦¤  ¥ ¨ ¨ ¨ © ¥ ¥ Definition: The vertices 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 . ¨ ¨ ¥ ¨ # $¥ ! , i.e., no intermediate vertex. . matrix ! 0¤£ ¡   ¤£¢ ¡     is the distance from to . So our aim 4 4 4 565¥ ! ¥ © ¡ % for 5 ¥ 1 32 ¤£¡ Subproblems: compute   . ¨ 1 32 ¤£¡   is to compute ) ¤ Claim: be the ' % (¥   ¤£¡ Let is set to be  
  • 6. 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 ' (% ¥ . £ ¨¥ 0 § ¡ ¤£¡ ¡ ¨¥ § ¨ ¨ ¥ # $¥ ! % % 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 6 .
  • 7. 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 . % % ¨¥ § % ' ¡ ¨¥ 0 § £ ¨¥ § £ ¡ ¡ ¨¥ § % $¥ ¨ ¨ ¨ ¥ !   ! Hence the path has length . Combining the two cases we get ¨ ¦ 7 § ¥ 0 £ ¡ ¡ ¨¥ § ¥ ¨¥ § ¤£¡ ¥ £ ¤¢ ¡ ¡ ¤£¡
  • 8. 8 ¢ £ ¨¥ § ¡ ¡ ¤£¡ § ¥ 0 ¥ ¨¥ § ¨¥ § .   ¥ £ ¢ ¨ ¨ ¨ ¥ ! ¤£¡ ) ¤ ¡ ¡ % ¡ ¡ ¡ from ! ¤£¡ for Compute   Bottom: using , the weight matrix.   Step 3: the Bottom-up Computation
  • 9. 9 ! ¥ % ¥ ¨¥ § ¨¥ § © ! ! § ¥ ¨¥ ! § § % % ¥ ¨¥ 0 ¥ % § ¥ 0 ¡ ! ! ! ! ¥ ¡ ¥ ! ¤© £ ¡ ¡ ¥ ! ¡ ¥ 0 ! % ¥ ¨¥   § ; ; ; ! ¡     ¨ ¨ ¨ ¨ 1 § 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
  • 10. 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. 10    
  • 11. ! ! ! ¥ ! £ ¡ ¤© ! ! § % ¡ ¥ ! ¥ % ! % ¥ © ¥ ! ¥ % ¡ ¥ ! ¥   ¢ %     ¨ ¨ ¨ ¨ § return 11 ; ; ¡ 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
  • 12. 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 . 12 ! ¥ £ ¡ ¤¢©
  • 13. The Algorithm for Extracting the Shortest Paths ¥ ¦   ¥ ! ¢ ¥ ©   if ( ) ¡ ¥ Path( ) single edge £ ¡ ¤¢© output ; compute the two parts of the path else ! £ © ¡ ! ¥ ¥ £ ¡ ¥ ¤¢© ¥   Path( Path( ); ); 13 § §
  • 14. 14 (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