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:
- Make one pass: go through every edge and relax it.
- Repeat the pass
V - 1times. 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. - 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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Bellman-Ford Algorithm - Graph Algorithms
2The Algorithm
How it works?Pseudo CodeImplementation (Part 1)Implementation (Part 2)