3.5.11 · D1Graphs

Foundations — Bellman-Ford algorithm — DP approach, negative cycles detection, O(VE)

2,305 words10 min readBack to topic

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.


1. What is a graph? (the picture behind everything)

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:
  • The letter (capital vee) means the number of vertices — how many dots. In the picture .
  • The letter (capital ee) means the number of edges — how many roads. In the picture .

Why the topic needs this: Bellman-Ford's whole running time is written — you cannot understand that cost until and are real things you can count.


2. Directed edges and weights

Here and are just placeholder names for "some starting vertex" and "some ending vertex" of one edge.

  • means "the road from city 0 to city 1 costs 4".
  • A negative weight like means that road pays you 3 — travelling it lowers your total cost. Look at the coral edge in the figure.

3. Path, source, and distance

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.

If no path reaches 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 . See Shortest Path Problem.


4. The symbol and the starting dist array


5. Relaxation — the one move the algorithm makes

Everything Bellman-Ford does is built from a single tiny operation.

Read it in plain words: "If the cost to reach , plus the cost of the road from to , beats what I currently think costs — then I've found a better route, so lower 's distance."

The figure shows one relaxation: before, thought it cost 9; via (cost 4) plus the road (cost 3) it now costs 7, so we lower it. The symbol means "gets the new value" (assignment).


6. Reading

Why this exact number? We do sweeps (about ), and each sweep relaxes all edges. So total work , which we call .


7. The symbols in the DP table (peek ahead)

The parent note writes . You now have every piece to read it:

The base row (where the table starts): with edges allowed you can only stand at the source, so This is exactly the initial dist array from Section 4.

Because a simple path uses at most edges (Section 3), the answer lives at : that's why the parent stops there.


8. One edge case to foreshadow: negative-cost loops


Prerequisite map

Graph = dots and lines

Vertices and count V

Edges list of triples E

Directed edge u to v

Weight w u v

Negative weight

Path

Simple path uses V-1 edges

Source s and distance

Init dist s = 0 rest inf

Relaxation move

DP table D k v and recurrence

Negative cost cycle

Bellman-Ford


Equipment checklist

What is a vertex, and what does count?
A dot / place in the graph; is the number of vertices.
What is a directed edge ?
A one-way road you can travel from to only.
What does mean?
The weight (cost) of the edge from to .
What is edges as a data structure?
A list of triples , 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 edges.
What is the source ?
The single starting vertex all distances are measured from.
How is the dist array initialised?
and for every .
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 , set .
Why only relax when dist[u] is finite?
You can't spread cost from a city you haven't reached; is still .
What does mean here?
Work grows like sweeps times edges per sweep.
What does store, and its recurrence?
Cheapest cost from to using at most edges; .
What is a negative-weight cycle, and how is it flagged?
A loop whose weights sum below zero; flagged if one extra sweep after still relaxes an edge.

Connections

  • Parent topic (Hinglish)
  • Shortest Path Problem — the problem all of this solves
  • Graph Relaxation — the single move built here
  • Dynamic Programming — the 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