Lesson 25 — Ultimate: Dijkstra with path
Boss level: the route, not just the cost
To return the actual route, Dijkstra also records prev[v] = the node we came from on the cheapest way to v. At the end, start at the target and follow prev backwards to the start, then reverse the list.
# prev = {"B": "A", "C": "B", "D": "C"}
# D -> C -> B -> A, reversed: A, B, C, D
Why it matters: a navigation app that only says "it is 12 minutes away" is useless — you need the turns. Reconstructing a path from "came from" links is used in games, maps and puzzle solvers.
Finish the path reconstruction: walk prev from the target back to the start, then reverse.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based Algorithms Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Algorithms Foundations books — check the publication year so the examples match current versions.
Search Amazon →