Graph Theory37 sections · 1633 units
Open in Course

Relaxation in Code

Complete code walkthrough

This is the core logic. For a neighbor vv with edge weight ww:

If taking the path through uu is shorter than the current best path to vv, update it.

if dist[u] + w < dist[v] then
    dist[v] := dist[u] + w
    pq.push((dist[v], v))

Relaxation improves distance estimates. Eventually, all estimates become exact shortest distances. The greedy order ensures you finalize nodes in shortest-distance order.