The Bellman-Ford algorithm is a single-source shortest path algorithm. It's capable of handling graphs with negative edge weights, unlike Dijkstra's algorithm.
However, it's less efficient than Dijkstra's algorithm for graphs with non-negative edge weights.
pseudocode:
function BellmanFord(Graph, source):
// Step 1: Initialize distances
for each vertex v in Graph:
distance[v] = infinity
distance[source] = 0
// Step 2: Relax edges repeatedly
for i from 1 to |V|-1: // |V| is the number of vertices
for each edge (u, v) in Graph:
if distance[u] + weight(u, v) < distance[v]:
distance[v] = distance[u] + weight(u, v)
predecessor[v] = u
// Step 3: Check for negative-weight cycles
for each edge (u, v) in Graph:
if distance[u] + weight(u, v) < distance[v]:
return "Graph contains a negative-weight cycle"
return distance, predecessor