SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

03 algorithm properties
03 algorithm properties03 algorithm properties
03 algorithm properties
Lincoln School
 

Was ist angesagt? (20)

Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Graph algorithm
Graph algorithmGraph algorithm
Graph algorithm
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Collision in Hashing.pptx
Collision in Hashing.pptxCollision in Hashing.pptx
Collision in Hashing.pptx
 
A* algorithm
A* algorithmA* algorithm
A* algorithm
 
Lecture 25 hill climbing
Lecture 25 hill climbingLecture 25 hill climbing
Lecture 25 hill climbing
 
Ai 8 puzzle problem
Ai 8 puzzle problemAi 8 puzzle problem
Ai 8 puzzle problem
 
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
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
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Introduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of OptimalityIntroduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of Optimality
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Hamiltonian path
Hamiltonian pathHamiltonian path
Hamiltonian path
 
03 algorithm properties
03 algorithm properties03 algorithm properties
03 algorithm properties
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 

Andere mochten auch

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
 

Andere mochten auch (9)

Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall 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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Kürzlich hochgeladen (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 

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