You are on page 1of 4

Floyd’s algorithm

Floyd’s algorithm allows to determine the shortest route between any two nodes in the
network instead of Dijkstra’s that only determines the shortest route from the source node and
any node in the network.

dik dkj

i j
dij

Figure 5.3.2 Floyd’s algorithm


Consider the nodes i, j and k as illustrated in Figure 5.3.2. Floyd’s algorithm is based on
evaluating the inequality below and it is shorter to reach j passing through k if
dik + dkj < dij

If true, then it is optimal to reach node i from j with the indirect route i  k  j than with the
direct route i  j .
Step 0: Define the starting distance matrix D0 and the sequence starting matrix S0 (all
diagonal elements are blocked). Set k = 1

1 2 …… j …… n
1 - d12 …… d1j …… d1n
2 d21 - …… d2j d2n
D0 = ⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
I di2 di2 …… dij …… din
⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
N dn1 dn2 …… dnj …… -

1 2 …… j …… n
1 - 2 …… j …… n
2 1 - …… j n
⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
S0 = I 1 di2 …… j …… n
⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
N 1 2 …… j …… -
Step k: Set row k and column k as pivot row and pivot column. For each element dij in
Dk-1, check the condition dik + dkj < dij. If the condition is satisfied then

a. Create the distance matrix Dk for iteration k replacing in Dk-1 for those elements that
satisfied the inequality with dik + dkj.
b. Create the sequence matrix Sk by replacing sij in Sk-1 with k. Set k = k + 1. If k = n +
1, stop, else repeat step k.
Example 5.5 For the network presented in figure 5.3 find the shortest route between
every two nodes. Use Floyd’s algorithm.
[55,4] (3) P
[100, 1] (1) [40, 3] (2)

15
2
4

100 10 50
20

30
60
1 3 5

[30, 1] (1) (P) [90, 3] (1)


[0, -] [90, 4] (3)

Figure 5.3 Network Example 5.3

1 2 …… j …… n
1 - d12 …… d1j …… d1n
2 d21 - …… d2j d2n
D0 = ⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
I di2 di2 …… dij …… din
⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
N dn1 dn2 …… dnj …… -

1 2 …… j …… n
1 - 2 …… j …… n
2 1 - …… j n
⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
S0 = I 1 di2 …… j …… n
1 2 …… j …… n
1 - d12 …… d1j …… d1n
2 d21 - …… d2j d2n
D0 = ⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
I di2 di2 …… dij …… din
⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
N dn1 dn2 …… dnj …… -

1 2 …… j …… n
1 - 2 …… j …… n
2 1 - …… j n
⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
S0 = I 1 di2 …… j …… n
⁞ ⁞ ⁞ ⁞ ⁞ ⁞ ⁞
N 1 2 …… j …… -

Iteration 5
No improvement can be obtained by pivoting on node 5, therefore optimal solution has
be reached. D4 and S4 contains the information to determine the shortest route between every two
nodes in the network. For example if we want to find the shortest route between nodes 1 and 5,
from D4 the shortest distance is 12. To determine the shortest route, the first step is to find if
there is a direct route from 1→5.The segment (i, j) represents a direct link only if sij = j. Looking
at S , s15  4  5 , the route is initially 1→ 4 → 5. Now, need to determine if segment (1, 4) has a
4

direct link s14  2  4 . The segment (1, 4) has not a direct link, and 1→ 4 is replaced by 1→ 2→
4 → 5. Since s12  2, s24  4, and s45 =5 , 1→ 2→ 4 → 5 is the shortest route from node 1 to
node 5.
Example 5.6 Solve in class using TORA Floyd’s algorithm iteration by iteration problem
6.3C # 2.
Example 5.7 Solve problem 6.3 C # 4.
Bob

Jim

Joe Kim

Kay Rae

Figure 5.4 Problem 6.3C # 4


Solution: Use TORA. If node i can directly connect with node j assign distance is 1, if not
infinite. Nodes 1- Joe, 2-Bob, 3-Kay, 4-Jim, 5-Rae, 6-Kim,
The largest number of contacts (distance of the route) from your TORA output are:
Nodes Distance Route
Bob (2) – Joe (1) 4 2-3-5-6-1
Jim (4) – Joe (1) 4 4-3-5-6-1
Rae (5) – Jim (4) 4 5-6-2-3-4

You might also like