BeginnerCoder

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.

Want it another way?
Task

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.

Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Algorithms Foundations

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.