Questions: Floyd-Warshall Algorithm for All-Pairs Shortest Paths

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

In Floyd-Warshall's triple-nested loop, why must k (the intermediate vertex index) be the outermost loop?

ABecause the algorithm processes vertices in decreasing order of their weights, which requires k to be fixed first
BBecause updating dist[i][j] via intermediate vertex k requires that dist[i][k] and dist[k][j] already reflect the best paths using intermediates 1 through k-1 — the k dimension must be fully processed first
CBecause j must complete all updates before k can be incremented, and k controls the j-loop range
DTo avoid overwriting distance values needed for other pairs in the same pass of the i and j loops
Question 2 Multiple Choice

After running Floyd-Warshall, you check the result matrix and find that dist[5][5] = -8. What does this indicate?

AVertex 5 has a self-loop with weight -8 that was part of the input graph
BThe matrix was initialized incorrectly — dist[i][i] should always remain 0
CVertex 5 lies on a negative-weight cycle; the path from 5 back to itself can be made arbitrarily short
DFloyd-Warshall detected that 5 is unreachable from itself and assigned a sentinel value
Question 3 True / False

Floyd-Warshall correctly computes shortest paths in graphs containing negative-weight edges, as long as no negative-weight cycles exist.

TTrue
FFalse
Question 4 True / False

For sparse graphs (few edges), Floyd-Warshall is generally faster than running Dijkstra's algorithm once from nearly every vertex.

TTrue
FFalse
Question 5 Short Answer

Explain the dynamic programming recurrence in Floyd-Warshall: what is dist_k[i][j], and why does progressively expanding the set of allowed intermediate vertices correctly compute all-pairs shortest paths?

Think about your answer, then reveal below.