arctic λime

bmssp series 2 - edge relaxation

When you studied Dijkstra in the previous article, you saw it repeatedly perform this operation:

if d + w < dist[v]:
    dist[v] = d + w

This simple update (checking if we found a better path and updating our distance estimate) is called edge relaxation. Dijkstra is not the only algorithm that uses it, this operation is at the heart of nearly all shortest path algorithms.

What is Edge Relaxation?

Edge relaxation is a repeated primitive independent of priority queues. It involves checking whether the current best known distance to a vertex v can be improved by going through an adjacent vertex u plus the edge cost u→v (alternate path). If a shorter path is found, the distance to v is updated.

Think of it like this: we maintain distance estimates that start too high (infinity) and gradually "relax" them down toward their true values. Each relaxation asks: "can I improve this estimate by going through another node?"

Visual example

Here we can see an example with a simple 3-node graph that illustrates how ER works:

\usepackage{tikz-cd}
\begin{document}

\begin{tikzcd}[every cell/.append style={nodes={circle,draw=gray,minimum size=2em}}, row sep=2em, column sep=3em]
A \arrow[r, "4"] \arrow[rd, "2"] & B \\
                                  & C \arrow[u, "1"]
\end{tikzcd}
\end{document}
Initial distances: {'A': 0, 'B': inf, 'C': inf}

Round 1: Relaxing all edges
  Relax A→B (weight 4): 
    d[B] = min(inf, 0 + 4) = 4 ✓ improved
  Relax A→C (weight 2): 
    d[C] = min(inf, 0 + 2) = 2 ✓ improved
  Relax C→B (weight 1): 
    d[B] = min(4, 2 + 1) = 3 ✓ improved
  Distances: {'A': 0, 'B': 3, 'C': 2}
  Changes made: 3

Round 2: Relaxing all edges
  Relax A→B (weight 4): 
    d[B] = min(3, 0 + 4) = 3 (no change)
  Relax A→C (weight 2): 
    d[C] = min(2, 0 + 2) = 2 (no change)
  Relax C→B (weight 1): 
    d[B] = min(3, 2 + 1) = 3 (no change)
  Distances: {'A': 0, 'B': 3, 'C': 2}
  Changes made: 0 → Converged.

Final distances: {'A': 0, 'B': 3, 'C': 2}
Shortest path to B: A→C→B (total: 3)

In the pseudo-code, the comparison/edge relaxation is expressed through the formula d[v] = min(d[v], d[u] + w(u,v)).

Triangle Inequality

The principle behind edge relaxation is triangle inequality intuition, a concept that states that "the direct path between two points is never longer than any indirect path." (which makes sense, taking a detour can't be shorter than going direct). In basic trigonometry, if you have three points forming a triangle, the sum of any two sides is always ≥ the third side.

Why is this important? The idea is that if going through B gives me a shorter route to C, then my current "direct" route A to C must be wrong! By repeatedly fixing violations of the triangle inequality, we converge to the optimal solution.

Next Steps

In the next article, we'll focus on Bellman-Ford algorithm, and how it solves one of the main issues in Dijkstra (spoiler: negative edges). As we continue covering these concepts, we understand the domain (and the problem) better, and we get closed to the bmssp implementation (as well as any future improvements).