LibraryBellman-Ford Algorithm

Bellman-Ford Algorithm

Learn about Bellman-Ford Algorithm as part of GATE Computer Science - Algorithms and Data Structures

Bellman-Ford Algorithm: Finding Shortest Paths with Negative Edges

The Bellman-Ford algorithm is a fundamental graph algorithm used to find the shortest paths from a single source vertex to all other vertices in a weighted directed graph. Its key advantage over Dijkstra's algorithm is its ability to handle graphs with negative edge weights, a scenario where Dijkstra's greedy approach fails.

Why Bellman-Ford? The Problem with Negative Weights

Dijkstra's algorithm works by greedily selecting the unvisited vertex with the smallest known distance from the source. This works perfectly when all edge weights are non-negative because once a vertex is visited, its shortest path is finalized. However, if there are negative edge weights, a path that initially seems longer might become shorter by traversing a negative edge later. Bellman-Ford addresses this by iteratively relaxing all edges, ensuring that even paths involving negative weights are considered.

How Bellman-Ford Works: The Relaxation Process

The algorithm initializes the distance to the source vertex as 0 and all other vertices as infinity. It then performs |V| - 1 iterations, where |V| is the number of vertices. In each iteration, it goes through all the edges (u, v) with weight w and 'relaxes' the edge. Relaxation means checking if the path to v through u is shorter than the current known shortest path to v. If

code
distance[u] + w < distance[v]
, then
code
distance[v]
is updated.

Bellman-Ford guarantees shortest paths by relaxing all edges |V|-1 times.

After |V|-1 passes, if all edge weights are non-negative, the shortest paths are found. This is because the longest possible simple shortest path can have at most |V|-1 edges.

The core idea is that in a graph with no negative cycles, the shortest path from a source to any other vertex can have at most |V|-1 edges. By relaxing every edge |V|-1 times, the algorithm ensures that the shortest path information propagates through the graph, even if it involves traversing multiple edges. Each pass effectively extends the potential shortest paths by one edge.

How many iterations does the Bellman-Ford algorithm perform to find shortest paths in a graph with V vertices?

|V| - 1 iterations.

Detecting Negative Cycles

A critical feature of Bellman-Ford is its ability to detect negative cycles. A negative cycle is a cycle in the graph where the sum of the edge weights is negative. If such a cycle is reachable from the source, the shortest path is undefined because one can keep traversing the cycle to infinitely decrease the path cost. After |V|-1 iterations, the algorithm performs one additional pass. If any edge can still be relaxed in this |V|-th pass, it indicates the presence of a negative cycle.

If a negative cycle is detected, the algorithm reports it and terminates, as shortest paths are not well-defined in such graphs.

The Bellman-Ford algorithm iteratively updates the shortest distance estimates for each vertex. Imagine a table where each row represents a vertex and columns represent the iteration number. Initially, only the source has a distance of 0. In each iteration, we check every edge (u, v) with weight w. If dist[u] + w < dist[v], we update dist[v] for the current iteration. This process is repeated |V|-1 times. The final check for negative cycles involves one more pass over all edges.

📚

Text-based content

Library pages focus on text content

Time Complexity and Applications

The time complexity of the Bellman-Ford algorithm is O(|V| * |E|), where |V| is the number of vertices and |E| is the number of edges. This is because it iterates |V| times, and in each iteration, it examines all |E| edges. Despite its higher complexity compared to Dijkstra's (O(|E| + |V| log |V|) with a priority queue), Bellman-Ford is invaluable in scenarios involving negative edge weights, such as in certain routing protocols or when detecting arbitrage opportunities in financial markets.

FeatureDijkstra's AlgorithmBellman-Ford Algorithm
Negative Edge WeightsNot supportedSupported
Negative CyclesCannot detect/handleCan detect
Time ComplexityO(E + V log V)O(V * E)
Primary Use CaseNon-negative weightsGraphs with negative weights

Learning Resources

Bellman-Ford Algorithm - GeeksforGeeks(documentation)

A comprehensive explanation of the Bellman-Ford algorithm, including its implementation, time complexity, and how it detects negative cycles.

Bellman-Ford Algorithm | Algorithms | Computer Science(tutorial)

A step-by-step tutorial that breaks down the Bellman-Ford algorithm, its logic, and provides pseudocode for understanding.

Bellman-Ford Algorithm - YouTube(video)

A visual explanation of the Bellman-Ford algorithm, demonstrating how it works with negative edge weights and detects negative cycles.

Shortest Paths: Bellman-Ford Algorithm(blog)

This resource provides a clear explanation of the Bellman-Ford algorithm, its working principle, and its applications with illustrative examples.

Bellman-Ford Algorithm - Wikipedia(wikipedia)

The Wikipedia page offers a detailed overview of the Bellman-Ford algorithm, its history, mathematical formulation, and variations.

Introduction to Algorithms (CLRS) - Chapter 24: Single-Source Shortest Paths(paper)

An excerpt from the renowned 'Introduction to Algorithms' textbook, covering the Bellman-Ford algorithm in depth, suitable for advanced understanding.

Graph Algorithms: Bellman-Ford(paper)

Lecture notes from Princeton University that provide a concise yet thorough explanation of the Bellman-Ford algorithm and its properties.

Bellman-Ford Algorithm Explained - Codecademy(blog)

A beginner-friendly explanation of the Bellman-Ford algorithm, focusing on its core concepts and how it differs from Dijkstra's.

Understanding the Bellman-Ford Algorithm(blog)

This article breaks down the Bellman-Ford algorithm with clear explanations and code examples, making it accessible for learners.

Bellman-Ford Algorithm - Stanford University(documentation)

A concise explanation of the Bellman-Ford algorithm, focusing on its application in finding shortest paths and detecting negative cycles.