Floyd Warshall Algorithm
Floyd Warshall Algorithm
ON
BY
(Dec 2019)
Introduction:
The Floyd–Warshall algorithm is an example of dynamic programming, and was published in its
currently recognized form by Robert Floyd in 1962. However, it is essentially the same as
algorithms previously published by Bernard Roy in 1959 and also by Stephen Warshall in
1962 for finding the transitive closure of a graph, and is closely related to Kleene's
algorithm (published in 1956) for converting a deterministic finite automaton into a regular
expression. The modern formulation of the algorithm as three nested for-loops was first described
by Peter Ingerman, also in 1962.
2
Example:
3
4
5
Algorithm:
The function Floyd() is called in the command window. The program asks for the D-matrix,
starting and ending node for which to find the shortest distance and path, and also whether to
display the D and S matrix solution and iterations/steps. The D-matrix must be enter with
diagonals as ’0’ instead of ‘-‘. In the code, after the function is prompts are anwered, a nested
for loop is used so as to create the initial S-matrix. In the nested for loop all the rows are
assigned values of 1 to the size of the D-matrix (corresponding column value), then if the row
and column values/positions are the same then 0 is assigned to that position (diagonal
elements).
The nested for loop is closed. New element named previous_D is defined and takes the value of
D. A for loop is opened which loops from 1 to the size of the inputted D-matrix. In that loop, we
replace distance [i][j] with distance [i][k] + distance [j][k] if distance [i][j] > distance [i][k] +
[k][j] (where K=K th vertex, i=ith row and j=j th column). If the condition is not satisfied then
distance [i][j] remains unchanged. After that in the S-matrix each element is checked for whether
the new D value is lesser the original one using D and previous_D, if so the that position in the
S-matrix is given the value of the current loop/iteration. previous_D takes the updated values of
6
D-matrix. The loop is closed and the loop keeps repeating until the last iteration.
For finding the shortest distance we can take the value corresponding to the initial and final
nodes.But for the path, we use a matrix F to store the backtracked path data. Use for loop, give
F(1) value of final node, input the other values using the loop, if the final path value is same as
the corresponding column, the loop ends immediately. The F matrix is then rectified for any
repeating path values by using a for loop. Then finally the distance and path values are
displayed, with for the path the F variable being printed in reverse after the initial variables has
been printed. If the user had enter ‘Y’ for the last prompt, the program would have displayed the
iterations/steps also. Instead of the traditional ‘-‘given to the diagonal elements, this program
gives the diagonal elements a value of ‘0’
Numerical Implementation:
function Floyd()
prompt1 = 'Enter the D-matrix: ';
D=input(prompt1);
prompt2 ='Enter the starting node: ';
initial_node=input(prompt2);
prompt3 ='Enter the ending node: ';
final_node=input(prompt3);
prompt4 = 'Would you like to see the steps/iterations for the entire
network? [Answer with Y/N]: ';
str = input(prompt4,'s');
if isempty(str)
str = 'N';
end
if str=='Y'
show=1;
else
show=0;
end
for i = 1:length(D)
for j = 1:length(D)
S(i,j)=j;
if i==j
S(j,j)= 0;
end
end
end
7
if show ~=0
fprintf('Intial matrices:')
D
S
end
previous_D = D;
for k = 1:length(D)
D = min(D,D(:,k) + D(k,:));
S(D<previous_D) = k;
previous_D = D;
if show ~=0
fprintf('Interation Number %d:',k)
D
S
end
end
t=0;
final=final_node;
for i=2:length(D)
F(1)=final_node;
if S(initial_node,final)==final
F(i)=S(initial_node,final);
t=1;
else
F(i)=S(initial_node,final);
final=S(initial_node,final);
end
if t==1
break
end
end
shortest_distance = D(initial_node,final_node);
b=0;
for i=1:length(F)-1
if F(i)==F(i+1)
for j=i:length(F)-1
F(j)=F(j+1);
end
b=b+1;
end
end
8
fprintf('The shortest distance from %d to %d is %d and the shortest
path is %d',initial_node,final_node,shortest_distance,initial_node)
for i=length(F)-b:-1:1
fprintf('->%d',F(i))
end
end
Example problems:
Problem 1:
>> Floyd()
Enter the D-matrix: [ 0 8 3 5 Inf; 8 0 2 Inf 5; Inf 1 0 3 4; 6 Inf Inf 0 7 ; Inf 5 Inf
Inf 0;]
Would you like to see the steps/iterations for the entire network? [Answer with
9
Y/N]: N The shortest distant from 3 to 1 is 9 and the shortest path is 3->2->1
Problem 2:
>> Floyd()
Enter the D-matrix: [0 5 3 Inf Inf Inf Inf; 5 0 1 5 2 Inf Inf; 3 1 0 7 Inf Inf 12; Inf 5 7 0 3 Inf 3; Inf
2 Inf 3 0 1 Inf; Inf Inf Inf 1 1 0 Inf; Inf Inf 12 3 Inf 4 0;]
Enter the starting node: 2
Enter the ending node: 7
Would you like to see the steps/iterations for the entire network? [Answer with
D=
5 0 1 5 2 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
6 2 3 3 0 1 6
7 3 4 1 1 0 4
12 8 9 3 6 4 0
S=
0 3 3 3 3 5 4
16
3 0 3 4 5 5 4
1 2 0 2 2 5 4
3 2 2 0 5 5 7
3 2 2 4 0 6 4
5 5 5 4 5 0 4
4 4 4 4 4 6 0
Interation Number
6: D =
0 4 3 8 6 7 11
4 0 1 4 2 3 7
3 1 0 5 3 4 8
9 5 6 0 3 4 3
6 2 3 2 0 1 5
7 3 4 1 1 0 4
11 7 8 3 5 4 0
0 3 3 6 3 5 6
3 0 3 6 5 5 6
1 2 0 6 2 5 6
3 2 2 0 5 5 7
3 2 2 6 0 6 6
17
5 5 5 4 5 0 4
6 6 6 4 6 6 0
Interation Number
7: D =
0 4 3 8 6 7 11
4 0 1 4 2 3 7
3 1 0 5 3 4 8
9 5 6 0 3 4 3
6 2 3 2 0 1 5
7 3 4 1 1 0 4
11 7 8 3 5 4 0
S=
0 3 3 6 3 5 6
3 0 3 6 5 5 6
1 2 0 6 2 5 6
3 2 2 0 5 5 7
3 2 2 6 0 6 6
5 5 5 4 5 0 4
6 6 6 4 6 6 0
19
The shortest distance from 2 to 7 is 7 and the shortest path is 2->5->6->7
Problem 3:
>> Floyd()
Enter the D-matrix: [ 0 3 10 Inf Inf; 3 0 Inf 5 Inf; 10 Inf 0 6 15; Inf 5 6 0 4; Inf Inf
Would you like to see the steps/iterations for the entire network? [Answer with
Y/N]: N The shortest distance from 4 to 5 is 4 and the shortest path is 4->5
19