Skip to main content
Back to the DSA atlas
Graphs Algorithm Advanced

Floyd-Warshall

Compute all-pairs shortest paths by gradually allowing more intermediate vertices. This is the clean dynamic-programming answer to dense shortest-path queries.

graphsshortest-pathdynamic-programming

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

initial matrix

from \\ to0123
00310inf
1inf017

Dynamic programming over intermediates

Initial matrix: direct edges only.

Family

Graphs

Nodes, edges, traversal, connectivity, and shortest paths.

Builds on

2 topics

Read these first if you want the surrounding context.

Unlocks

0 next topics

Use these follow-ups when you want to apply the current idea in a richer setting.

Learning paths

2

This topic appears in curated progressions so you can keep moving without guessing the next step.

Problem

You need shortest paths between every pair of vertices, not just from one source.

Running Dijkstra from every vertex can work, but for dense graphs or when you want a conceptually direct all-pairs method, Floyd-Warshall is the classic choice.

Core idea

Let dist[i][j] be the best known path from i to j.

Then process vertices one by one as allowed intermediates:

dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j])dist[i][j] = \min(dist[i][j], dist[i][k] + dist[k][j])

At stage k, you ask:

is the best path from i to j better if I am allowed to route through vertex k?

That is a pure dynamic-programming recurrence.

Why it matters

Floyd-Warshall is the cleanest demonstration that shortest paths are not only about greedy algorithms like Dijkstra. Sometimes the right model is a DP over allowed intermediates.

It also handles negative edge weights, as long as there is no negative cycle.

Complexity

OperationTimeSpace
All-pairs shortest pathsO(n3)O(n^3)O(n2)O(n^2)

This is expensive for large sparse graphs, but excellent for dense graphs or small n.

Key takeaways

  • Floyd-Warshall is the standard all-pairs shortest-path DP
  • It is cubic, so it shines when n is modest or the graph is dense
  • Unlike Dijkstra, it can handle negative edges if there is no negative cycle
  • This fills the gap between single-source shortest paths and full reachability analysis

Practice problems

ProblemDifficultyKey idea
Find the City With the Smallest Number of Neighbors at a Threshold Distance🟡 MediumAll-pairs distances on modest n
Shortest Routes II🟡 MediumMultiple pair queries after preprocessing
Arbitrage / transitive closure variants🔴 HardDP over intermediates

Relation to other topics

  • Dijkstra is better for single-source nonnegative shortest paths
  • Dynamic Programming explains the recurrence structure directly
  • Graph Fundamentals provides the weighted-path model Floyd-Warshall operates on

Build on these first

These topics supply the mental model or underlying structure that this page assumes.

Related directions

These topics live nearby conceptually, even if they are not direct prerequisites.

More from Graphs

Stay in the same family when you want parallel variations of the same mental model.

Paths that include this topic

Follow one of these sequences if you want a guided next step instead of open-ended browsing.

From the blog

Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.