SlideShare uma empresa Scribd logo
1 de 14
Baixar para ler 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

Mais conteúdo relacionado

Mais procurados

Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithmtaimurkhan803
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's AlgorithmTanmay Baranwal
 
Floyd Warshall Algorithm
Floyd Warshall Algorithm Floyd Warshall Algorithm
Floyd Warshall Algorithm Imamul Kadir
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadlineArafat Hossan
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first searchHossain Md Shakhawat
 
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
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic ProgrammingFenil Shah
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searchingKawsar Hamid Sumon
 

Mais procurados (20)

Tsp branch and-bound
Tsp branch and-boundTsp branch and-bound
Tsp branch and-bound
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
 
Floyd Warshall Algorithm
Floyd Warshall Algorithm Floyd Warshall Algorithm
Floyd Warshall Algorithm
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
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
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searching
 

Destaque

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
 

Destaque (8)

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
 

Semelhante a Floyd-Warshall Algorithm Explained

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
 

Semelhante a Floyd-Warshall Algorithm Explained (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
 

Mais 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
 

Mais 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
 

Último

ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Último (20)

ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

Floyd-Warshall Algorithm Explained

  • 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