Introduction

The single-source shortest-paths problem asks for a shortest path from a source ss 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

Core concepts

Shortest-path weight

δ(s,v)={min⁡{w(p):p is an s–v path}if a path exists,∞otherwise.\delta(s,v) = \begin{cases} \min\{w(p): p\text{ is an }s\text{--}v\text{ path}\} & \text{if a path exists},\\ \infty & \text{otherwise}. \end{cases}

If a negative cycle is reachable from ss and can reach vv, one sets δ(s,v)=−∞\delta(s,v) = -\infty.

Relaxation

Text
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 <- u

Every 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)

Text
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: O((V+E)lg⁡V)O((V+E)\lg V). Fibonacci heap: O(E+Vlg⁡V)O(E + V \lg V). Array (dense): O(V2)O(V^2). Every vertex is inserted and extracted once (∣V∣|V| EXTRACT-MIN calls) and every edge is relaxed exactly once, when its tail is extracted (∣E∣|E| RELAX calls, at most ∣E∣|E| DECREASE-KEY calls).

Settled vertices. In this CLRS form, line 9 relaxes every edge out of uu, including edges into vertices already in SS. With nonnegative weights such a relaxation never succeeds. With a negative edge it can lower the dd 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)

Text
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 TRUE

Time. Θ(VE)\Theta(VE). After ∣V∣−1|V|-1 passes every finite shortest path (at most ∣V∣−1|V|-1 edges) has been relaxed in order. A further improving edge witnesses a negative cycle.

How many passes are really needed. After pass ii, every vertex that has a shortest path with at most ii edges holds its final distance, whatever the edge order. The order matters for speed: on the path s→a→b→cs \to a \to b \to c (weights 1), relaxing the edges in the order (s,a),(a,b),(b,c)(s,a), (a,b), (b,c) finishes in one pass, while the order (b,c),(a,b),(s,a)(b,c), (a,b), (s,a) needs all ∣V∣−1=3|V|-1 = 3 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

Text
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. Θ(V+E)\Theta(V+E). 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 uu is extracted, Dijkstra claims u.d=δ(s,u)u.d = \delta(s,u). The proof compares uu with the first unsettled vertex yy on a shortest path to uu. Its settled predecessor has already relaxed it, so d[y]=δ(s,y)d[y]=\delta(s,y). Nonnegative weights imply δ(s,y)≤δ(s,u)\delta(s,y)\le\delta(s,u), giving d[u]≤d[y]=δ(s,y)≤δ(s,u)≤d[u]d[u]\le d[y]=\delta(s,y)\le\delta(s,u)\le d[u]. 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 uu at 2 and its successor xx at 3 before the negative edge v→uv\to u is seen; xx keeps the wrong value 3.

One safe exception. If only edges leaving the source ss are negative and there is no negative cycle, Dijkstra is still correct (CLRS Exercise 24.3-10): the key inequality δ(s,y)≤δ(s,u)\delta(s,y) \le \delta(s,u) needs nonnegative weights only on the part of the path after yy.

Triangle inequality for shortest paths

For every edge (u,v)(u,v), δ(s,v)≤δ(s,u)+w(u,v)\delta(s,v) \le \delta(s,u) + w(u,v), for any weights as long as no negative cycle is reachable from ss. Relaxation enforces this locally until the estimates match the true distances.

Changing all the weights

  • Multiply every weight by c>0c > 0: every path weight is scaled by cc, so the same paths stay shortest.
  • Add c>0c > 0 to every weight: a path with kk edges gains kckc, so paths with fewer edges are favoured. Example: s→ts\to t weight 3 against s→a→ts\to a\to t 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 1+1+1=31+1+1 = 3 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 s,t,x,y,zs,t,x,y,z. Edges (weight): s→t:3s\to t:3, s→y:5s\to y:5, t→x:6t\to x:6, t→y:2t\to y:2, y→t:1y\to t:1, y→x:4y\to x:4, y→z:6y\to z:6, x→z:2x\to z:2, z→x:7z\to x:7, z→s:3z\to s:3. All weights positive.

Extract order from ss: s (0)s\,(0), t (3)t\,(3), y (5)y\,(5), x (9)x\,(9), z (11)z\,(11).

Extractedd[t]d[t]d[y]d[y]d[x]d[x]d[z]d[z]
ss (0)35∞\infty∞\infty
tt (3)359∞\infty
yy (5)35911
xx (9)35911
zz (11)35911

When tt is extracted, t→yt\to y offers 3+2=53+2 = 5, a tie with the current 5, so d[y]d[y] and π[y]=s\pi[y] = s stay. t→xt\to x sets d[x]=9d[x] = 9. When yy is extracted, y→xy\to x offers 5+4=95+4 = 9 (no change) and y→zy\to z sets d[z]=11d[z] = 11. Final distances: s:0,t:3,y:5,x:9,z:11s:0,t:3,y:5,x:9,z:11. Relaxations that succeed: 4 (tt, yy from ss; xx from tt; zz from yy) out of 10 edge relaxations.

Dijkstra relaxation trace from s
Figure 1. Dijkstra from ss on the five-vertex digraph: extract order s,t,y,x,zs,t,y,x,z with final distances 0,3,5,9,110,3,5,9,11.

Negative-edge counterexample for Dijkstra

Vertices s,u,v,xs,u,v,x; edges s→u:2s\to u:2, s→v:4s\to v:4, v→u:−3v\to u:-3, u→x:1u\to x:1. There is no negative cycle. True distances: δ(s,u)=4−3=1\delta(s,u) = 4 - 3 = 1 and δ(s,x)=1+1=2\delta(s,x) = 1 + 1 = 2.

  1. Extract ss (0): d[u]=2d[u] = 2, d[v]=4d[v] = 4.
  2. Extract uu (2): d[x]=3d[x] = 3.
  3. Extract xx (3): no outgoing edges.
  4. Extract vv (4): relaxing v→uv\to u lowers d[u]d[u] to 1 in the CLRS form (a variant that skips settled vertices leaves it at 2), but uu is already settled, so u→xu\to x is never relaxed again.

Dijkstra reports d[x]=3d[x] = 3, while δ(s,x)=2\delta(s,x) = 2. Without the edge u→xu\to x, the CLRS form would end with the correct d[u]=1d[u] = 1 by luck; the extra vertex makes the failure independent of the variant.

Negative edge that breaks Dijkstra
Figure 2. Digraph with a negative edge v→uv\to u of weight −3-3 and an edge u→xu\to x of weight 1: Dijkstra settles xx with d[x]=3d[x]=3 before the negative edge is relaxed, while the true shortest-path weight of xx is 22.

Bellman-Ford detects a negative cycle

Take the three vertices s,u,vs, u, v with edges s→u:2s\to u:2, s→v:4s\to v:4, v→u:−3v\to u:-3, and add u→v:1u\to v:1. Cycle u→v→uu\to v\to u has weight −2-2. After ∣V∣−1=2|V|-1 = 2 relaxation passes, the check pass still finds an improving edge, so Bellman-Ford returns FALSE.

Undirected negative edge. An undirected edge {u,v}\{u,v\} of weight −1-1 is two directed edges u→vu\to v and v→uv\to u, which form a negative cycle of weight −2-2. So shortest paths with negative undirected edges are not well defined in this model.

DAG shortest and longest paths

Edges s→a:2s\to a:2, s→b:6s\to b:6, a→b:3a\to b:3, a→c:4a\to c:4, b→c:−2b\to c:-2; topological order s,a,b,cs, a, b, c. One pass gives d[a]=2d[a] = 2, d[b]=min⁡(6,2+3)=5d[b] = \min(6, 2+3) = 5, d[c]=min⁡(2+4,5−2)=3d[c] = \min(2+4, 5-2) = 3. For the longest paths, run the same pass with negated weights: the longest ss--cc path is s→a→cs\to a\to c of weight 6.

Potentials and reweighting

Related material. If h(v)h(v) is a potential (for example h(v)=δ(s′,v)h(v)=\delta(s',v) from a super-source), the reduced weight wh(u,v)=w(u,v)+h(u)−h(v)w_h(u,v)=w(u,v)+h(u)-h(v) 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 hh, then Dijkstra from every vertex on whw_h. 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

SituationAlgorithm
All weights equal to a positive constant (or unweighted)BFS; multiply hop distances by that constant
Nonnegative weights, general digraphDijkstra
Arbitrary weights, need cycle reportBellman-Ford
Digraph known to be a DAGDAG topo pass
All pairs, dense, negatives allowedFloyd-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 ∣V∣−1|V|-1 times); the DAG algorithm is a single topo pass.
  • Predecessor subgraph of correct distances is a shortest-paths tree rooted at ss.
  • 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 c>0c > 0 keeps shortest paths; adding a constant can change them.
  • PASS COUNTS. Bellman-Ford needs at most ∣V∣−1|V|-1 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 dd 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 O((V+E)lg⁡V)O((V+E)\lg V); Bellman-Ford Θ(VE)\Theta(VE); DAG Θ(V+E)\Theta(V+E).

Summary

Sources

  1. 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).

Next steps