0
0
Data Structures Theoryknowledge~6 mins

Bellman-Ford algorithm in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you need to find the shortest path from one city to all other cities on a map, but some roads might have negative tolls or costs. The Bellman-Ford algorithm helps solve this problem even when some paths reduce the total cost, unlike simpler methods that only work with positive costs.
Explanation
Relaxation Process
The algorithm repeatedly updates the shortest known distance to each city by checking all roads. If it finds a cheaper way to reach a city, it updates the distance. This process is called relaxation and is done for all roads multiple times.
Relaxation gradually improves distance estimates by checking all edges repeatedly.
Number of Iterations
The algorithm runs the relaxation step exactly one less time than the number of cities. This ensures that the shortest paths are found because the longest possible path without cycles can only have that many edges.
Running relaxation V-1 times guarantees shortest paths without cycles.
Negative Weight Cycle Detection
After completing the relaxation steps, the algorithm checks all roads one more time. If it can still find a cheaper path, it means there is a negative weight cycle, which makes shortest paths undefined because you can keep reducing the cost indefinitely.
Extra check detects if negative cycles exist, which break shortest path logic.
Handling Negative Weights
Unlike some algorithms, Bellman-Ford can handle roads with negative costs safely, as long as there are no negative cycles. This makes it useful in situations where costs can decrease, such as discounts or refunds.
Bellman-Ford works with negative weights but not with negative cycles.
Real World Analogy

Imagine you are planning a road trip visiting several cities. Some roads have tolls, but others offer cashback or discounts. You try different routes multiple times to find the cheapest way to reach each city. If you find a loop where you keep getting discounts endlessly, you realize something is wrong with the map.

Relaxation Process → Trying different routes and updating your best known cost to each city when you find a cheaper way
Number of Iterations → Checking all roads enough times to be sure you found the cheapest routes without unnecessary loops
Negative Weight Cycle Detection → Noticing a loop where you keep getting discounts, meaning the trip cost can be reduced forever
Handling Negative Weights → Accepting roads that give discounts or cashback as part of your trip planning
Diagram
Diagram
┌───────────────┐
│ Start Vertex  │
└──────┬────────┘
       │
       ▼
┌───────────────┐       ┌───────────────┐
│ Relax all     │──────▶│ Update shortest│
│ edges V-1     │       │ distances      │
└──────┬────────┘       └──────┬────────┘
       │                       │
       ▼                       ▼
┌───────────────┐       ┌───────────────┐
│ Check for     │◀──────│ Negative      │
│ negative      │       │ weight cycles │
│ cycles        │       └───────────────┘
└───────────────┘
Flowchart showing Bellman-Ford steps: relax edges V-1 times, update distances, then check for negative weight cycles.
Key Facts
RelaxationProcess of updating shortest path estimates by checking all edges.
Negative Weight CycleA cycle whose total edge weights sum to a negative number, causing infinite cost reduction.
Number of IterationsBellman-Ford runs relaxation steps V-1 times, where V is the number of vertices.
Shortest Path GuaranteeBellman-Ford finds shortest paths even with negative edge weights if no negative cycles exist.
Time ComplexityBellman-Ford runs in O(V * E) time, where V is vertices and E is edges.
Code Example
Data Structures Theory
def bellman_ford(edges, vertex_count, start):
    distances = [float('inf')] * vertex_count
    distances[start] = 0
    for _ in range(vertex_count - 1):
        for u, v, w in edges:
            if distances[u] != float('inf') and distances[u] + w < distances[v]:
                distances[v] = distances[u] + w
    for u, v, w in edges:
        if distances[u] != float('inf') and distances[u] + w < distances[v]:
            return None  # Negative cycle detected
    return distances

# Example graph edges: (from, to, weight)
edges = [
    (0, 1, 6),
    (0, 2, 7),
    (1, 2, 8),
    (1, 3, 5),
    (1, 4, -4),
    (2, 3, -3),
    (2, 4, 9),
    (3, 1, -2),
    (4, 0, 2),
    (4, 3, 7)
]

result = bellman_ford(edges, 5, 0)
print(result if result is not None else 'Negative cycle detected')
OutputSuccess
Common Confusions
Bellman-Ford can find shortest paths even with negative cycles.
Bellman-Ford can find shortest paths even with negative cycles. Bellman-Ford detects negative weight cycles but cannot find shortest paths if such cycles exist because costs can decrease endlessly.
Bellman-Ford is faster than Dijkstra's algorithm.
Bellman-Ford is faster than Dijkstra's algorithm. Bellman-Ford is generally slower (O(V*E)) than Dijkstra's (O(E + V log V)) but works with negative weights, unlike Dijkstra's.
Summary
Bellman-Ford finds shortest paths even when some edges have negative weights by repeatedly relaxing all edges.
It runs the relaxation step V-1 times and then checks for negative weight cycles to ensure valid shortest paths.
If a negative weight cycle exists, Bellman-Ford detects it and reports that shortest paths cannot be determined.