The Floyd-Warshall algorithm for all-pair shortest paths computation is based on
2016
The Floyd-Warshall algorithm for all-pair shortest paths computation is based on
- A.
Greedy paradigm.
- B.
Divide-and-Conquer paradigm.
- C.
Dynamic Programming paradigm.
- D.
neither Greedy nor Divide-and-Conquer nor Dynamic Programming paradigm.
Attempted by 299 students.
Show answer & explanation
Correct answer: C
Answer: Dynamic Programming paradigm.
Why:
The algorithm defines d_k(i,j) as the shortest-path distance from vertex i to vertex j using only intermediate vertices from the set {1, …, k}.
It uses the recurrence d_k(i,j) = min(d_{k-1}(i,j), d_{k-1}(i,k) + d_{k-1}(k,j)), which clearly builds each solution from earlier computed subsolutions.
Because it solves overlapping subproblems and combines them according to a recurrence, this is dynamic programming.
Implementation (conceptual pseudocode):
Initialize dist[i][j] = weight(i,j) if edge exists, 0 if i = j, otherwise +infinity.
For k from 1 to n:
For i from 1 to n:
For j from 1 to n:
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
Time complexity: O(n^3) due to the three nested loops.
Works with negative edge weights but will not produce meaningful shortest paths if a negative-weight cycle exists; a negative cycle is detectable if dist[i][i] becomes negative for some i.
A video solution is available for this question — log in and enroll to watch it.