Shortest paths with Dijkstra
Construction slows down one footpath on campus. At what point does taking a detour get you there faster?
Add travel times, update tentative distances and reconstruct a shortest path.
Theory
Letters mark stops and numbers give walking times in either direction. Dijkstra finds the smallest total time from A to E.
Listen to the explanation
The audio could not load. The transcript is below.
Audio transcript
At the start, your travel time to A is zero. Every other stop has an unknown time. Check the paths out of A and write down the times you can reach. Next, choose the unvisited stop with the smallest known time. With nonnegative walking times, its shortest time is now certain. If B takes two minutes to reach and C is another two minutes away, you have a four minute route to C. Replace its old time only if four is better. Change the direct walking time and see when the detour wins.
Read full theory
Which stop is certain next?
Each edge gives the walking time in minutes. A starts at distance 0. Every other stop starts at ∞ because no route to it is known yet. Choose the open stop with the smallest known distance. This lab breaks ties alphabetically.
Check the neighbours
If B takes 2 minutes to reach and C is 2 minutes beyond B, you can reach C in 4 minutes. If this improves the best time for C, record 4 and its predecessor B. Since every edge weight is non-negative, the distance of each chosen stop is final.
Read the route
Once E is final, follow its predecessors back to A, then read the sequence in reverse. When routes tie, the lab shows the first one it found.
1Explore
Change the walking time from A to C. Predict the quickest route from A to E, then trace each decision.
2Solve it yourself
Make a prediction and test it in the model. If the results differ, compare them and try again with new values.
How you learn here
- ExploreYou change the simulation yourself and watch.
- Predict & solveFirst you predict, then you calculate, click or build – not just type in numbers.
- See the consequencesAfter a mistake the simulation plays out your result and shows the gap.
- Write it downYou note in one sentence what you noticed – only then can you go on.
- New numbersAfter every mistake you get a new variant. Solving it shows you have understood – guessing does not help.
Curriculum: Weighted graphs and tracing a shortest-path algorithm.
For teachers: Set this lab as homework and check the proof codes of your class →
Found a mistake? Tell us – every simulation and every answer is checked by code, but people still make mistakes.
Add the weight of each edge as you travel along the specified path.
For a neighbouring edge, add its weight to the distance to the chosen stop. Keep the smaller of this candidate and the previous distance.
The shortest walk can pass through several intermediate stops. Compare the total walking times for the possible routes.