All revision notes topics

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 ∞\infty (or a dash) if there is none;
  • the route table: the entry in row XX, column YY is the first vertex to travel to when going from XX to YY. At the start each entry is just the column vertex YY. For an undirected network the distance table is symmetric. For a directed network (one-way roads) it need not be.
Key termsFloyd's algorithmdistance tableroute table

Section 2

One iteration

There is one iteration for each vertex in turn. Iteration kk uses the kkth 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 XX, YY (neither being the pivot KK), compare the current distance with going through KK: d(X,K)+d(K,Y)<d(X,Y)d(X,K)+d(K,Y)<d(X,Y) If this is true, replace d(X,Y)d(X,Y) by d(X,K)+d(K,Y)d(X,K)+d(K,Y) and set the route entry (X,Y)(X,Y) equal to the route entry (X,K)(X,K). Otherwise leave both unchanged. The pivot's own row and column never change during its iteration. An entry of ∞\infty in row KK or column KK means that pairing cannot improve anything. Use ∞\infty as larger than any number.

Key termspivot
Common mistake

Setting the new route entry to KK. It must be copied from the entry (X,K)(X,K), because the first step towards KK may not be KK itself.

Exam tip

Update only if the new total is strictly less. If it is equal, leave the table alone.

Section 3

Worked example

Roads AB=2AB=2, AC=3AC=3, BC=7BC=7, BD=10BD=10, CD=4CD=4.

  • Iteration 1 (pivot AA): B→CB\to C becomes 2+3=52+3=5 (route entry AA). Same for C→BC\to B. Nothing else.
  • Iteration 2 (pivot BB): A→DA\to D becomes 2+10=122+10=12, route entry BB (and D→AD\to A likewise). A→CA\to C: 2+5=7>32+5=7>3, so no change.
  • Iteration 3 (pivot CC): A→DA\to D becomes 3+4=73+4=7 (route entry CC). B→DB\to D becomes 5+4=95+4=9 (route entry is (B,C)=A(B,C)=A).
  • Iteration 4 (pivot DD): no entry improves. Final distances: AB=2AB=2, AC=3AC=3, AD=7AD=7, BC=5BC=5, BD=9BD=9, CD=4CD=4.

Section 4

Reading the route from the tables

The distance table gives the length. To find the route from XX to YY, look up (X,Y)(X,Y) in the route table to get the next vertex ZZ. Then look up (Z,Y)(Z,Y), and continue until you reach YY. From the worked example: B→DB\to D: (B,D)=A(B,D)=A, (A,D)=C(A,D)=C, (C,D)=D(C,D)=D. The route is B−A−C−DB-A-C-D with length 2+3+4=92+3+4=9. Always check by adding the road lengths along the route; the total must equal the table entry.

Common mistake

Reading the route table as the last vertex before YY. It is the first vertex after XX.

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 nn iterations on an n×nn\times n table.
  • A network with nn vertices needs exactly nn iterations.
  • For a directed network, treat d(X,Y)d(X,Y) and d(Y,X)d(Y,X) separately, and only update using arcs that exist.
  • Show each iteration clearly, with the changed entries, to earn the method marks.
Exam tip

After each iteration, check that row KK and column KK 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

  1. A network has four vertices AA, BB, CC and DD. The roads and their lengths are AB=2AB=2, AC=3AC=3, BC=7BC=7, BD=10BD=10 and CD=4CD=4. Floyd's algorithm is applied, with the pivots in the order AA, BB, CC, DD. 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 XX, column YY is the first vertex to travel to when going from XX to YY; initially it is YY 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 BB to DD and use the final route table to give the route.2 marks
  2. Five sites AA to EE are joined by roads with lengths in km: AB=5AB=5, BC=3BC=3, CD=6CD=6, DE=2DE=2, EA=12EA=12 and BE=11BE=11. Floyd's algorithm is applied, with the pivots in the order AA, BB, CC, DD, EE. In the route table, the entry in row XX, column YY is the first vertex to travel to when going from XX to YY; initially it is YY 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 EE to CC and give the route.2 marks
  3. Four villages PP, QQ, RR and SS are joined by roads with lengths in km: PQ=3PQ=3, PR=5PR=5, QR=9QR=9, QS=7QS=7 and RS=2RS=2. Floyd's algorithm is applied, with the pivots in the order PP, QQ, RR, SS. In the route table, the entry in row XX, column YY is the first vertex to travel to when going from XX to YY; initially it is YY 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
See the full worksheet

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).