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?

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:
| A | B | C | D | |
|---|---|---|---|---|
| A | 0 | 3 | 9 | 7 |
| B | 3 | 0 | 4 | โ |
| C | 9 | 4 | 0 | 2 |
| D | 7 | โ | 2 | 0 |
Route table:
| A | B | C | D | |
|---|---|---|---|---|
| A | A | B | C | D |
| B | A | B | C | D |
| C | A | B | C | D |
| D | A | B | C | D |
๐ก 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:
| P | Q | R | S | |
|---|---|---|---|---|
| P | 0 | 5 | 2 | โ |
| Q | 5 | 0 | 1 | 6 |
| R | 2 | 1 | 0 | 8 |
| S | โ | 6 | 8 | 0 |
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
- Highlight row k and column k. These entries do not change in this iteration. Why: going from i to k "through k" adds nothing.
- For every other entry (row i, column j, with i โ j), compare the current distance with : 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.
- If the sum is smaller, replace 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.
- Move on to the next vertex as k. After the last vertex, the tables are final.
๐ก 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 write | Why I do it |
|---|---|
| Row A and column A stay as they are | k = A. |
| B to C: , not less than 4 | No change. |
| B to D: . Write 10; route entry r(B, A) = A | The dash is beaten by any real route. |
| C to D: , not less than 2 | No change. |
| D to B: 10, route A; C to B and D to C: no change | The 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
- Dijkstra's Algorithm: Shortest Routes with Working Values
- 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.
Previous note: Dijkstra's Algorithm: Shortest Routes with Working Values
All Edexcel A Level Further Maths topics ยท A Level Further Maths