1. Gyanastra
  2. Algorithms
  3. Bellman-Ford Algorithm
  4. Bellman-Ford: Shortest Path Contains at Most n-1 Edges
Free lesson, no account needed 4 min read 9 practice questions after it
Asked in past papers3 times
  • GATE CSE202620132010

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 nn nodes, a shortest path never uses more than n−1n-1 edges. This bound is what makes the algorithm correct and limits it to n−1n-1 rounds of edge relaxation.

Key concepts

  • Edge bound for shortest paths: if a graph has nn nodes, the shortest path in the graph will never contain more than n−1n-1 edges.
  • Why nn edges cannot help: in a simple graph (no self loops) with nn nodes and nn edges, a cycle is definitely possible with those nn nodes and nn edges. A path using nn nodes and nn 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 n−1n-1 edge bound is therefore stated for graphs with no negative weight cycles (cycles may have positive or zero weight).
Graph diagram: Bellman-Ford: Shortest Path Contains at Most n-1 Edges
Figure. Bellman-Ford: Shortest Path Contains at Most n-1 Edges

How it works

  1. Take a graph with nn nodes.
  2. Any shortest path uses at most n−1n-1 edges.
  3. Suppose a shortest path used nn edges. With nn nodes and nn edges in a simple graph, a cycle exists, so the path would be a cycle.
  4. If that cycle has positive weight, removing the entire cycle gives a path of strictly smaller weight — contradicting that the path was shortest.
  5. If the graph has a negative weight cycle, no shortest path exists at all.
  6. Conclusion: with nn nodes, at most n−1n-1 edges are enough in the path.

Formulas

  • n−1n - 1 — maximum number of edges in a shortest path, where nn is the number of nodes in the graph.
  • For n=3n = 3: n−1=3−1=2n - 1 = 3 - 1 = 2 — 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 (n−1=3−1=2n-1 = 3-1 = 2).
  • 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 n−1n-1 edges.

Why more than n−1n-1 edges cannot help:

  • Consider a simple graph (no self loops) with 3 nodes and 3 edges (nn nodes, nn edges).
  • Whenever there are nn nodes and nn edges, a cycle is definitely possible with those nn nodes and nn edges.
  • If a shortest path contained nn nodes and nn 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 nn nodes contains at most n−1n-1 edges.
  • For 3 nodes, the shortest path uses at most 2 edges.
  • nn nodes and nn edges in a simple graph implies a cycle exists.
  • A shortest path with nn 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 n−1n-1 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 dkd_{k}(u) of the DP in the synchronous formulation).
    Asked inGATE 2026
  • dkd_{k}(u) is non-increasing in k and stabilises once k ≥ n-1 (when no negative cycle is reachable);
    d0d_{0}(s)=0 and d0d_{0}(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 O(V⋅E)O(V\cdot E) time; with an adjacency matrix it is O(V3)O(V^{3}), 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.
Pages 1–2 of 2