Intuition The one core idea
A graph is a map of places joined by roads that have costs, and Bellman-Ford finds the cheapest way from one starting place to every other place — even when some roads pay you (negative cost). It does this by patiently checking every road over and over, letting cheap news spread one road-hop further with each round — and if the cost of some city never stops dropping , that reveals a negative-cost loop .
Before you can read the parent note on Bellman-Ford, you must own every word and symbol it throws at you. This page builds each one from nothing, in an order where each idea leans only on the ones before it.
Definition Graph, vertex, edge
A graph is a set of dots called vertices (also "nodes"), joined by lines called edges . Each edge connects two vertices. That's the whole object — dots and connections.
Think of vertices as cities and edges as roads between them.
A single vertex is drawn as a dot. In code we usually name them by number: 0 , 1 , 2 , …
The letter V (capital vee) means the number of vertices — how many dots. In the picture V = 4 .
The letter E (capital ee) means the number of edges — how many roads. In the picture E = 4 .
Why the topic needs this: Bellman-Ford's whole running time is written O ( V ⋅ E ) — you cannot understand that cost until V and E are real things you can count.
A directed edge is a one-way road. We write it u → v : you may travel from u to v , but not back along the same edge. The arrow shows the allowed direction.
Here u and v are just placeholder names for "some starting vertex" and "some ending vertex" of one edge.
Every edge carries a number called its weight , written w ( u , v ) — read "the weight of the edge from u to v ". It is the cost to travel that road.
w ( 0 , 1 ) = 4 means "the road from city 0 to city 1 costs 4".
A negative weight like w ( 1 , 2 ) = − 3 means that road pays you 3 — travelling it lowers your total cost. Look at the coral edge in the figure.
Intuition Why negatives matter at all
If every road cost money (all weights ≥ 0 ), taking more roads could never help. Negative roads flip that: a longer route might end up cheaper . This single fact is why we need Bellman-Ford instead of the faster Dijkstra's Algorithm .
Definition How the edges are stored: a list of triples
When we say ==edges== we mean a list, one item per edge, where each item is a triple (u, v, w) — the start vertex, the end vertex, and the weight. For the picture above:
edges = [ ( 0 , 1 , 4 ) , ( 0 , 2 , 5 ) , ( 1 , 2 , − 3 ) , ( 2 , 3 , 2 ) ]
So a loop like "for each (u, v, w) in edges" simply walks through every road one at a time , handing you its start, end, and cost. That is the only data Bellman-Ford ever reads.
A path is a sequence of vertices where each consecutive pair is joined by a directed edge, following the arrows. Example: 0 → 1 → 2 is a path if both those edges exist.
A path is simple if it never repeats a vertex . Since there are only V vertices, a simple path visits at most V of them, so it uses at most V − 1 edges.
The figure shows one simple path (mint) using 3 vertices / 2 edges, and one non-simple path (coral, dashed) that loops back and repeats a vertex.
Intuition Why "at most V−1 edges" is the heart of the algorithm
A chain touching every one of V cities has V − 1 gaps between them — like V beads on a string need V − 1 knots. So if no weird loops help you, the best route can never use more than V − 1 roads. That number V − 1 is exactly how many rounds Bellman-Ford runs.
Definition Source and distance
The source , written s (or src in code), is the one city we start from. The distance to a city v , written dist ( v ) , is the cheapest total weight of any path from s to v . We store these in an array called ==dist==, where dist[v] holds the best cost to v found so far.
If no path reaches v at all, its distance is ∞ (infinity) — "unreachable, infinitely expensive".
Why the topic needs this: Bellman-Ford is a single-source algorithm — everything is measured as a distance from one fixed s . See Shortest Path Problem .
Definition Infinity as "not yet reached"
∞ is not a real number — it is a placeholder meaning "no path found yet, so treat as impossibly large" . In code this is written float('inf'), which is just that language's spelling of ∞ — a value larger than any real distance.
Intuition Picture it as fog
At the start, every city except the source is hidden in fog: distance = ∞ . As we discover routes, the fog lifts and real (finite) numbers replace the ∞ s.
Common mistake Adding to infinity
If dist[u] is still ∞ , then ∞ + w is still ∞ — you learned nothing. That is exactly why we guard with "only relax when dist[u] is finite (not ∞ )" before relaxing: don't spread news from a city you can't even reach yet.
Everything Bellman-Ford does is built from a single tiny operation.
To relax the edge u → v with weight w means: ask whether going through u gives a cheaper way to reach v , and if so, update.
if dist [ u ] + w < dist [ v ] then dist [ v ] ← dist [ u ] + w
Read it in plain words: "If the cost to reach u , plus the cost of the road from u to v , beats what I currently think v costs — then I've found a better route, so lower v 's distance."
The figure shows one relaxation: before, v thought it cost 9; via u (cost 4) plus the road (cost 3) it now costs 7, so we lower it. The symbol ← means "gets the new value" (assignment).
Intuition Why relaxing repeatedly is enough
One relaxation lets good news travel exactly one road further . Do a full sweep over all E edges and the cheapest info spreads at least one hop. Do V − 1 sweeps and it reaches the end of the longest possible simple path — so every true distance is found. This is the bridge into Graph Relaxation and the Dynamic Programming view.
O ( ⋅ ) describes how the work grows as the input grows, ignoring constant factors. O ( V ⋅ E ) means "roughly V times E basic steps."
Why this exact number? We do V − 1 sweeps (about V ), and each sweep relaxes all E edges. So total work ≈ ( V − 1 ) ⋅ E , which we call O ( V ⋅ E ) .
The parent note writes D [ k ] [ v ] . You now have every piece to read it:
The base row (where the table starts): with 0 edges allowed you can only stand at the source, so
D [ 0 ] [ s ] = 0 , D [ 0 ] [ v ] = ∞ ( v = s ) .
This is exactly the initial dist array from Section 4.
Because a simple path uses at most V − 1 edges (Section 3), the answer lives at k = V − 1 : that's why the parent stops there.
Intuition What if a loop of roads pays you every lap?
Suppose roads 0 → 1 → 2 → 0 have weights that add up to a negative total. Then each time you drive the loop your cost drops — so you could loop forever and reach − ∞ . There is no cheapest path any more. Such a loop is a negative-weight cycle , and detecting it is a headline feature of Bellman-Ford.
Definition How Bellman-Ford spots it (preview)
After the V − 1 sweeps, honest distances have stopped changing. So Bellman-Ford runs one extra sweep : if any edge can still be relaxed (some dist[v] still drops), the only possible cause is a negative-cost loop reachable from the source. You will meet the full argument in the parent note and in Negative Weight Cycles — for now, just know why it exists: negative loops make "shortest distance" undefined, so we must flag them.
Simple path uses V-1 edges
DP table D k v and recurrence
What is a vertex, and what does V count? A dot / place in the graph; V is the number of vertices.
What is a directed edge u → v ? A one-way road you can travel from u to v only.
What does w ( u , v ) mean? The weight (cost) of the edge from u to v .
What is edges as a data structure? A list of triples ( u , v , w ) , one per edge: start, end, weight.
What does a negative weight represent? A road that lowers your total cost — it pays you.
What is a simple path, and how many edges can it use? A path with no repeated vertex; at most V − 1 edges.
What is the source s ? The single starting vertex all distances are measured from.
How is the dist array initialised? dist [ s ] = 0 and dist [ v ] = ∞ for every v = s .
Why do we initialise unreached cities to ∞ ? They have no known path yet; ∞ marks "impossibly expensive so far".
State the relaxation rule in words. If dist [ u ] + w < dist [ v ] , set dist [ v ] = dist [ u ] + w .
Why only relax when dist[u] is finite? You can't spread cost from a city you haven't reached; ∞ + w is still ∞ .
What does O ( V ⋅ E ) mean here? Work grows like V sweeps times E edges per sweep.
What does D [ k ] [ v ] store, and its recurrence? Cheapest cost from s to v using at most k edges; D [ k ] [ v ] = min ( D [ k − 1 ] [ v ] , min ( u , v ) D [ k − 1 ] [ u ] + w ( u , v )) .
What is a negative-weight cycle, and how is it flagged? A loop whose weights sum below zero; flagged if one extra sweep after V − 1 still relaxes an edge.
Parent topic (Hinglish)
Shortest Path Problem — the problem all of this solves
Graph Relaxation — the single move built here
Dynamic Programming — the D [ k ] [ v ] table view
Dijkstra's Algorithm — the faster cousin that needs non-negative weights
Negative Weight Cycles — what negative weights can create
Floyd-Warshall — the all-pairs relative
SPFA — a queue-based speedup