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   num- bers,   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 b c d 20 12 5 17 8 − 20 5 10 4 4 4 a b c d e without   negative   cost   cycle 6   e   3 3 4 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   sophisticateddata structures. Solution   2:   Assume   no   negative   cycles . Dynamic   programming   solution,   based   on   a   nat- ural   decomposition   of   the   problem. . using   “   repeated   squaring”. This   lecture:   Assume   no   negative   cycles . develop   another   dynamic   programming   algorithm,   the Floyd-Warshallalgorithm,   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: if if if , . Output   Format:   an and and distance where is   the   distance   from   vertex   to   . 4
Step   1:   The   Floyd-Warshall   Decomposition Definition:   The   vertices intermediate   vertices   of   the   path are   called   the . Let be   the   length   of   the   shortest   path   from to   such   that   all   intermediate   vertices   on   the   path ( if   any )   are   in   set is   set   to   be . ,   i.e.,   no   intermediate   vertex. Let be   the matrix . Claim: is   the   distance   from   to   .   So   our   aim is   to   compute . Subproblems:   compute for 5
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   con- tains   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 from   the   set ,   there   are   two   possibilities: 1. is   not   a   vertex   on   the   path, The   shortest   such   path   has   length . 2. is   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 to and   a vertex   .   It   consists   of   a   subpath   from subpath   from to   . Each   subpath   can   only   contain   intermediate   vertices in ,   and   must   be   as   short   as   possible, and . namely   they   have   lengths Hence   the   path   has   length . Combining   the   two   cases   we   get 7
Step   3:   the   Bottom-up   Computation Bottom: ,   the   weight   matrix. using Compute for from . 8
The   Floyd-Warshall   Algorithm:   Version   1 Floyd-Warshall( ) to do initialize for for to do ; ; for to do dynamic   programming to do for for to do if ; ; else ; return ; 9
Comments   on   the   Floyd-Warshall   Algorithm The   algorithm’s   running   time   is   clearly . The   predecessor   pointer to   extract   the   final   path   (see   later   ). Problem:   the   algorithm   uses It   is   possible   to   reduce   this   down   to can   be   used space. space by   keeping   only   one   matrix   instead   of   . Algorithm   is   on   next   page.   Convince   yourself   that it   works. 10
The   Floyd-Warshall   Algorithm:   Version   2 Floyd-Warshall( ) to do initialize for for to do ; ; for to do dynamic   programming to do for for to do if ; ; return ; 11
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 from   to and   the   shortest   path   from to   . 12
The   Algorithm   for   Extracting   the   Shortest   Paths Path( ) if   ( ) single   edge output else Path( Path( ; compute   the   two   parts   of   the   path ); ); 13
Example   of   Extracting   the   Shortest   Paths Find   the   shortest   path   from   vertex   2   to   vertex   3. Path Path Path Path Path Path Path (2,5) (5,4) (4,6) (6,3) 14

More Related Content

PPTX
Greedy Algorithms
PPT
Bellman Ford's Algorithm
PPT
Bellman ford algorithm
PDF
Floyd warshall algorithm
PPTX
Prims and kruskal algorithms
PPTX
Bellman ford Algorithm
PPT
Minimum spanning tree
PPTX
Kruskal's algorithm
Greedy Algorithms
Bellman Ford's Algorithm
Bellman ford algorithm
Floyd warshall algorithm
Prims and kruskal algorithms
Bellman ford Algorithm
Minimum spanning tree
Kruskal's algorithm

What's hot (20)

PPTX
Prim's algorithm
PPTX
Kruskal’s algorithm
PPT
Backtracking
PPT
Single source stortest path bellman ford and dijkstra
PPTX
A presentation on prim's and kruskal's algorithm
PPTX
Dijkstra’S Algorithm
PPT
Spanning trees
PPT
minimum spanning trees Algorithm
PPT
KRUSKAL'S algorithm from chaitra
PPTX
Solving recurrences
PPTX
Ford Fulkerson Algorithm
PPT
Floyd Warshall Algorithm
PPT
Divide and conquer
PPT
Graphs
PDF
Greedy algorithm activity selection fractional
PDF
Minimum spanning tree
PDF
Master theorem
PPTX
Breadth First Search & Depth First Search
PPTX
Bellman ford algorithm
PPT
Greedy Algorihm
Prim's algorithm
Kruskal’s algorithm
Backtracking
Single source stortest path bellman ford and dijkstra
A presentation on prim's and kruskal's algorithm
Dijkstra’S Algorithm
Spanning trees
minimum spanning trees Algorithm
KRUSKAL'S algorithm from chaitra
Solving recurrences
Ford Fulkerson Algorithm
Floyd Warshall Algorithm
Divide and conquer
Graphs
Greedy algorithm activity selection fractional
Minimum spanning tree
Master theorem
Breadth First Search & Depth First Search
Bellman ford algorithm
Greedy Algorihm
Ad

Similar to The Floyd–Warshall algorithm (20)

PDF
Floyd warshall-algorithm
PPT
4900514.ppt
PPT
The Floyd-Warshall of design and analysis of Algorithm.ppt
PDF
My presentation all shortestpath
DOC
Floyd aaaaaa
PDF
White Grey Minimalist Geometric Project Presentation.pdf
PPTX
Floyd's:All pairs shortest path algorithm.pptx
PPTX
All Pair shortest Path.pptx
PDF
Shortest Path in Graph
PPTX
Floyd warshall algorithm and it's applications
PPT
Lec-35Graph - Graph - Copy in Data Structure
PPTX
Batch 5 DAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA.pptx
PPTX
DAA_Presentation - Copy.pptx
PDF
Unit-10 Graphs .pdf
PPTX
LU_30_Dynamic_Programming_Warshal_Floyd_1712140744434.pptx
PDF
21 All Pairs Shortest Path
PPTX
8. Dynamic Prog _Warshall_Floyd Algorithm.pptx
PPTX
Floyd Warshall Algorithm.pptxFgloyd Wars
PPTX
DIJKSTRA_123.pptx
PDF
E017512630
Floyd warshall-algorithm
4900514.ppt
The Floyd-Warshall of design and analysis of Algorithm.ppt
My presentation all shortestpath
Floyd aaaaaa
White Grey Minimalist Geometric Project Presentation.pdf
Floyd's:All pairs shortest path algorithm.pptx
All Pair shortest Path.pptx
Shortest Path in Graph
Floyd warshall algorithm and it's applications
Lec-35Graph - Graph - Copy in Data Structure
Batch 5 DAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA.pptx
DAA_Presentation - Copy.pptx
Unit-10 Graphs .pdf
LU_30_Dynamic_Programming_Warshal_Floyd_1712140744434.pptx
21 All Pairs Shortest Path
8. Dynamic Prog _Warshall_Floyd Algorithm.pptx
Floyd Warshall Algorithm.pptxFgloyd Wars
DIJKSTRA_123.pptx
E017512630
Ad

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Machine learning based COVID-19 study performance prediction
PDF
KodekX | Application Modernization Development
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
cuic standard and advanced reporting.pdf
PPT
Teaching material agriculture food technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Machine learning based COVID-19 study performance prediction
KodekX | Application Modernization Development
Building Integrated photovoltaic BIPV_UPV.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
sap open course for s4hana steps from ECC to s4
Mobile App Security Testing_ A Comprehensive Guide.pdf
cuic standard and advanced reporting.pdf
Teaching material agriculture food technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Encapsulation_ Review paper, used for researhc scholars
The Rise and Fall of 3GPP – Time for a Sabbatical?
Empathic Computing: Creating Shared Understanding
Programs and apps: productivity, graphics, security and other tools
Review of recent advances in non-invasive hemoglobin estimation
MYSQL Presentation for SQL database connectivity
Spectral efficient network and resource selection model in 5G networks
“AI and Expert System Decision Support & Business Intelligence Systems”

The 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 num- bers, 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 b c d 20 12 5 17 8 − 20 5 10 4 4 4 a b c d e without negative cost cycle 6 e 3 3 4 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 sophisticateddata structures. Solution 2: Assume no negative cycles . Dynamic programming solution, based on a nat- ural decomposition of the problem. . using “ repeated squaring”. This lecture: Assume no negative cycles . develop another dynamic programming algorithm, the Floyd-Warshallalgorithm, 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: if if if , . Output Format: an and and distance where is the distance from vertex to . 4
  • 5. Step 1: The Floyd-Warshall Decomposition Definition: The vertices intermediate vertices of the path are called the . Let be the length of the shortest path from to such that all intermediate vertices on the path ( if any ) are in set is set to be . , i.e., no intermediate vertex. Let be the matrix . Claim: is the distance from to . So our aim is to compute . Subproblems: compute for 5
  • 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 con- tains 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 from the set , there are two possibilities: 1. is not a vertex on the path, The shortest such path has length . 2. is 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 to and a vertex . It consists of a subpath from subpath from to . Each subpath can only contain intermediate vertices in , and must be as short as possible, and . namely they have lengths Hence the path has length . Combining the two cases we get 7
  • 8. Step 3: the Bottom-up Computation Bottom: , the weight matrix. using Compute for from . 8
  • 9. The Floyd-Warshall Algorithm: Version 1 Floyd-Warshall( ) to do initialize for for to do ; ; for to do dynamic programming to do for for to do if ; ; else ; return ; 9
  • 10. Comments on the Floyd-Warshall Algorithm The algorithm’s running time is clearly . The predecessor pointer to extract the final path (see later ). Problem: the algorithm uses It is possible to reduce this down to can be used space. space by keeping only one matrix instead of . Algorithm is on next page. Convince yourself that it works. 10
  • 11. The Floyd-Warshall Algorithm: Version 2 Floyd-Warshall( ) to do initialize for for to do ; ; for to do dynamic programming to do for for to do if ; ; return ; 11
  • 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 from to and the shortest path from to . 12
  • 13. The Algorithm for Extracting the Shortest Paths Path( ) if ( ) single edge output else Path( Path( ; compute the two parts of the path ); ); 13
  • 14. Example of Extracting the Shortest Paths Find the shortest path from vertex 2 to vertex 3. Path Path Path Path Path Path Path (2,5) (5,4) (4,6) (6,3) 14