SlideShare a Scribd company logo
1 of 14
Download to read offline
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

More Related Content

What's hot

Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithmsSaga Valsalan
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra Sahil Kumar
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithmmansab MIRZA
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmA. S. M. Shafi
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmIfad Rahman
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithmtaimurkhan803
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqmontaser185
 
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
 
minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm sachin varun
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDAAshish Duggal
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithmjyothimonc
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph AJAL A J
 

What's hot (20)

Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Kruskal's algorithm
Kruskal's algorithmKruskal's algorithm
Kruskal's algorithm
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's Algorithm
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
Heaps
HeapsHeaps
Heaps
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraq
 
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
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
PRIM'S ALGORITHM
PRIM'S ALGORITHMPRIM'S ALGORITHM
PRIM'S ALGORITHM
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 

Viewers also liked

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
 

Viewers also liked (9)

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
 
(floyd's algm)
(floyd's algm)(floyd's algm)
(floyd's algm)
 
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
 

Similar to Floyd warshall-algorithm

Code of the Tie
Code of the TieCode of the Tie
Code of the TieRob Sawyer
 
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
 

Similar to 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
 
Code of the Tie
Code of the TieCode of the Tie
Code of the Tie
 
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
 

More from 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
 

More from 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
 

Recently uploaded

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
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
 
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
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
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
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
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
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
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
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 

Recently uploaded (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
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
 
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
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
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
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
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.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.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