Algorithms · Bellman-Ford Algorithm
Bellman-Ford: Shortest Path Contains at Most n-1 Edges
Overview
Bellman-Ford relies on a structural bound: in a graph with nodes, a shortest path never uses more than edges. This bound is what makes the algorithm correct and limits it to rounds of edge relaxation.
Key concepts
- Edge bound for shortest paths: if a graph has nodes, the shortest path in the graph will never contain more than edges.
- Why edges cannot help: in a simple graph (no self loops) with nodes and edges, a cycle is definitely possible with those nodes and edges. A path using nodes and edges would in fact be a cycle.
- Positive weight cycles: if a cycle were the shortest path, one of its edges could be removed to get a shorter path — provided the cycle is a positive weight cycle. So such a path cannot be shortest.
- Negative weight cycles: if there is a negative weight cycle, the shortest path cannot be found at all. The edge bound is therefore stated for graphs with no negative weight cycles (cycles may have positive or zero weight).

How it works
- Take a graph with nodes.
- Any shortest path uses at most edges.
- Suppose a shortest path used edges. With nodes and edges in a simple graph, a cycle exists, so the path would be a cycle.
- If that cycle has positive weight, removing the entire cycle gives a path of strictly smaller weight — contradicting that the path was shortest.
- If the graph has a negative weight cycle, no shortest path exists at all.
- Conclusion: with nodes, at most edges are enough in the path.
Formulas
- — maximum number of edges in a shortest path, where is the number of nodes in the graph.
- For : — the shortest path uses at most 2 edges.
Worked examples
3-node graph (nodes A, B, C):
- The shortest path contains at most 2 edges ().
- A to B contains 1 edge (A to B).
- A to C to B contains 2 edges.
- In every case the shortest path never contains more than edges.
Why more than edges cannot help:
- Consider a simple graph (no self loops) with 3 nodes and 3 edges ( nodes, edges).
- Whenever there are nodes and edges, a cycle is definitely possible with those nodes and edges.
- If a shortest path contained nodes and edges, the path would in fact be a cycle.
- If a cycle were part of the shortest path, removing the whole cycle would give a shorter path — provided the cycle has positive weight.
- If there is a negative weight cycle, the shortest path cannot be found at all.
Revision summary
- Shortest path in a graph with nodes contains at most edges.
- For 3 nodes, the shortest path uses at most 2 edges.
- nodes and edges in a simple graph implies a cycle exists.
- A shortest path with edges would be a cycle; removing one edge of a positive weight cycle gives a shorter path, so such a path cannot be shortest.
- With a negative weight cycle, no shortest path exists; the edge bound is stated for graphs without negative weight cycles.
- The argument assumes a simple graph (no self loops).
Previous-year exam insights
- Bellman-Ford performs n-1 rounds of relaxation over all edges; after the k-th round, d[u] is at most the weight of a shortest path from s to u using at most k edges (equal to (u) of the DP in the synchronous formulation).
Asked inGATE 2026 - (u) is non-increasing in k and stabilises once k ≥ n-1 (when no negative cycle is reachable);
(s)=0 and (u)=infinity for u ≠ s.
Asked inGATE 2026 - A negative-weight cycle is detected if any edge can still be relaxed after n-1 rounds; in that case shortest paths are undefined (-infinity) for vertices reachable from the cycle.
- Bellman-Ford runs in time; with an adjacency matrix it is , and it works with negative edge weights, unlike Dijkstra.
- In an undirected graph, any edge of negative weight forms a negative cycle (go back and forth), so undirected graphs with negative weights have no well-defined shortest paths.
Asked inGATE 2026 - When no negative cycle is reachable, there is always a shortest path that is simple (no repeated vertices; a zero-weight cycle can be dropped), so it has at most n-1 edges and at most n vertices.