Floyd's algorithmEdexcel A-Level Further Maths: Revision notes
Section 1
What Floyd's algorithm does
Floyd's algorithm finds the shortest distance between every pair of vertices in a network at once. It works with two tables, each with a row and a column for every vertex:
- the distance table: the leading diagonal is 0, each other entry is the length of the direct road, or (or a dash) if there is none;
- the route table: the entry in row , column is the first vertex to travel to when going from to . At the start each entry is just the column vertex . For an undirected network the distance table is symmetric. For a directed network (one-way roads) it need not be.
Section 2
One iteration
There is one iteration for each vertex in turn. Iteration uses the th vertex as the pivot, so iteration 1 uses the first row and column, iteration 2 the second row and column, and so on. For every pair , (neither being the pivot ), compare the current distance with going through : If this is true, replace by and set the route entry equal to the route entry . Otherwise leave both unchanged. The pivot's own row and column never change during its iteration. An entry of in row or column means that pairing cannot improve anything. Use as larger than any number.
Setting the new route entry to . It must be copied from the entry , because the first step towards may not be itself.
Update only if the new total is strictly less. If it is equal, leave the table alone.
Section 3
Worked example
Roads , , , , .
- Iteration 1 (pivot ): becomes (route entry ). Same for . Nothing else.
- Iteration 2 (pivot ): becomes , route entry (and likewise). : , so no change.
- Iteration 3 (pivot ): becomes (route entry ). becomes (route entry is ).
- Iteration 4 (pivot ): no entry improves. Final distances: , , , , , .
Section 4
Reading the route from the tables
The distance table gives the length. To find the route from to , look up in the route table to get the next vertex . Then look up , and continue until you reach . From the worked example: : , , . The route is with length . Always check by adding the road lengths along the route; the total must equal the table entry.
Reading the route table as the last vertex before . It is the first vertex after .
Section 5
Using Floyd's algorithm
- One run gives the shortest distance between every pair. Dijkstra's algorithm gives them only from one start vertex, so it would need to be run from each vertex in turn.
- If only one start vertex is needed, Dijkstra's is quicker, as Floyd's does iterations on an table.
- A network with vertices needs exactly iterations.
- For a directed network, treat and separately, and only update using arcs that exist.
- Show each iteration clearly, with the changed entries, to earn the method marks.
After each iteration, check that row and column have not changed, and that for an undirected network the table is still symmetric.
That's the notes covered.
Carry on to the next subtopic.
Exam questions on Floyd's algorithm
- A network has four vertices , , and . The roads and their lengths are , , , and . Floyd's algorithm is applied, with the pivots in the order , , , . In the distance table the leading diagonal is 0 and a dash means there is no direct road. In the route table, the entry in row , column is the first vertex to travel to when going from to ; initially it is itself. Each iteration uses the next vertex in order as the pivot (iteration 1 uses the first row and column).Complete the algorithm. Find the shortest distance from to and use the final route table to give the route.2 marks
- Five sites to are joined by roads with lengths in km: , , , , and . Floyd's algorithm is applied, with the pivots in the order , , , , . In the route table, the entry in row , column is the first vertex to travel to when going from to ; initially it is itself. Each iteration uses the next vertex in order as the pivot (iteration 1 uses the first row and column).Complete the algorithm. Find the shortest distance from to and give the route.2 marks
- Four villages , , and are joined by roads with lengths in km: , , , and . Floyd's algorithm is applied, with the pivots in the order , , , . In the route table, the entry in row , column is the first vertex to travel to when going from to ; initially it is itself. Each iteration uses the next vertex in order as the pivot (iteration 1 uses the first row and column).Carry out iterations 1 and 2. State each entry of the distance table that changes, with its new value, and the new route table entries.3 marks
Written by the Exaim team, led by Shaun Daswani (Head of Upper Secondary, Improve ME Institute; MSc Financial Mathematics, Imperial College London; BSc, UCL) and Jason Daswani (operational lead, Improve ME Institute; LSE).