Shortest Path Algorithms
Revise Shortest Path Algorithms for Further Decision Mathematics 1 WDM11 (AS Level) — revision notes and instant AI marking. Free to start.
Shortest Path Algorithms
Big idea: Dijkstra's algorithm finds the quickest way from one fixed starting point to everywhere else, while Floyd's algorithm finds the quickest way between every possible pair of points at once — same goal (shortest distance), different scale of ambition.
Summary — What This Chapter Covers
- Dijkstra's algorithm finds the shortest distance from one fixed start vertex to every other vertex in a network (or you can stop early once you hit your target vertex).
- Each vertex gets a labelling box with four values: the vertex name, its order of being finalised, its final (permanent) shortest distance, and its running "working values."
- You build up the network by only ever looking at neighbours of the most recently finalised vertex, and you always keep the smallest working value seen so far.
- Once every vertex (or your target) has a final label, you trace the shortest route backwards from the destination using the "difference in final labels = edge weight" trick.
- Floyd's algorithm finds the shortest distance between every pair of nodes in a network — not just from one starting point — using two matrices: a distance matrix and a route matrix.
- Floyd's algorithm updates both matrices in a fixed number of iterations (one per node), each time checking whether going "via" a particular node gives a shorter route.
- Dijkstra = fast, but only from one start point. Floyd = slower, but gives you the whole picture for any start/end pair. Repeating Dijkstra from every possible start vertex would eventually give you the same information as one full run of Floyd's algorithm.
Key Vocabulary — Get Comfortable With These First
1. Dijkstra's Algorithm
What problem does it actually solve?
Imagine you're standing at a warehouse (your start vertex) and you want to know the shortest route to every single delivery address in your area. You don't want to solve "shortest route to address A," then separately solve "shortest route to address B" from scratch — that would be wasteful, because a lot of those routes overlap.
Dijkstra's algorithm is smart about this. It starts at your fixed vertex and grows outward like ripples on a pond, always finalising the closest unfinished vertex next. By the time it's done, you know the shortest distance from your start to every other vertex in the network — all in one pass.
If you only care about one destination, you can stop the moment that destination gets its final label — no need to finish labelling the rest of the network. This is exactly why Dijkstra's is fast in practice.
The labelling box — read this carefully
Every vertex gets a small box with four pieces of information. Here's what each part means and where it sits:
| Box | What goes here |
|---|---|
| Vertex | The name of the vertex (e.g. "C") |
| Labelling order | Which numbered round this vertex became permanent in (1st, 2nd, 3rd...) |
| Final label | The confirmed shortest distance from the start vertex — this is the answer, once filled in |
| Working values | A running list of "current best guesses" — you can write several over time, but only the smallest (most recent, correctly-updated) one matters |
The six steps of Dijkstra's algorithm
Worked Example — Full Walkthrough
Network: A–B (9), A–C (12), A–F (5), A–G (8), B–C (4), C–G (9), C–D (4), F–G (2), F–E (4), G–E (1), E–D (7). Find the shortest path from A to D.
Step 2: Start vertex A → order = 1, final label = 0.
Round 1 (Step 3 from A): Neighbours of A are B, C, F, G. Working values: B = 9, C = 12, F = 5, G = 8.
Round 1 (Step 4): Smallest working value is F (5). → F gets order 2, final label 5.
Round 2 (Step 3 from F): F connects to G and E. Via F: G = 5 + 2 = 7 (smaller than the 8 we had — update it!). E = 5 + 4 = 9.
Round 2 (Step 4): Smallest unfinalised value now is G (7). → G gets order 3, final label 7.
Round 3 (Step 3 from G): G connects to C and E. Via G: C would be 7 + 9 = 16 (bigger than 12, so ignore). E would be 7 + 1 = 8 (smaller than 9 — update it!).
Round 3 (Step 4): Smallest unfinalised value now is E (8). → E gets order 4, final label 8.
Round 4 (Step 3 from E): E connects to D. Via E: D = 8 + 7 = 15.
Round 4 (Step 4): Compare B(9), C(12), D(15) — smallest is B (9). → B gets order 5, final label 9.
Round 5 (Step 3 from B): B connects to C. Via B: C would be 9 + 4 = 13 (bigger than 12 — ignore).
Round 5 (Step 4): Compare C(12), D(15) — smallest is C (12). → C gets order 6, final label 12.
Round 6 (Step 3 from C): C connects to D. Via C: D would be 12 + 4 = 16 (bigger than 15 — ignore).
Round 6 (Step 4): Only D left with a working value (15). → D gets order 7, final label 15.
Step 6 — trace backwards from D (final label 15):
| Check | Calculation | Result |
|---|---|---|
| D → E | 15 − 8 = 7 | matches edge E–D (7) ✓ |
| E → G | 8 − 7 = 1 | matches edge G–E (1) ✓ |
| G → F | 7 − 5 = 2 | matches edge F–G (2) ✓ |
| F → A | 5 − 0 = 5 | matches edge A–F (5) ✓ |
Practice Questions — Dijkstra's
In Dijkstra's algorithm, why do we only look at neighbours of the most recently finalised vertex in Step 3, rather than checking every vertex that's ever been finalised?
A vertex X already has a working value of 14 written in its box. During a later round, you calculate a new possible working value of 17 coming from a different vertex. What should you do, and why?
2. Floyd's Algorithm
What problem does it actually solve?
Now imagine a completely different situation: you're building a delivery app where any customer might want to travel between any two points in the city — not always starting from the same warehouse. Running Dijkstra's algorithm over and over again (once per possible starting point) would eventually get you the answer, but that's a lot of repeated effort.
Floyd's algorithm solves this more directly. Instead of growing outward from one vertex, it works by asking a clever question over and over: "Is it shorter to go from node i to node j directly, or to go via some other node k?" It checks this question for every possible "via" node, one at a time, updating a table of best-known distances as it goes.
By the time it's finished, you have the shortest distance between every single pair of nodes in the network — not just from one fixed start.
Setting up the two matrices
Floyd's algorithm tracks two grids side by side, and updates both together at each step:
- Distance matrix — the current best-known shortest distance between each pair of nodes.
- Route matrix — for each pair of nodes, which node to go to next on the shortest path (not the whole path — just the next stepping stone).
Initial distance matrix
Fill in the direct distances between nodes straight from the network diagram. If there's no direct edge between two nodes, write ∞ (infinity) — this just means "we don't know a route yet, assume it's huge for now." Because most networks in this topic are undirected, the matrix will always come out symmetric (the distance from A to C is the same as from C to A) — this is a great way to double-check your work.
Initial route matrix
At the very start, before any clever routing has been considered, we simply assume the shortest way from any node to any other node is to go directly. So every cell just contains the name of the destination node itself (row = start, column = destination, cell = "go straight there").
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | – | 7 | 5 | 10 | ∞ |
| B | 7 | – | 4 | ∞ | 3 |
| C | 5 | 4 | – | 6 | ∞ |
| D | 10 | ∞ | 6 | – | 3 |
| E | ∞ | 3 | ∞ | 3 | – |
Initial route matrix (every cell just says "go direct to the column node"):
| 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 each iteration actually works
Floyd's algorithm runs one iteration per node in the network. On the k-th iteration, you highlight the k-th row and k-th column of the distance matrix (they represent "distances involving node k").
Updating the distance matrix: for every other cell (except the "–" diagonal), add together the two highlighted values that line up with that cell (its row's highlighted value + its column's highlighted value). If that sum is smaller than the number currently sitting in the cell, replace it — you've just found a shortcut that goes "via" node k. If it's not smaller, leave the cell exactly as it is.
Updating the route matrix at the same time: highlight the k-th column of the route matrix. Wherever a cell in the distance matrix got updated on this iteration, replace the matching cell in the route matrix with the node name k (the "via" node). This records: "to get from this row's node to this column's node, the next stepping stone is node k."
Reading off answers from the final matrices
Once every iteration is done, you're left with a final distance matrix and a final route matrix. Here's how to use them:
Worked example — reading the final matrices:
| Final distance matrix | |||||
|---|---|---|---|---|---|
| A | B | C | D | E | |
| A | – | 7 | 5 | 10 | 10 |
| B | 7 | – | 4 | 6 | 3 |
| C | 5 | 4 | – | 6 | 7 |
| D | 10 | 6 | 6 | – | 3 |
| E | 10 | 3 | 7 | 3 | – |
Row A, column E gives 10 — that's the shortest distance from A to E. Row B, column C gives 4 — the shortest distance from B to C (they're direct neighbours, so it makes sense this is just the edge weight).
| Final route matrix | |||||
|---|---|---|---|---|---|
| A | B | C | D | E | |
| A | A | B | C | D | B |
| B | A | B | C | E | E |
| C | A | B | C | D | B |
| D | A | E | C | D | E |
| E | B | B | B | D | E |
Finding the route from B to D:
- Start on row B. Look under column D (our destination) → the cell says E. So the first stepping stone is E.
- Move to row E. Look under column D again → the cell says D. We've reached the destination!
Practice Questions — Floyd's
Why does the initial distance matrix always come out symmetric for an undirected network, and how can this fact help you check your work as you go?
During the 4th iteration of Floyd's algorithm, you check a cell and find that (highlighted row value) + (highlighted column value) = 14, but the current value already sitting in that cell is 9. What do you do, and what does this tell you about the route?
3. Comparing Dijkstra's & Floyd's Algorithms
| Dijkstra's Algorithm | Floyd's Algorithm | |
|---|---|---|
| Finds shortest path from... | One fixed start vertex to every other vertex | Any pair of start and end nodes |
| Best used when... | The starting point can't move (e.g. a power station, a distribution warehouse) | The starting point could be anywhere (e.g. a robot inspecting a pipeline network from various points) |
| Main advantage | Speed — can stop early once the target vertex is finalised | Amount of information — gives you everything, for every pair, in one go |
| Main disadvantage | Limited — only useful for routes starting from that one fixed vertex | Time — much slower to do by hand, since you must complete every iteration |
What to Memorise
Dijkstra's — the 6 steps in one line each
- Draw labelling boxes for every vertex.
- Start vertex: order = 1, final label = 0.
- Find working values for all unlabelled neighbours of the most recently finalised vertex (only replace if smaller).
- Finalise whichever unlabelled vertex has the smallest working value.
- Repeat steps 3–4 until the destination (or whole network) is labelled.
- Trace back: final label difference = edge weight, to find the path.
Floyd's — the core update rule
Quick comparison to remember
Dijkstra: one start, fast, stoppable early. Floyd: all pairs, slower, needs full completion (one iteration per node).
Concepts Checklist
Exam Tips & Common Mistakes
- 3. Comparing Dijkstra's & Floyd's Algorithms
- Exam Tips & Common Mistakes
Read the full Shortest Path Algorithms notes free
That's the preview — create a free account to read the rest, plus flashcards and practice questions with instant AI marking. No credit card.
Unlock the full notes free →