Skip to content
DSA graphs 7 min read

Bellman-Ford Algorithm: Shortest Paths with Negative Weights

The Bellman-Ford algorithm finds single-source shortest paths even when the graph has negative edge weights — something Dijkstra cannot handle. It works by relaxing every edge repeatedly: V - 1 full passes are enough for the shortest paths to settle, and one extra pass detects a negative cycle. It runs in O(V · E) time — slower than Dijkstra, but more general.

Why V - 1 passes

A shortest path in a graph with V vertices uses at most V - 1 edges (any more would repeat a vertex, and on a non-negative cycle that never helps). Each pass over all edges lets correct distances propagate one edge further, so after V - 1 passes every shortest path is found.

#include <vector>
#include <climits>

struct Edge { int u, v, w; };

// returns dist; sets hasNegCycle if a negative cycle is reachable
std::vector<long long> bellmanFord(int V, const std::vector<Edge>& edges,
                                   int src, bool& hasNegCycle) {
    const long long INF = LLONG_MAX;
    std::vector<long long> dist(V, INF);
    dist[src] = 0;
    for (int i = 0; i < V - 1; i++) {              // V-1 relaxation rounds
        for (const auto& e : edges) {
            if (dist[e.u] != INF && dist[e.u] + e.w < dist[e.v])
                dist[e.v] = dist[e.u] + e.w;
        }
    }
    hasNegCycle = false;                            // extra pass to detect
    for (const auto& e : edges)
        if (dist[e.u] != INF && dist[e.u] + e.w < dist[e.v])
            hasNegCycle = true;
    return dist;
}

Pitfall: Always guard with dist[u] != INF before relaxing. Adding a finite weight to “infinity” can overflow (in C++/Java) or wrongly relax an unreachable vertex.

Detecting negative cycles

If any edge can still be relaxed after V - 1 passes, a negative cycle is reachable — a loop whose total weight is negative, which makes “shortest path” undefined (you could loop forever to drive the cost down). The extra pass above flags exactly this. To find which vertices are affected, mark every vertex relaxed in that final pass and propagate.

Going deeper: A common optimization stops early if a full pass changes nothing (the distances have converged), often finishing well before V - 1 rounds.

Bellman-Ford vs Dijkstra

                     Bellman-Ford        Dijkstra
Negative weights     yes                 no
Negative-cycle test  yes                 no
Time complexity      O(V*E)              O((V+E) log V)
Best for             negative edges,     non-negative
                     small/medium graphs weights, large graphs

When to use it

Choose Bellman-Ford when edges can be negative (currency arbitrage, certain scheduling/cost models) or when you must detect negative cycles. For non-negative weights, Dijkstra is faster. For all-pairs shortest paths on a dense graph, see Floyd-Warshall.

Last updated June 25, 2026
Was this helpful?