3.5.12 · D1Graphs

Foundations — Floyd-Warshall — all-pairs shortest paths, O(V³)

1,955 words9 min readBack to topic

This page assumes you have seen nothing. Before you can read the parent note Floyd-Warshall, you must own every symbol it throws at you. We will build each one from a picture. Nothing is used before it is defined.


1. What is a graph? (, , )

Forget equations. Draw dots and arrows.

Figure — Floyd-Warshall — all-pairs shortest paths, O(V³)

Each symbol is just shorthand for a part of the picture:

Symbol Plain words In the picture
the whole map dots + arrows together
all the dots the circles labelled
all the connectors the arrows

2. Directed vs undirected, and weights ()

Two extra facts live on each road.

Figure — Floyd-Warshall — all-pairs shortest paths, O(V³)

So means: the one-way road from city to city costs .


3. A path, its length, and "shortest"

Figure — Floyd-Warshall — all-pairs shortest paths, O(V³)

In the figure the direct road costs , but the detour costs . So , not . A detour can beat the direct road — that single fact is the engine of the whole algorithm.


4. The distance matrix (, and the symbol)

We store all those answers in a grid called a matrix.

Why on the diagonal? The distance from a city to itself is zero — you are already there.


5. The two tools: min and adding two half-paths

Everything Floyd-Warshall computes is built from just these two moves.

Figure — Floyd-Warshall — all-pairs shortest paths, O(V³)

Put the two tools together and you get the heart of the parent note: In words: "Is stopping at cheaper than my current best? If so, adopt it."


6. The intermediate vertex and the notation

The trickiest symbol in the parent note is the subscript in .


7. Negative edges and negative cycles


Prerequisite map

Graph G = dots and arrows

Directed edges

Weights w on edges

Path and its length

Shortest distance d i j

Distance matrix with infinity

min picks cheaper

Relax through middle city k

Split path at k

Allowed set 1 to k grows

Floyd-Warshall recurrence

Negative edges and cycles

Everything on the left feeds the single relaxation step , which repeated over growing allowed sets is the algorithm .


Equipment checklist

Read each line, answer in your head, then reveal:

What do and stand for in ?
= the set of vertices (dots / cities); = the set of edges (arrows / roads).
What does a directed edge allow, and not allow?
Travel from to only; it does not grant travel from to .
What is ?
The cost (weight) written on the road from to .
What is the length of the path ?
— the sum of the weights of the roads used.
What does mean?
The length of the shortest path from to .
Why do we initialise unknown distances to ?
So any real path is smaller and will replace it; means "no known road yet."
Why is set to at the start?
The distance from a city to itself is zero — you are already there.
What does do and why is it the right tool?
Returns the smaller value; "best route" means cheapest, so we keep the smaller cost.
When you split a path through , what is its cost?
— cost to reach plus cost onward from .
Read aloud in plain words.
Shortest distance from to using only cities through as intermediate stops.
What is the difference between a negative edge and a negative cycle?
A negative edge is a single cost-reducing road (allowed); a negative cycle is a loop whose weights sum below zero (forbidden — makes shortest path undefined).
How would you spot a negative cycle after running the algorithm?
Some diagonal entry becomes less than .

Now you own every symbol. Return to the parent note and the recurrence will read like plain English.