SlideShare a Scribd company logo
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

Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
Alizay Khan
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
mandlapure
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 

What's hot (20)

Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm
 
Branch & bound
Branch & boundBranch & bound
Branch & bound
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithm
 
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
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
 
Backtracking
BacktrackingBacktracking
Backtracking
 

Viewers also liked

Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
gsp1294
 
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
Kaushik Maitra
 
PUBLIC KEY ENCRYPTION
PUBLIC KEY ENCRYPTIONPUBLIC KEY ENCRYPTION
PUBLIC KEY ENCRYPTION
raf_slide
 

Viewers also liked (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
 

Similar to Floyd warshall-algorithm

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
BorchalaDareje
 
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
Adrian Mostert
 
MinFill_Presentation
MinFill_PresentationMinFill_Presentation
MinFill_Presentation
Anna 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
 
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
 

More from Malinga Perera

Prepaire a statement of cost
Prepaire a statement of costPrepaire a statement of cost
Prepaire a statement of cost
Malinga Perera
 
Financial statement analysis
Financial statement analysisFinancial statement analysis
Financial statement analysis
Malinga Perera
 
Example income and-spending
Example  income and-spendingExample  income and-spending
Example income and-spending
Malinga Perera
 
Example cost volume-profit (cvp) analysis
Example cost volume-profit (cvp) analysisExample cost volume-profit (cvp) analysis
Example cost volume-profit (cvp) analysis
Malinga 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 production
Malinga Perera
 
3 demand, supply and the market
3   demand, supply and the market3   demand, supply and the market
3 demand, supply and the market
Malinga Perera
 
2 net present value ex
2   net present value ex2   net present value ex
2 net present value ex
Malinga 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 analysis
Malinga Perera
 
1 introduction to economics
1   introduction to economics1   introduction to economics
1 introduction to economics
Malinga 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

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security Services
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.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