BELLMAN-FORD ALGORITHM
TEST TIME ON THE CELEBRITY PROBLEM
URL:
BELLMAN-FORD ALGORITHM
EXPLANATION
1. Single Source Shortest Paths: The Bellman-Ford algorithm is a dynamic
programming approach to find the shortest paths from a single source vertex
to all other vertices in a weighted, directed graph.
2.Negative Edge Weights: Unlike Dijkstra's algorithm, Bellman-Ford can
handle graphs with negative edge weights. This makes it suitable for a wider
range of real-world scenarios where negative weights may be present.
BELLMAN-FORD ALGORITHM
EXPLANATION
3.Iterative Relaxation: The algorithm works by iteratively relaxing
(updating) the estimated shortest distances to vertices. It repeats this
relaxation process for V-1 times, where V is the number of vertices in the
graph.
4.Detection of Negative Cycles: Bellman-Ford also has the ability to detect
the presence of negative weight cycles in the graph. If, after V-1
iterations, there are still relaxations that can be made, it indicates the
existence of a negative weight cycle.
BELLMAN-FORD ALGORITHM
EXPLANATION
5.Use Cases: The Bellman-Ford algorithm is used in various applications,
including network routing with potential packet delays, financial modeling, and
any scenario where negative weights or cycles might be encountered in the
graph. It's a versatile algorithm for solving the single-source shortest path
problem.
BELLMAN-FORD ALGORITHM
WHERE IT WORKS
The Bellman-Ford algorithm works for weighted, directed graphs, including those
with negative weight edges, and it can detect negative weight cycles.
BELLMAN-FORD ALGORITHM
IMPORTANT TERMINOLOGY
`V`: The number of vertices in the graph.
`E`: The number of edges in the graph.
`dist[]`: An array to store the shortest distances from the source vertex to
all other vertices.
`CreateEdge`: A class representing an edge with source, destination, and
weight.
BELLMAN-FORD ALGORITHM
ALGORITHM
1. Initialize the `dist` array, setting the distance to the source vertex as 0
and all other distances as infinity.
2. Repeat the following V-1 times (V is the number of vertices):
For each edge (u, v) with weight w, if `dist[u] + w` is smaller than
`dist[v]`, update `dist[v]` to `dist[u] + w`.
3. Check for negative weight cycles by iterating through all edges and
performing the same relaxation step. If any `dist[u] + w` is smaller than
`dist[v`, a negative weight cycle exists.
4. If no negative weight cycle is found, print the shortest distances from the
source vertex to all other vertices.
BELLMAN-FORD ALGORITHM
PSEUDOCODE
BellmanFord(graph, source):
Initialize dist[] with infinity values
Set dist[source] to 0
Repeat V-1 times:
For each edge (u, v) with weight w:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
Check for negative weight cycles:
For each edge (u, v) with weight w:
if dist[u] + w < dist[v]:
Print "Graph contains negative weight cycle"
return
Print the shortest distances from the source vertex to all other vertices
BELLMAN-FORD ALGORITHM
public class Main { void BellmanFord(int src) {
static class CreateGraph { int dist[] = new int[V];
class CreateEdge { for (int i = 0; i < V; ++i)
int src, dest, weight; dist[i] = Integer.MAX_VALUE;
CreateEdge(int src, int dest, dist[src] = 0;
int weight) { for (int i = 1; i < V; ++i) {
this.src = src; for (int j = 0; j < E; ++j) {
this.dest = dest; int u = edge[j].src;
this.weight = weight; int v = edge[j].dest;
} int w = edge[j].weight;
} if (dist[u] !=
int V, E; Integer.MAX_VALUE && dist[u] + w <
CreateEdge edge[]; dist[v])
CreateGraph(int v, int e) { dist[v] = dist[u] + w;
V = v; }
E = e; }
edge = new CreateEdge[e]; for (int j = 0; j < E; ++j) {
} int u = edge[j].src;
int v = edge[j].dest;
BELLMAN-FORD ALGORITHM
int w = edge[j].weight; CreateGraph graph = new CreateGraph(V, E);
if (dist[u] != Integer.MAX_VALUE graph.edge[0] = graph.new
&& dist[u] + w < dist[v]) { CreateEdge(0, 1, 5);
System.out.println("Graph contains negative graph.edge[1] = graph.new
weight cycle"); CreateEdge(0, 2, 4);
return; graph.edge[2] = graph.new
} CreateEdge(1, 3, 3);
} graph.edge[3] = graph.new
printSolution(dist); CreateEdge(2, 1, 6);
} graph.edge[4] = graph.new
void printSolution(int dist[]) { CreateEdge(3, 2, 2);
System.out.println("Vertex graph.edge[5] = graph.new
Distance from Source"); CreateEdge(1, 4, -4);
for (int i = 0; i < V; ++i) graph.edge[6] = graph.new
System.out.println(i + "\t\t" + dist[i]); CreateEdge(4, 2, 2);
} graph.BellmanFord(0); // 0 is the
} source vertex
public static void main(String[] args) { }}
int V = 5;
int E = 7;
BELLMAN-FORD ALGORITHM
TIME AND SPACE COMPLEXITY
Time Complexity: O(VE) where V is the number of vertices and E is the number of
edges.
Space Complexity: O(V) for the `dist` array.
BELLMAN-FORD ALGORITHM
DIJKSTRA’S ALGORITHM VS BELLMAN FORD ALGORITHM
Aspect Dijkstra's Algorithm Bellman-Ford Algorithm
Negative edge weights are
Edge Weight Non-negative edge weights allowed
Can handle negative edge
Provides shortest paths with weights and detect negative
Optimality non-negative edge weights weight cycles
Typically uses priority queue Uses an array to store
Data Structures with a minimum key distances
Suitable for graphs with
Suitable for graphs with non- negative edge weights or
Suitable For negative edge weights cycles
Used when negative weights or
Used for finding shortest cycles may be present, e.g.,
paths in non-negative weighted network routing with possible
Common Usage graphs, e.g., road networks packet delays
INTERVIEW QUESTIONS
1. What is the Bellman-Ford algorithm, and in what type of graphs is it
typically used?
Answer: The Bellman-Ford algorithm is used to find the shortest paths from
a single source vertex to all other vertices in a weighted, directed
graph. It can handle graphs with negative edge weights and detect negative
weight cycles. It is typically used in scenarios where negative weights or
cycles may be present in the graph.
INTERVIEW QUESTIONS
2. Can you explain the basic idea behind the Bellman-Ford algorithm?
Answer: The Bellman-Ford algorithm uses a dynamic programming approach. It
iteratively relaxes the estimated shortest distances to vertices,
repeating this process for V-1 times (where V is the number of vertices).
In each iteration, it considers all edges in the graph and updates the
distance if a shorter path is found. This process continues until no
further improvements can be made, or a negative weight cycle is detected.
INTERVIEW QUESTIONS
3. What is the purpose of the V-1 iterations in the Bellman-Ford
algorithm?
Answer: The V-1 iterations in the Bellman-Ford algorithm ensure that the
shortest paths are found in the majority of cases, assuming there are no
negative weight cycles. In a graph with V vertices, the longest possible
path between two vertices is V-1 edges. Therefore, V-1 iterations are
sufficient to find the shortest paths.
INTERVIEW QUESTIONS
4. How does the Bellman-Ford algorithm detect the presence of a negative
weight cycle in a graph?
Answer: After the V-1 iterations, the Bellman-Ford algorithm checks all
edges one more time. If it finds any further improvements in distances, it
indicates the presence of a negative weight cycle. If a vertex's distance
can be reduced during this additional iteration, it implies that the graph
contains a negative weight cycle.
INTERVIEW QUESTIONS
5.Can you provide an example scenario where the Bellman-Ford algorithm
would be a better choice than Dijkstra's algorithm?
Answer: The Bellman-Ford algorithm is a better choice when working with
network routing problems in scenarios where negative delays, costs, or
congestion can occur. For example, in a communication network, packet
delays can sometimes be negative (indicating faster routing). Bellman-Ford
can accurately model such scenarios, while Dijkstra's algorithm cannot
handle negative weights.
INTERVIEW QUESTIONS
6.What distinguishes the Bellman-Ford algorithm from Dijkstra's
algorithm?
1. Edge Weights: Bellman-Ford can handle negative edge weights, while
Dijkstra's algorithm works with non-negative weights.
2. Negative Cycle Detection: Bellman-Ford can detect negative weight
cycles, which Dijkstra's algorithm cannot.
3. Use Cases: Use Bellman-Ford when dealing with negative weights or cycle
detection; choose Dijkstra for non-negative weights in the graph.
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra
https://learn.codemithra.com
codemithra@ethnus.com +91 7815 095 095 +91 9019 921 340