How many invocations of a single-source shortest path subroutine are needed to solve the all-pairs shortest path problem? (As usual, n denotes the number of vertices.)
a) 1
b) n − 1
c) n
d) n²
(See Section 18.3.3 for the solution and discussion.)
Correct answer: (c). One invocation of the single-source shortest path subroutine will compute shortest-path distances from a single vertex s to every vertex of the graph (n numbers in all, out of the n² required). Invoking the subroutine once for each of the n choices for s computes shortest-path distances for every possible origin and destination.^{20}
^{20}If the input graph has a negative cycle, it will be detected by one of the invocations of the single-source shortest path subroutine.