Bellman Ford’s Algorithm
course Algorithmics
last review @April 2, 2023
mastery rookie
assignment
progress not started
Weight
Files
date
due date
notes
days left
Description
The Bellman-Ford algorithm is used to find the shortest path between two points in a
graph. It works by looking at all possible paths and choosing the one with the smallest
total distance. This can be really helpful when you need to figure out how to get from one
place to another, like finding the quickest route from your house to your friend's house.
Steps
Bellman Ford’s Algorithm 1
, The algorithm is implemented in three steps:
1. Initialize the graph by setting the distance to all vertices to infinity and the
predecessor to null. The distance from the source to itself is set to 0.
2. Relax edges repeatedly. Repeat the process |V|-1 times, where |V| is the number of
vertices in the graph. For each edge (u, v) with weight w in edges, if distance[u] + w
< distance[v], then update distance[v] to distance[u] + w and predecessor[v] to u.
3. Check for negative-weight cycles. For each edge (u, v) with weight w in edges, if
distance[u] + w < distance[v], then there is a negative-weight cycle in the graph. Find
the cycle and return an error.
Pseudocode:
function BellmanFord(list vertices, list edges, vertex source) is
// This implementation takes in a graph, represented as
// lists of vertices (represented as integers [0..n-1]) and edges,
// and fills two arrays (distance and predecessor) holding
// the shortest path from the source to each vertex
distance := list of size n
predecessor := list of size n
// Step 1: initialize graph
for each vertex v in vertices do
distance[v] := inf // Initialize the distance to all vertices to infinity
predecessor[v] := null // And having a null predecessor
distance[source] := 0 // The distance from the source to itself is, of course, zero
// Step 2: relax edges repeatedly
Bellman Ford’s Algorithm 2