How it works?
Lesson 3 of 9 in Coddy's Dijkstra's Algorithm - Graph Algorithms course.
Keep a dist array: dist[source] = 0 and every other vertex starts at "infinity" (a very large number). Keep a visited flag per vertex.
Step-by-Step Process:
- Among the unvisited vertices, pick the one
uwith the smallestdist. If none is reachable, stop. - Mark
uvisited: its distance is now final. - Relax every edge
u -> vof weightw: ifdist[u] + w < dist[v], lowerdist[v]. - Repeat until all reachable vertices are finalized.
At the end, any vertex still at infinity is unreachable (we report it as -1).
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Dijkstra's Algorithm - Graph Algorithms
2The Algorithm
How it works?Pseudo CodeImplementation (Part 1)Implementation (Part 2)