What the lesson covers
Dijkstra's algorithm finds the shortest route from one node to every other in a graph with non-negative weights. Students follow the distances as they settle, trace the shortest path and practise the key rule.
The slides, one by one
1 Dijkstra's algorithm
The shortest route through a network
How does a sat-nav find the quickest way home?
2 Shortest distances spread out from the start
Edge numbers: lengths
\textsf{Pink: shortest distance from }A
\textsf{Shortest path to }F\textsf{: A }{\to}\textsf{ C }{\to}\textsf{ E }{\to}\textsf{ F}
3 Always settle the nearest unvisited node
Each step
- Pick the nearest unvisited node
- Mark its distance final
- Update its neighbours
Rules
- Start at 0, others ∞
- Keep the shorter route
- Weights must not be negative
Greedy, but always correct for these graphs
4 Tracing the algorithm
Use the graph on the previous slide (A–B 4, A–C 2, C–B 1, B–D 5, C–E 6, D–E 2, D–F 3, E–F 1). Find the shortest distance from A to F.
→ Settle nodes in order of distance
A = 0
C = 2,\ B = \min(4, 2 + 1) = 3
D = 3 + 5 = 8,\ E = 2 + 6 = 8
F = \min(8 + 3,\ 8 + 1) = 9
The shortest route A → C → E → F has length 9
5 Quick check
Which node does Dijkstra's algorithm settle next?
- The node with the most edges
- The node added most recently
- The node furthest away
- The unvisited node with the smallest distance
Show the answer
The unvisited node with the smallest distance
Taking the smallest tentative distance guarantees it cannot be improved later.
6 What to remember
Start at 0, every other node ∞
Settle the nearest unvisited node
Update neighbours with shorter routes
Needs non-negative weights
Challenge: Why can a negative edge weight make Dijkstra's algorithm give a wrong answer?
Every slide is built click by click: text, formulas and pictures come in one step at a time, so the class follows the reasoning instead of reading ahead. Graphs with sliders can be dragged live while you present.
Make your own version with AIChange the level, the numbers or the language: type the topic and get a new deck that moves in about 30 seconds. Free, no account needed.
Questions teachers ask
How does Dijkstra's algorithm work?
Give the start distance 0 and every other node infinity. Repeatedly take the unvisited node with the smallest distance, mark it final and update its neighbours with shorter routes through it, until the target is settled.
Are these slides really free?
Yes. Watch them on this page or present them full screen with no account. Making your own copy to edit is free too.
Can I change the slides before my lesson?
Yes. Press "Make my own copy" and the deck becomes one of your projects in the Tracé editor, where you can change the text, the numbers, the colours or the order of the clicks.
Do they work on a classroom projector or an interactive whiteboard?
Yes. They run in the browser on any laptop, so anything that shows your screen works. Press the right arrow or click to go to the next step.