Files never leave your device

Floyd–Warshall calculator

Compute a full distance matrix and detect negative cycles. Your inputs stay on your device.

Set up your task

Ready. Processing stays in this browser.

How to use Floyd–Warshall calculator

Compute a full distance matrix and detect negative cycles. The pre-filled problem gives you a worked starting point. Compare the response with your own working before trying the self-check below.

  1. Check the example inputs: Vertices (space-separated), Edges (from, to, weight, one per line), Edges are.
  2. Change the inputs for your problem. Results update as you type, so finish entering the values before copying an answer.
  3. Use the working to check an intermediate value or assumption. Then change the example as suggested in the self-check and compare the result.

Supported inputs and limits

Enter up to 20 vertices and 80 weighted edges. Directed edges may be negative if there are no negative cycles.

How it works

Floyd–Warshall computes shortest distances for every ordered pair of vertices. It starts with direct edge costs and zero on the diagonal. It then tries one additional vertex at a time as an allowed intermediate point, comparing the current route with the route through that point.

Each pass extends the permitted set of intermediate vertices. The algorithm therefore succeeds even for directed edges of negative weight when there are no negative cycles. A negative diagonal entry after the passes signals a negative-weight closed walk, whose repeated traversal can keep reducing costs.

Example

Run the pre-filled example. Its first result is “All-pairs shortest distances”.

Vertices (space-separated)
A B C
Edges (from, to, weight, one per line)
A B 3
B C -2
A C 5
Edges are
Directed
All-pairs shortest distances
From / to  A  B  C
A  0  3  1
B  ∞  0  -2
C  ∞  ∞  0

Questions about Floyd–Warshall calculator

Why start the diagonal at zero?

Staying at the same vertex with no moves costs zero. A negative result on the diagonal exposes a closed walk that reduces that cost.

What does infinity in the result mean?

The destination cannot be reached from that particular start using the supplied direction rules.

Inspect the distance from A to C in the example.

It is 1, from A to B at cost 3 and then B to C at cost −2. The direct route costs 5.

Related tools

Year 2: Algorithms and theory

Last updated