Pseudo Code
Lesson 4 of 9 in Coddy's Bellman-Ford Algorithm - Graph Algorithms course.
bellmanFord(n, edges, source):
dist = [INF, INF, ...]; dist[source] = 0
repeat (n - 1) times:
for each edge (u -> v, weight w):
if dist[u] != INF and dist[u] + w < dist[v]:
dist[v] = dist[u] + w
replace every remaining INF with -1
return dist
// negative cycle: after n-1 passes, if any edge STILL relaxes, a
// negative cycle exists.- The guard
dist[u] != INFstops us from building paths out of unreachable vertices. - Relaxing the whole edge list is one pass. Bellman-Ford is just that pass repeated
n - 1times.
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)