Solve all pairs using permitted intermediates
Graphs are weighted and directed. The calculator fixes A,B,C and all six directed edges between distinct vertices. Weights are integers −100…100; no numeric weight means an absent edge. Negative cycles are checked and rejected with an explanation.[1]
Understand the calculation
- Initialize d(i,i)=0, direct edges with their weights and absent edges with infinity. The diagonal represents the empty path.
- Permit intermediate vertices in a fixed order. At stage k, only the first k vertices may occur inside a path.
- A shortest path either avoids new intermediate k or splits there into i→k and k→j paths. Compare both possibilities.
- Update every ordered pair at every stage. After all stages, negative diagonal entries reveal a negative cycle, so not all shortest-path values are finite.
Intermediate-vertex recurrence with distinct stages[1]
Visualize the learning idea
One permitted intermediate vertex shortens the path
Free with a Quanta account. Your download stays ready after registration.
Two fully checked examples
All pairs in a complete three-vertex graph
- Given
- Directed: AB=3,AC=10,BA=8,BC=2,CA=4,CB=6. No other edges.
- Find
- Find the entire shortest-distance matrix in order A,B,C.
Start with zero diagonal and direct weights.
Via B, improve A→C; via C, improve B→A.
Result: D=[[0,3,5],[6,0,2],[4,6,0]]. The diagonal remains zero.
Check: A→B→C costs 5 and B→C→A costs 6. Other direct values remain minimal; every added cycle is nonnegative.
A negative edge without a negative cycle
- Given
- Directed AB=2,AC=9,BA=5,BC=−1,CA=4,CB=3.
- Find
- Calculate finite distances despite a negative edge.
Via B, A→C drops from 9 to 1.
Via C, B→A drops from 5 to 3.
Result: D=[[0,2,1],[3,0,−1],[4,3,0]]. Negative B→C is a valid cost, not a negative geometric length.
Check: Two-edge cycles cost 7,13,2; the two simple three-edge cycles cost 5 and 17. None is negative, so repetition cannot drive cost arbitrarily low.
Recall the method later with the formula’s flashcards.
10 flashcards · English materials
Already have an account? Sign in for flashcardsAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Interpret the quantities
Path sums share the unit of edge weights. Our calculator uses dimensionless costs. Standard self-distance is zero for the empty path; a negative closed walk violates a finite-minimum interpretation.
Five solved exercises
Calculate · Foundation
AB=3,AC=10,BA=8,BC=2,CA=4,CB=6: find d(A,C).
Show hint
Compare the direct edge with the path via B.
Learning goal: Use an actual intermediate vertex.
Show solution and calculation
min(10,3+2)=5 via A→B→C.
Rearrange · Application
AB=3,AC=10,BA=8,CA=4,CB=6 and BC=x≥0. For which x does A→B→C beat the direct edge?
Show hint
Compare 3+x with 10.
Learning goal: Express path improvement as an inequality.
Show solution and calculation
3+x<10 means 0≤x<7.
At x=7 both paths tie.
Interpret · Foundation
For AB=2,AC=9,BA=5,BC=−1,CA=4,CB=3, is BC itself a negative cycle?
Show hint
A cycle returns to its starting vertex.
Learning goal: Distinguish negative edge and cycle.
Show solution and calculation
No.
The edge ends at another vertex.
B→C→B costs −1+3=2; the other simple cycles are also nonnegative.
Error analysis · Application
A general Floyd–Warshall initialization sets absent edges to zero. What is wrong?
Show hint
Zero represents a real free connection.
Learning goal: Preserve graph structure during initialization.
Show solution and calculation
That invents nonexistent connections.
Absent edges start at infinity; only an empty self-path starts at zero.
All six edges in the fixed complete calculator graph are explicitly present.
Transfer · Challenge
AB=1,AC=5,BA=−2,BC=3,CA=5,CB=3: why is an ordinary finite all-pairs result invalid?
Show hint
Check A→B→A.
Learning goal: Understand negative cycles as an existence boundary.
Show solution and calculation
The cycle costs 1−2=−1.
Repetition drives cost arbitrarily low.
A negative diagonal flags this case; the calculator rejects it with an explanation.
Check your understanding with the formula’s reference test.
8 test questions · English materials
Already have an account? Sign in for the testAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Limits and connections
Dijkstra and Bellman–Ford start from one source. A minimum spanning tree asks a different question: connect all vertices at minimum total tree cost rather than find a cheapest route for every pair.
Sources for this explanation
Quanta · Updated November 9, 2026
Editorial responsibility: Quanta Study editorial team
Review scope
This explanation, 2 worked examples and 5 exercises are available in German and English. Structure, source references and mathematical notation undergo automated checks. Calculations, model assumptions and both language versions receive a separate AI review.
An independent, named human subject review has not yet been documented. Our exercise numbers are worked examples, rather than measurements published by the cited sources.
Editorial responsibility, sources and methodsChange history
- Expanded all-pairs computation with a complete directed teaching graph, a negative edge and negative-cycle limits.
Cite this reference page
Quanta. Floyd–Warshall algorithm. Updated November 9, 2026. https://quanta-study.de/en/formula-sheet/floyd-warshall-kuerzeste-wege
When citing a specific scientific statement, also cite the specialist source linked to that statement.