Floyd’s algorithm


Floyd’s algorithmMathworldPlanetmath is also known as the all pairs shortest path algorithm. It will compute the shortest path between all possible pairs of vertices in a (possibly weighted) graph or digraphMathworldPlanetmath simultaneously in O⁢(n3) time (where n is the number of vertices in the graph).

Algorithm Floyd(V)
Input: A weighted graph or digraph with vertices V
Output: A matrix c⁢o⁢s⁢t of shortest paths and a matrix p⁢r⁢e⁢d of predecessors in the shortest path

for (a,b)∈V2 do
          if a⁢d⁢j⁢a⁢c⁢e⁢n⁢t⁢(a,b) then

c⁢o⁢s⁢t⁢(a,b)←w⁢e⁢i⁢g⁢h⁢t⁢(a,b)

p⁢r⁢e⁢d⁢(a,b)←a else

c⁢o⁢s⁢t⁢(a,b)←∞

p⁢r⁢e⁢d⁢(a,b)←n⁢u⁢l⁢l

for c∈V do
          for (a,b)∈V2 do

if c⁢o⁢s⁢t⁢(a,c)<∞ and c⁢o⁢s⁢t⁢(c,b)<∞ then

if c⁢o⁢s⁢t⁢(a,b)=∞ or c⁢o⁢s⁢t⁢(a,c)+c⁢o⁢s⁢t⁢(c,b)<c⁢o⁢s⁢t⁢(a,b) then

c⁢o⁢s⁢t⁢(a,b)←c⁢o⁢s⁢t⁢(a,c)+c⁢o⁢s⁢t⁢(c,b)

p⁢r⁢e⁢d⁢(a,b)←p⁢r⁢e⁢d⁢(c,b)

Title Floyd’s algorithm
Canonical name FloydsAlgorithm
Date of creation 2013-03-22 12:16:29
Last modified on 2013-03-22 12:16:29
Owner vampyr (22)
Last modified by vampyr (22)
Numerical id 6
Author vampyr (22)
Entry type Algorithm
Classification msc 68R10
Classification msc 05C38
Classification msc 05C85
Synonym all pairs shortest path algorithm