Menu
Coddy logo textTech

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:

  1. Among the unvisited vertices, pick the one u with the smallest dist. If none is reachable, stop.
  2. Mark u visited: its distance is now final.
  3. Relax every edge u -> v of weight w: if dist[u] + w < dist[v], lower dist[v].
  4. 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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Dijkstra's Algorithm - Graph Algorithms