Skip to content

Resource · Guide or article

Floyd–Warshall Algorithm

Explains Floyd-Warshall, the all-pairs shortest path algorithm that handles negative edge weights as long as there are no negative cycles. Its distance tables make it a good fit for candidates learning dynamic programming over graphs.

Reviewed

Open the original: Floyd–Warshall Algorithm (opens the original site in a new tab)

Practice this topicOpens the practice interviewer with a question on Graphs.

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

In computer science, the Floyd–Warshall algorithm is an algorithm for finding shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles). A single execution of the algorithm will find the lengths (summed weights) of shortest paths between all pairs of vertices. Although it does not return details of the paths themselves, it is possible to reconstruct the paths with simple modifications to the algorithm. The Floyd–Warshall algorithm compares all possible paths through the graph between each pair of vertices. It is able to do this with O(|V|^3) comparisons in a graph. This is remarkable considering that there may be up to |V|^2 edges in the graph, and every combination of edges is tested. It does so by incrementally improving an estimate on the shortest path between two vertices, until the estimate is optimal. Consider a graph G with vertices V numbered 1 through N. Further consider a function shortestPath(i, j, k) that returns the shortest possible path from i to j using vertices only from the set {1, 2, ..., k} as intermediate points along the way. Now, given this function, our goal is to find the shortest path from each i to each j…

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Article

What it covers

  • The shortestPath(i, j, k) subproblem: best path from i to j using only vertices 1 to k as intermediates
  • States the cost as O(|V|^3) comparisons
  • A four-vertex example with a distance matrix for each k from 0 to 4, including negative weights
  • Notes that the algorithm returns path lengths only and needs a small change to reconstruct the paths

How to use it

PrepKit study advice, not a claim made by the source.

Rebuild the k = 0 matrix from the example and compute each later table yourself before comparing with the README. Then code the triple loop and add a next-hop matrix so you can print the actual path between any two vertices.

Scope and limitations

The recurrence itself appears only as images rendered from Wikipedia, so it may be hard to read or copy. Negative-cycle detection and path reconstruction are mentioned but not shown. The k = 1 table has one cell printed as a bare dash.

Access and format

README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository; English, free to read.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/graph/floyd-warshall/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email