Introduction
The single-source shortest-paths problem asks for a shortest path from a source to every vertex of a weighted directed graph. Three algorithms cover the GATE syllabus cases: Dijkstra (nonnegative weights), Bellman-Ford (arbitrary weights, detects negative cycles), and a linear DAG scan that uses a topological order. Unweighted graphs reduce to BFS.
Prerequisites
- Breadth-First Search: unweighted shortest paths and layers.
- Binary Heaps: priority queue for Dijkstra.
- Topological Sort: DAG algorithm.
- Greedy Algorithms: Dijkstra as a greedy choice under nonnegative weights.
Core concepts
Shortest-path weight
If a negative cycle is reachable from and can reach , one sets .
Relaxation
RELAX(u, v, w)
1 if v.d > u.d + w(u, v) then
2 v.d <- u.d + w(u, v)
3 v.pi <- uEvery shortest-path algorithm initialises v.d <- infinity, s.d <- 0, and repeatedly relaxes edges. Correctness rests on the path-relaxation property: relaxing the edges of a shortest path in order produces the correct distance.
Algorithms and pseudocode
Pseudocode uses <- for assignment and a min-priority queue for Dijkstra.
Dijkstra (nonnegative weights)
DIJKSTRA(G, w, s)
1 for each v in G.V: v.d <- infinity; v.pi <- NIL
2 s.d <- 0
3 S <- empty set
4 Q <- G.V
5 while Q is not empty
6 u <- EXTRACT-MIN(Q) // keyed by d
7 S <- S union {u}
8 for each v in Adj[u]
9 RELAX(u, v, w)Time. Binary heap: . Fibonacci heap: . Array (dense): . Every vertex is inserted and extracted once ( EXTRACT-MIN calls) and every edge is relaxed exactly once, when its tail is extracted ( RELAX calls, at most DECREASE-KEY calls).
Settled vertices. In this CLRS form, line 9 relaxes every edge out of , including edges into vertices already in . With nonnegative weights such a relaxation never succeeds. With a negative edge it can lower the value of a settled vertex, but that improvement is never propagated, because the settled vertex is not extracted again. Many textbook variants skip settled vertices altogether. The four-vertex counterexample below defeats both forms.
Assumption that always holds for correctness. Every edge weight is nonnegative. Negative edges can make Dijkstra return wrong distances even without a negative cycle.
Bellman-Ford (arbitrary weights)
BELLMAN-FORD(G, w, s)
1 for each v in G.V: v.d <- infinity; v.pi <- NIL
2 s.d <- 0
3 for i <- 1 to |V| - 1
4 for each edge (u, v) in G.E
5 RELAX(u, v, w)
6 for each edge (u, v) in G.E
7 if v.d > u.d + w(u, v) then
8 return FALSE // negative cycle reachable from s
9 return TRUETime. . After passes every finite shortest path (at most edges) has been relaxed in order. A further improving edge witnesses a negative cycle.
How many passes are really needed. After pass , every vertex that has a shortest path with at most edges holds its final distance, whatever the edge order. The order matters for speed: on the path (weights 1), relaxing the edges in the order finishes in one pass, while the order needs all passes. A pass with no change means the distances are final, so an implementation may stop early. That shortcut does not replace the negative-cycle check, which still needs a pass after the last change.
DAG shortest paths
DAG-SHORTEST-PATHS(G, w, s)
1 topologically sort the vertices of G
2 for each v in G.V: v.d <- infinity; v.pi <- NIL
3 s.d <- 0
4 for each vertex u in topological order
5 for each v in Adj[u]
6 RELAX(u, v, w)Time. . Negative weights are allowed because a DAG has no cycles at all. Negating every weight turns it into a longest-path algorithm, which is critical-path analysis in project networks. Longest simple paths are hard in general graphs, but easy in a DAG.
Mathematical explanation
Why Dijkstra needs nonnegative weights
When is extracted, Dijkstra claims . The proof compares with the first unsettled vertex on a shortest path to . Its settled predecessor has already relaxed it, so . Nonnegative weights imply , giving . A negative edge can make a later vertex offer a better path to an already settled vertex, breaking the claim. The counterexample in the worked examples settles at 2 and its successor at 3 before the negative edge is seen; keeps the wrong value 3.
One safe exception. If only edges leaving the source are negative and there is no negative cycle, Dijkstra is still correct (CLRS Exercise 24.3-10): the key inequality needs nonnegative weights only on the part of the path after .
Triangle inequality for shortest paths
For every edge , , for any weights as long as no negative cycle is reachable from . Relaxation enforces this locally until the estimates match the true distances.
Changing all the weights
- Multiply every weight by : every path weight is scaled by , so the same paths stay shortest.
- Add to every weight: a path with edges gains , so paths with fewer edges are favoured. Example: weight 3 against with weights 1 and 1. The two-edge path (2) is shortest; after adding 2 to every edge it costs 6, against 5 for the direct edge, so the shortest path changes. Adding a constant to remove negative weights is therefore wrong; Johnson's reweighting (below) is the correct method.
- Square every (positive) weight: shortest paths can change. A direct edge of weight 2 beats a three-edge path of weights 1, 1, 1 (2 against 3); after squaring, the path costs and the edge costs 4.
- Contrast with MSTs, which are unchanged by all three transforms (Minimum Spanning Trees).
Worked examples
Dijkstra on five vertices
Vertices . Edges (weight): , , , , , , , , , . All weights positive.
Extract order from : , , , , .
| Extracted | ||||
|---|---|---|---|---|
| (0) | 3 | 5 | ||
| (3) | 3 | 5 | 9 | |
| (5) | 3 | 5 | 9 | 11 |
| (9) | 3 | 5 | 9 | 11 |
| (11) | 3 | 5 | 9 | 11 |
When is extracted, offers , a tie with the current 5, so and stay. sets . When is extracted, offers (no change) and sets . Final distances: . Relaxations that succeed: 4 (, from ; from ; from ) out of 10 edge relaxations.

Negative-edge counterexample for Dijkstra
Vertices ; edges , , , . There is no negative cycle. True distances: and .
- Extract (0): , .
- Extract (2): .
- Extract (3): no outgoing edges.
- Extract (4): relaxing lowers to 1 in the CLRS form (a variant that skips settled vertices leaves it at 2), but is already settled, so is never relaxed again.
Dijkstra reports , while . Without the edge , the CLRS form would end with the correct by luck; the extra vertex makes the failure independent of the variant.

Bellman-Ford detects a negative cycle
Take the three vertices with edges , , , and add . Cycle has weight . After relaxation passes, the check pass still finds an improving edge, so Bellman-Ford returns FALSE.
Undirected negative edge. An undirected edge of weight is two directed edges and , which form a negative cycle of weight . So shortest paths with negative undirected edges are not well defined in this model.
DAG shortest and longest paths
Edges , , , , ; topological order . One pass gives , , . For the longest paths, run the same pass with negated weights: the longest -- path is of weight 6.
Potentials and reweighting
Related material. If is a potential (for example from a super-source), the reduced weight preserves shortest paths and can eliminate negatives when no negative cycle exists. Johnson's all-pairs algorithm uses one complete Bellman-Ford run to build , then Dijkstra from every vertex on . Johnson is related material beyond the main syllabus treatment; the idea explains why "make weights nonnegative" is sometimes possible and why it fails when a negative cycle exists.
Choosing among BFS, Dijkstra, Bellman-Ford and DAG
| Situation | Algorithm |
|---|---|
| All weights equal to a positive constant (or unweighted) | BFS; multiply hop distances by that constant |
| Nonnegative weights, general digraph | Dijkstra |
| Arbitrary weights, need cycle report | Bellman-Ford |
| Digraph known to be a DAG | DAG topo pass |
| All pairs, dense, negatives allowed | Floyd-Warshall (next note) |
Writing the table in the answer sheet before tracing saves time when the question only says "compute shortest paths".
Important properties and observations
- Dijkstra is greedy; Bellman-Ford is DP-like (relaxing all edges times); the DAG algorithm is a single topo pass.
- Predecessor subgraph of correct distances is a shortest-paths tree rooted at .
- Reweighting all edges by a potential (Johnson) preserves shortest paths and can make weights nonnegative; that is the bridge to all-pairs algorithms.
- Common misconceptions.
- "Dijkstra works with one negative edge if there is no negative cycle." False; the counterexample above has no negative cycle.
- "Bellman-Ford always returns the distances even with a negative cycle." It returns FALSE; distances of vertices affected by the cycle are not well-defined as finite numbers.
- "DAG shortest paths forbid negative weights." They allow them; forbidding cycles is enough.
GATE examination perspective
- DIRECTED OR UNDIRECTED. Record this before tracing; an undirected edge is two directed edges, and an undirected negative edge is already a negative cycle.
- WEIGHT TRANSFORMS. Scaling by keeps shortest paths; adding a constant can change them.
- PASS COUNTS. Bellman-Ford needs at most passes; fewer suffice when the edge order happens to follow the shortest paths.
- CHOOSE THE ALGORITHM. Nonnegative: Dijkstra; negatives possible: Bellman-Ford; DAG: topo pass; unweighted: BFS.
- TRACE DIJKSTRA. Show the priority queue and values after each extract.
- ONE NEGATIVE EDGE. Be ready with a small counterexample (the four-vertex graph above), and remember the exception for negative edges that leave only the source.
- NEGATIVE CYCLE TEST. The extra Bellman-Ford pass.
- COMPLEXITY. Dijkstra ; Bellman-Ford ; DAG .
Summary
Sources
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009: Chapter 24, Sections 24.1 (Bellman-Ford), 24.2 (DAG shortest paths), 24.3 (Dijkstra); Exercises 24.3-2 (negative edges) and 24.3-10 (negative edges leaving the source).