1. Gyanastra
  2. Algorithms
  3. Floyd-Warshall Algorithm
  4. All-Pairs Shortest Path: Problem Definition vs Single-Source
Free lesson, no account needed 4 min read 11 practice questions after it
Asked in past papers9 times
  • GATE CSE20031998
  • ISRO2023201820172008
  • UGC NET202020182015

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.
Graph diagram: All-Pairs Shortest Path: Problem Definition vs Single-Source
Figure. All-Pairs Shortest Path: Problem Definition vs Single-Source

How it works

  1. Pick the first node as source and find shortest paths from it to all other nodes.
  2. Pick the next node as source and find shortest paths from it to all other nodes.
  3. Repeat for every node, so that each node acts once as source and every other node acts as destination.

Comparison

AspectSSSPAPSP
SourceOne fixed source nodeEvery node serves as source in turn
DestinationsAll other nodesEvery other node for each source
Pairs covered(S, X) — source always the fixed node S(X, Y) — any two nodes X and Y
Example queriesS→A, S→B, S→CA→B, A→C, S→A, S→B, S→C, ...
Answers A→B, A→C?NoYes

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 Θ(V3)\Theta(V^{3}) time (O(V3)O(V^{3})) using an O(V2)O(V^{2}) 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 O(V3)O(V^{3}) with an array implementation or O(V⋅Elog⁡V)O(V\cdot E \log V) with a heap, but it fails for negative edge weights; Bellman-Ford from every vertex costs O(V2⋅E)O(V^{2} \cdot E) (O(V4)O(V^{4}) 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 O(n3)O(n^{3}) 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 DkD^{k}[i][j] = min(Dk−1D^{k-1}[i][j], Dk−1D^{k-1}[i][k] + Dk−1D^{k-1}[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.
Pages 1–2 of 2