Which of the following is the best-case time complexity of Floyd’s algorithm for finding shortest paths in a graph with ‘n’ vertices?
- AOption (a)
Θ(n)
- BOption (b)
Θ(n3)
- COption (c)
Θ(n2)
- DOption (d)
Θ(1)
View answer & explanationHide answer & explanation
Correct answer
Θ(n3)
Why this answer is correct
Floyd’s warshall algorithm is used find the shortest path between every pair of nodes. It is used for all pair shortest path problem.
Algorithm:
N= w.rows
D0 = w
For k = 1 to n
Let Dk = dijk be new n × n matrix
For i = 1 to n
For j = 1 to n
Dijk = min (dijk-1, dikk-1 + dkjk-1)
Returns Dn.
So, in Floyd warshall to calculate the shortest path three for loops are used which goes upto n, so time complexity will be Θ(n3).