Floyd's Algorithm: Distance and Route Tables | Edexcel A Level Further Maths notes

Free preview: Floyd's Algorithm: Distance and Route Tables

Shortest Distances Between Every Pair

Edexcel Decision 1 only. Check your own specification for whether it is AS or A level content.

Let's start with a road atlas. The table at the back gives the shortest distance between every pair of towns. How would you build that table? Dijkstra's algorithm (previous note) would need running once for every start town. Floyd's algorithm builds the whole table in one systematic process.

  • Floyd's algorithm finds the shortest distance between every pair of vertices in a network.
  • It uses two tables: a distance table (the best distances so far) and a route table (how to start each best route).
  • It does one iteration for each vertex in turn. In iteration k it asks: is it shorter to go from i to j through vertex k?

Network with vertices A, B, C, D. Edges AB 3, AC 9, AD 7, BC 4, CD 2. There is no edge BD. A note says A to C is 9 direct or 3 + 4 = 7 via B

Following the diagram

  • The direct edge AC has length 9, but Aโ€“Bโ€“C has length 7. Floyd's algorithm will find this.
  • There is no edge BD, so the starting table has a dash (infinity) there. B to D will be found through other vertices.

The big idea

  • At first, the only routes we know are single edges.
  • After iteration 1, we also know the best routes that may pass through A.
  • After iteration 2, the best routes that may pass through A and/or B. And so on.
  • After the last iteration, routes may pass through any vertex, so every distance is the true shortest.

The starting tables

  • The distance table holds the edge weights, 0 on the diagonal, and a dash where there is no direct edge.
  • The route table starts with every entry in column j equal to j: "to go to j, go straight to j".

Distance table:

ABCD
A0397
B304โ€“
C9402
D7โ€“20

Route table:

ABCD
AABCD
BABCD
CABCD
DABCD

๐Ÿ’ก In plain words

  • The route table entry in row i, column j is the first vertex to visit on the best route found so far from i to j.
  • A dash means "no route known yet". Treat it as infinitely long: any real route beats it.

โœ๏ธ YOUR TURN 1

Try this first:

A network has vertices P, Q, R, S and edges PQ 5, PR 2, QR 1, QS 6, RS 8. Write down the starting distance table.

Answer:

PQRS
P052โ€“
Q5016
R2108
Sโ€“680

0 on the diagonal; a dash for P to S because there is no edge PS. The table is symmetric because the edges are two-way.

One Iteration, Step by Step

The recipe for iteration k

  1. Highlight row k and column k. These entries do not change in this iteration. Why: going from i to k "through k" adds nothing.
  2. For every other entry (row i, column j, with i โ‰  j), compare the current distance d(i,j) with d(i,k)+d(k,j): the entry in row i of column k plus the entry in column j of row k. Why: that is the length of the route i โ†’ k โ†’ j.
  3. If the sum is smaller, replace d(i,j) with the sum, and replace the route entry in row i, column j with the route entry in row i, column k. Why: the new route starts the same way as the route from i to k.
  4. Move on to the next vertex as k. After the last vertex, the tables are final.

if d(i,k)+d(k,j)<d(i,j):d(i,j)←d(i,k)+d(k,j),r(i,j)←r(i,k)

๐Ÿ’ก In plain words

For each pair, ask: "would a detour through k be shorter?" If yes, write the new distance, and copy how to start the trip to k.

๐Ÿ‘€ Watch me do it: iteration 1 (through A)

What I writeWhy I do it
Row A and column A stay as they arek = A.
B to C: d(B,A)+d(A,C)=3+9=12, not less than 4No change.
B to D: 3+7=10<∞. Write 10; route entry r(B, A) = AThe dash is beaten by any real route.
C to D: 9+7=16, not less than 2No change.
D to B: 10, route A; C to B and D to C: no changeThe table is symmetric, so the lower half mirrors the upper half.

Unlock the full note: start 7 days free.

Log in or subscribe to read the rest of this note. Members also get the AI tutor, practice quizzes, mock exams and flashcards for this topic.

Log in ยท Subscribe ยท Start free trial

All notes in Shortest Path Algorithms

  1. Dijkstra's Algorithm: Shortest Routes with Working Values
  2. Floyd's Algorithm: Distance and Route Tables

What's inside for this topic

2 teacher notes, 69 practice questions, 40 flashcards, 2 quizzes.

Get the full notes, every practice question, flashcards, quizzes and mock papers.

Start your free 7-day trial

Questions? Chat with us on WhatsApp ยท Try one live lesson

Previous note: Dijkstra's Algorithm: Shortest Routes with Working Values

All Edexcel A Level Further Maths topics ยท A Level Further Maths