Exercises — Bellman-Ford algorithm — DP approach, negative cycles detection, O(VE)
This page drills the parent note: Bellman-Ford. Everything used here — the relax rule, the passes, negative-cycle detection — is built there. We reuse only these ideas; nothing new is assumed without a reminder.
Level 1 — Recognition
L1.1
Which of these graphs forces you to use Bellman-Ford instead of Dijkstra's Algorithm? (a) all weights ; (b) one edge of weight , no cycles; (c) all weights .
Recall Solution
(b). Dijkstra's Algorithm is safe whenever every weight is , because its greedy "once finalized, never beaten" promise holds. A single negative weight can make a longer-hop route cheaper later, breaking that promise — so (b) needs Bellman-Ford. (a) and (c) are non-negative → Dijkstra is fine (and faster).
L1.2
For a graph with vertices, how many relaxation passes compute the final distances, and how many total passes does the full algorithm run (including detection)?
Recall Solution
Compute with passes. Then one extra detection pass → total passes. Mnemonic from the parent: "V-minus-one to relax, one more to react."
L1.3
True or false: "If Bellman-Ford finishes without reporting a negative cycle, the graph has no negative cycle anywhere."
Recall Solution
False. It only detects negative cycles ==reachable from the source ==. A negative cycle sitting in a component you can't reach from stays invisible. (Fix: add a virtual source with -weight edges to every vertex.)
Level 2 — Application
L2.1
Vertices . Edges: . Source . Relax edges in the listed order. Give final distances.

Recall Solution
Init: . Pass 1 (order as listed):
- :
- :
- :
- :
- :
End of pass 1: .
Pass 2: every edge — nothing improves. Early-exit
break. Answer: . Notice the good edge order let it converge in one pass.
L2.2
Same graph and edges as L2.1, but relax in the reverse order: . How many passes until convergence, and what are the final distances?
Recall Solution
Init: . Pass 1 (reverse order):
- : → skip (guard).
- : → skip.
- : → skip.
- : .
- : . End pass 1: . Pass 2:
- : .
- : , not → no.
- : .
- : no change. End pass 2: . Pass 3:
- : .
- rest: no change. End pass 3: . Pass 4: nothing improves → break. Answer: same final but it took 3 converging passes. Same result, more work — this is exactly why we bound by instead of trusting a lucky order.
L2.3
Vertices . Edges: . Source . Give final distances.
Recall Solution
Init . Pass 1 (listed order): ; : ; : not → no. → . Pass 2: no change → break. Answer: . The negative edge makes the two-hop route () beat the direct edge ().
Level 3 — Analysis
L3.1
A student runs Bellman-Ford and after pass 1 gets the entire correct answer, yet the algorithm keeps looping. What is the cleanest one-line change so it stops early, and does it change any output?
Recall Solution
Add the early-exit flag: track updated during each pass; if a whole pass makes no relaxation, break. It changes no output — if nothing improved in a pass, nothing will ever improve again (the array is a fixed point of the relax operator), so the remaining -bounded passes are wasted work. This is the Graph Relaxation fixed-point argument.
L3.2
Explain, using the DP subproblem = "shortest distance from to using at most edges", why is guaranteed to be final when there's no negative cycle — and why the bound would be wrong if we allowed non-simple paths.
Recall Solution
With no negative cycle, some shortest path to is simple (repeating a vertex would only add a cycle; a non-negative cycle never helps, a negative one is banned). A simple path visits at most vertices → at most edges. So an optimal path fits inside " edges", meaning already sees it. If we allowed revisiting vertices, a negative cycle could let paths use unboundedly many edges to keep dropping — then no finite suffices, and . This is why detection matters: it certifies that the "simple path" assumption held.
L3.3
Given vertices and edges, state the exact worst-case number of relax operations (edge examinations) performed by the compute phase (ignore early exit), and give the general formula.
Recall Solution
Compute phase = passes edges relax operations. General cost . (The detection pass adds one more , giving total examinations, but the asymptotic cost stays .)
Level 4 — Synthesis
L4.1
Vertices . Edges: . Source . Is there a negative cycle reachable from ? Show the detection.

Recall Solution
The cycle has weight , and it's reachable from via . So yes. Trace (listed order, guarded):
- Init .
- Pass 1: :; :; :; :. → .
- Pass 2: :; :; :. → .
- Pass 3 (): keeps dropping, e.g. falls further.
- Detection pass (4th): some edge (e.g. ) still relaxes → report negative cycle. Distances are meaningless; return the flag, not numbers.
L4.2
You must find whether a graph has any negative cycle (not just those reachable from a given source). The graph is with edges and an isolated vertex with no path to . Using only Bellman-Ford, how do you detect the cycle (weight ) even though it's unreachable from source ?
Recall Solution
Add a virtual source with a -weight edge to every vertex (, all weight ). Now makes every vertex reachable, so the cycle (weight ) becomes reachable and the extra detection pass catches it. Run Bellman-Ford from on vertices. This is the standard "add super-source" trick and underlies Floyd-Warshall-style global checks.
Level 5 — Mastery
L5.1 (recover the cycle, not just detect it)
After detection fires on an edge , describe an algorithm to print the actual negative cycle (the sequence of vertices), and argue why it terminates.
Recall Solution
Keep a parent[] array updated on every successful relax: when relaxes, set parent[v]=u. When the detection pass relaxes some edge :
- Let . Walk
x = parent[x]exactly times. This guarantees lands inside the cycle (any node reached after parent-hops must be on a cycle, since a simple chain has edges). - From this , follow
parentlinks, collecting vertices until you return to again. That collected loop is the negative cycle; reverse it for forward order. Termination: step 1 is a fixed hops. Step 2 follows parent pointers around a finite cycle → returns to in steps. Correctness: the extra relax means lies on a chain whose weight kept dropping, which can only happen along a negative cycle.
L5.2 (edge subdivision invariance)
Take any graph and replace every edge of weight by two edges of weight and of weight through a fresh midpoint . This adds vertices and turns edges into . What is the new worst-case complexity, and does the set of detected negative cycles change?
Recall Solution
New sizes: , . Cost . For dense graphs where this is — much worse. Detected cycles are unchanged: each subdivided edge preserves total weight (), so every cycle's sign is identical; a cycle is negative before iff negative after. Lesson: subdivision is semantically free but computationally costly — never inflate or without reason. (Contrast with SPFA, which optimizes the constant, not the asymptotics.)
L5.3 (parametric threshold)
Edges: , source , with a real parameter . For which values of does the cycle become a negative cycle that Bellman-Ford reports?
Recall Solution
Cycle weight . Negative cycle .
- : weight → detection fires, no finite distances.
- : weight → looping neither helps nor hurts; distances are finite (), no report.
- : weight → normal finite distances, no report. This is exactly the boundary the Shortest Path Problem cares about: the sign of the total cycle weight, computed by summing edges around the loop.
Recall Full-page self-test (do before revealing)
Cover the solutions; re-derive L2.1, L4.1, and L5.3 from scratch. If all three match, you own the relax rule, detection, and the cycle-sign threshold.
Connections
- Parent: Bellman-Ford (Hinglish)
- Graph Relaxation — the fixed-point behind early exit (L3.1)
- Dijkstra's Algorithm — why negative edges rule it out (L1.1)
- Dynamic Programming — the subproblem (L3.2)
- Negative Weight Cycles — L4, L5.3
- Floyd-Warshall — global negative-cycle detection alternative (L4.2)
- SPFA — constant-factor speedup, same (L5.2)
- Shortest Path Problem