Floyd's Algorithm (Edexcel A Level Further Maths): Revision Note
Floyd's Algorithm
What is Floyd's Algorithm?
Floyd's algorithm finds the shortest distance and the shortest route between any pair of nodes in a network
This is achieved by setting up and updating a pair of matrices
The first is a matrix of (least) distances between nodes
it is generally called the distance matrix
the final distance matrix shows the shortest distance between any pair of nodes
The second matrix allows the shortest route between nodes to be deduced
it is generally called the route matrix
At each stage (iteration) of Floyd's algorithm, both the distance matrix and route matrix are updated
Floyd's algorithm will seem long winded and slow at first
But the symmetry (in undirected networks) in the distance matrix can reduce the amount of work involved whilst also acting as a check
How do I set up the initial distance matrix and initial route matrix for Floyd's algorithm?
The initial distance matrix has the direct distances between nodes
If there is no direct distance the symbol
is used (to represent/assume a very large distance!)
In an undirected network, all distance matrices will exhibit symmetry
this is because the distance from node A to C, say, will be the same as the distance from C to A
this will apply to all pairs of nodes
this helps reduce the work required in Floyd's algorithm but also acts as a means of checking each distance matrix
The initial route matrix assumes the shortest route between each pair of nodes is a direct link
So the shortest route between A and D, say, is to go directly from A to D
It doesn't matter if there is no direct link between a pair of nodes
this will be accounted for later in Floyd's algorithm
Worked Example
For the network shown below, set up the initial distance matrix and the initial route matrix for Floyd's algorithm.

Use the direct distances given on the diagram Where there is no direct link, use for the (initial) distance
There is a direct link between nodes A and C, with a distance of 5, so row A, column C will contain a 5 (This is also true for row C, column A, as the network is undirected)
There is no direct link between nodes A and E, so use (also true for row E, column A)
| Initial distance matrix | ||||
| A | B | C | D | E |
A | - | 7 | 5 | 10 | |
B | 7 | - | 4 | 17 | 3 |
C | 5 | 4 | - | 6 | |
D | 10 | 17 | 6 | - | 3 |
E | 3 | 3 | - |
The initial route matrix has the destination node directly in place for each starting node
| Initial route matrix | ||||
| A | B | C | D | E |
A | A | B | C | D | E |
B | A | B | C | D | E |
C | A | B | C | D | E |
D | A | B | C | D | E |
E | A | B | C | D | E |
How do I update the distance matrix in Floyd's algorithm?
For the FIRST iteration highlight the first row and the first column of the distance matrix
For the other cells in the distance matrix (except those labelled with a '-')
add the corresponding highlighted distances together
if this is less than the current distance, then the current distance gets updated

This process of updating distances is repeated for each iteration of Floyd's algorithm
The number of iterations will be equal to the number of nodes
On the second iteration, the SECOND row and SECOND column would be highlighted
On the
iteration, the
row and column would be highlighted
How do I update the route matrix in Floyd's algorithm?
For the FIRST iteration, highlight the first column of the route matrix
Find the corresponding cells that have been updated in the distance matrix for this iteration
These cells are updated with the node in the highlighted column

This process of updating routes is repeated for each iteration of Floyd's algorithm
The number of iterations will be equal to the number of nodes
On the second iteration, the SECOND column would be highlighted
On the
iteration, the
column would be highlighted
Examiner Tips and Tricks
An exam question is unlikely to ask you to do all iterations of Floyd's algorithm
Make sure you can recognise where to start if given matrices after, say, the third iteration
Worked Example
The distance and route matrices below follow one complete iteration of Floyd's algorithm.
| Distance matrix |
|
|
| Route matrix | ||||||||
| A | B | C | D | E |
|
|
| A | B | C | D | E |
A | - | 7 | 5 | 10 |
|
| A | A | B | C | D | B | |
B | 7 | - | 4 | 17 | 3 |
|
| B | A | B | C | A | E |
C | 5 | 4 | - | 6 |
|
| C | A | B | C | D | E | |
D | 10 | 17 | 6 | - | 3 |
|
| D | A | A | C | D | E |
E | 3 | 3 | - |
|
| E | A | B | C | D | E |
Complete the remaining iterations of Floyd's algorithm, showing both the distance and route matrices after each iteration.
2nd Iteration
In the distance matrix, row A, column E, 3 + 7 = 10 and 10 < so the distance is changed to 10
Row C, column E, 3 + 4 = 7 and 7 < ∞ so the distance is changed to 7
Likewise for row E and columns A and C (note the symmetry as the network is undirected)
In the route matrix, corresponding cells are changed
| Distance matrix |
|
|
| Route matrix | ||||||||
| A | B | C | D | E |
|
|
| A | B | C | D | E |
A | - | 7 | 5 | 10 | 10 |
|
| A | A | B | C | D | B |
B | 7 | - | 4 | 17 | 3 |
|
| B | A | B | C | A | E |
C | 5 | 4 | - | 6 | 7 |
|
| C | A | B | C | D | B |
D | 10 | 17 | 6 | - | 3 |
|
| D | A | A | C | D | E |
E | 10 | 3 | 7 | 3 | - |
|
| E | B | B | B | D | E |
3rd iteration
Row B, column D, 6 + 4 = 10 and 10 < 17 so distance is changed to 10
This is the same for row D, column B and the corresponding route matrix cells are updated too
| Distance matrix |
|
|
| Route matrix | ||||||||
| A | B | C | D | E |
|
|
| A | B | C | D | E |
A | - | 7 | 5 | 10 | 10 |
|
| A | A | B | C | D | B |
B | 7 | - | 4 | 10 | 3 |
|
| B | A | B | C | C | E |
C | 5 | 4 | - | 6 | 7 |
|
| C | A | B | C | D | B |
D | 10 | 10 | 6 | - | 3 |
|
| D | A | C | C | D | E |
E | 10 | 3 | 7 | 3 | - |
|
| E | B | B | B | D | E |
4th iteration
There are no changes in this iteration, but note that there still could be changes in the fifth iteration
| Distance matrix |
|
|
| Route matrix | ||||||||
| A | B | C | D | E |
|
|
| A | B | C | D | E |
A | - | 7 | 5 | 10 | 10 |
|
| A | A | B | C | D | B |
B | 7 | - | 4 | 10 | 3 |
|
| B | A | B | C | C | E |
C | 5 | 4 | - | 6 | 7 |
|
| C | A | B | C | D | B |
D | 10 | 10 | 6 | - | 3 |
|
| D | A | C | C | D | E |
E | 10 | 3 | 7 | 3 | - |
|
| E | B | B | B | D | E |
5th iteration (final iteration)
There are two cells to change on this iteration
| Distance matrix |
|
|
| Route matrix | ||||||||
| A | B | C | D | E |
|
|
| A | B | C | D | E |
A | - | 7 | 5 | 10 | 10 |
|
| A | A | B | C | D | B |
B | 7 | - | 4 | 6 | 3 |
|
| B | A | B | C | E | E |
C | 5 | 4 | - | 6 | 7 |
|
| C | A | B | C | D | B |
D | 10 | 6 | 6 | - | 3 |
|
| D | A | E | C | D | E |
E | 10 | 3 | 7 | 3 | - |
|
| E | B | B | B | D | E |
Final matrices
| Final distance matrix |
|
|
| Final route matrix | ||||||||
| A | B | C | D | E |
|
|
| A | B | C | D | E |
A | - | 7 | 5 | 10 | 10 |
|
| A | A | B | C | D | B |
B | 7 | - | 4 | 6 | 3 |
|
| B | A | B | C | E | E |
C | 5 | 4 | - | 6 | 7 |
|
| C | A | B | C | D | B |
D | 10 | 6 | 6 | - | 3 |
|
| D | A | E | C | D | E |
E | 10 | 3 | 7 | 3 | - |
|
| E | B | B | B | D | E |
How do I use Floyd's algorithm to find shortest distances and routes?
The shortest distance is found by reading off the value from the final distance matrix
To find the shortest route between nodes, use the final route matrix
start on the row for the starting node
find the cell that is under the destination node, then move to that row
repeat until the cell contains the destination node
Examiner Tips and Tricks
Exam questions will often involve the final matrices so questions can be asked about shortest distances and routes
The far majority of networks are undirected
there will be symmetry in each distance matrix
this can cut down the amount of work needed at each iteration of Floyd's algorithm
but the repetition can also act as a check
Worked Example
The final distance and route matrices for Floyd's algorithm are given below.
| Final distance matrix |
|
|
| Final route matrix | ||||||||
| A | B | C | D | E |
|
|
| A | B | C | D | E |
A | - | 7 | 5 | 10 | 10 |
|
| A | A | B | C | D | B |
B | 7 | - | 4 | 6 | 3 |
|
| B | A | B | C | E | E |
C | 5 | 4 | - | 6 | 7 |
|
| C | A | B | C | D | B |
D | 10 | 6 | 6 | - | 3 |
|
| D | A | E | C | D | E |
E | 10 | 3 | 7 | 3 | - |
|
| E | B | B | B | D | E |
a) i) Find the shortest distance between nodes A and E.
ii) Find the shortest distance between nodes B and C.

i) Use the final distance matrix to find where row A meets column E
10
Also from row E, column A
ii) Use the final distance matrix to find where row B meets column C
4 Also from row C, column B
b) Find the shortest route between nodes B and D.

Start on row B (start node)
Node E is indicated under the destination node column (D) for this row, so move to row E
Node D is indicated under the destination node (D) so the route is complete
The shortest route is BED
You've read 0 of your 5 free revision notes this week
Sign up now. It’s free!
Did this page help you?