Menu
Coddy logo textTech

How it works?

Lesson 3 of 9 in Coddy's Bellman-Ford Algorithm - Graph Algorithms course.

Set dist[source] = 0 and every other vertex to a large "infinity". Then relax all edges, and repeat.

Step-by-Step Process:

  1. Make one pass: go through every edge and relax it.
  2. Repeat the pass V - 1 times. Why V-1? A shortest path visits at most V vertices, so it has at most V-1 edges, and each pass locks in at least one more edge of every shortest path.
  3. Any vertex still at infinity is unreachable (report -1).

A single pass usually is not enough, because an edge relaxed late may depend on one relaxed even later. Repeating V-1 times guarantees every shortest path is found (when there is no negative cycle).

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 Bellman-Ford Algorithm - Graph Algorithms