Dijkstra's Algorithm: Shortest Routes with Working Values | Edexcel A Level Further Maths notes

Free preview: Dijkstra's Algorithm: Shortest Routes with Working Values

Finding the Shortest Route: The Big Idea

Watch: 1-minute explainer

Year 1 (AS) for OCR B (MEI). For the other boards, check your own specification for which parts are AS and which are A level.

Let's start with a sat nav. It knows the time along every road and must find the quickest route from where you are to where you want to go. Trying every possible route would take far too long: even a small town has thousands. Dijkstra's algorithm does it efficiently.

  • Dijkstra's algorithm finds the shortest (least weight) path from a start vertex to every other vertex in a network.
  • It works outwards from the start, fixing vertices one at a time in order of distance, closest first.
  • Once a vertex gets its final label, that is definitely its shortest distance from the start. It never changes again.

Network with vertices S, A, B, C, D, E, T. Edges: SA 5, SB 9, SC 4, AB 3, CB 6, AD 9, BD 5, BE 8, CE 12, DT 6, ET 4, DE 3

Following the diagram

  • We want the quickest route from S to T. The numbers are journey times in minutes.
  • The direct-looking route S–B–D–T takes 9+5+6=20 minutes. Can we do better?
  • There are many routes, and some (like S–A–B) beat a single direct edge (S–B). The algorithm handles this automatically.

Why closest first?

  • Imagine water spreading out from S along every road at the same speed. It reaches the nearest vertex first.
  • When water first reaches a vertex, it has come by the quickest route, because every other route is still on its way.
  • Dijkstra copies this: the vertex with the smallest working value is the next one the water reaches, so its value is final.

Diagram: Wrong: S–B has weight 9, so B is given 9 straight away and treated as final.

What the picture shows

  • Wrong: S–B has weight 9, so B is given 9 straight away and treated as final.
  • Right: A has the smallest working value, 5, so A is made final first.
  • From A, 5+3=8, which beats 9. So the shortest distance to B is 8, through A.

The labels

  • Each vertex gets a box with three parts on top: the vertex, the order in which it got its final label, and its final label (shortest distance).
  • Underneath go the working values: the best distances found so far. A new working value is written only if it is smaller than the current one.

Here is what B's box will hold at the end of this note's example, part by part:

Part of the boxB's entryWhat it means
VertexBWhich vertex this box is for.
Order4B was the 4th vertex to get its final label.
Final label8The shortest distance from S to B.
Working values9, 89 was found first (straight along SB); 8 was found later (through A) and replaced it. The last working value becomes the final label.

💡 In plain words

  • Working value: "the best I've found so far". It can still go down.
  • Final label: "the best there is". It is fixed.

✏️ YOUR TURN 1

Try this first:

At some stage, the vertices without final labels have working values P 12, Q 9, R 15 and U (none yet). Which vertex gets its final label next? Can Q's distance still go down later?

Answer:

  • Q, with the smallest working value 9.
  • No. Q's working value already covers every route that uses only final vertices. Any other route must pass through P, R or U on the way, and those are already at least 12 away (or not reached), so with no negative weights it is longer than 9.

Dijkstra's Algorithm Step by Step

The recipe

  1. Give the start vertex the final label 0 and order 1. Why: the distance from S to itself is 0.
  2. For each vertex joined to the vertex that has just got its final label (and not yet final), work out: final label + edge weight. Write it as a working value if it is smaller than the existing one. Why: you have found a route through the new final vertex; keep it only if it is better.
  3. Of all vertices without final labels, choose the one with the smallest working value. Make that its final label, with the next order number. Why: nothing can beat it (closest first).
  4. Repeat steps 2 and 3 until the destination has a final label.
  5. Trace the route back from the destination (see below).

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

Video

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

Next note: Floyd's Algorithm: Distance and Route Tables

Dijkstra's Algorithm: Shortest Routes with Working Values on other exam boards

All Edexcel A Level Further Maths topics · A Level Further Maths