Algorithms · Floyd-Warshall Algorithm
All-Pairs Shortest Path: Problem Definition vs Single-Source
Overview
Single-source shortest path (SSSP) finds shortest paths from one fixed source to all other nodes, while all-pairs shortest path (APSP) finds the shortest distance between every pair of nodes.
APSP is the more general problem and can be solved by running an SSSP algorithm from every vertex, but that approach is computationally slow — which motivates a dedicated all-pairs algorithm such as Floyd-Warshall.
Key concepts
- Single-source shortest path (SSSP): One fixed source node; find the shortest path from that source to all other nodes. Every pair considered always contains the source S (e.g., S→A, S→B, S→C).
- All-pairs shortest path (APSP): Every node is treated as a source and every other node as a destination; find the shortest distance between every pair of nodes (e.g., A→B, A→C, S→A, S→B, S→C, ...).
- Pairs covered by SSSP: Only pairs of the form (S, X) — the source is always one fixed member of the pair.
- Pairs covered by APSP: Pairs of the form (X, Y) for any two nodes X and Y — the source can be any node, not just S.
- Relationship between the problems: APSP can be obtained by running a single-source shortest path algorithm once from every vertex, but this approach is computationally slow.

How it works
- Pick the first node as source and find shortest paths from it to all other nodes.
- Pick the next node as source and find shortest paths from it to all other nodes.
- Repeat for every node, so that each node acts once as source and every other node acts as destination.
Comparison
| Aspect | SSSP | APSP |
|---|---|---|
| Source | One fixed source node | Every node serves as source in turn |
| Destinations | All other nodes | Every other node for each source |
| Pairs covered | (S, X) — source always the fixed node S | (X, Y) — any two nodes X and Y |
| Example queries | S→A, S→B, S→C | A→B, A→C, S→A, S→B, S→C, ... |
| Answers A→B, A→C? | No | Yes |
Revision summary
- SSSP = one source, all destinations; every shortest path computed shares the same source.
- APSP = all pairs; every node serves as a source in turn, and every other node as a destination.
- APSP answers queries like A→B and A→C, which SSSP from S does not answer.
- Running an SSSP algorithm from every vertex solves APSP but is computationally slow.
- That slowness motivates a dedicated all-pairs algorithm (Floyd-Warshall).
Previous-year exam insights
- Floyd-Warshall is a Dynamic Programming algorithm; it computes all-pairs shortest paths in time () using an distance matrix, and works with negative edge weights provided there is no negative-weight cycle.
Asked inGATE 1998ISRO 2008UGC NET 2020ISRO 2017UGC NET 2015 - Running Dijkstra from every vertex solves APSP in with an array implementation or with a heap, but it fails for negative edge weights; Bellman-Ford from every vertex costs ( for dense graphs) and handles negative edges.
Asked inISRO 2023UGC NET 2020 - The transitive closure of a binary relation on n elements (reachability between all pairs) is computed by the Floyd-Warshall-style Warshall algorithm in time.
Asked inISRO 2018 - Algorithm-to-problem matching: Floyd-Warshall = all-pairs shortest path;
Dijkstra = single-source shortest path (non-negative weights);
Bellman-Ford = single-source shortest path with negative edges;
Kruskal/Prim = minimum spanning tree.
Asked inISRO 2023ISRO 2017UGC NET 2015 - Floyd-Warshall's recurrence is [i][j] = min([i][j], [i][k] + [k][j]), where k is the intermediate vertex allowed;
the triple loop must have k (the intermediate vertex) as the outermost loop. - A negative-weight cycle exists iff after Floyd-Warshall some diagonal entry D[i][i] < 0; Floyd-Warshall cannot give shortest paths in that case.