Open In App

Time and Space Complexity of Floyd Warshall Algorithm

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Floyd Warshall Algorithm has a time complexity of O(V3) and a space complexity of O(V2), where V represents the number of vertices in the graph. This algorithm computes the shortest paths between all pairs of vertices in a weighted graph. The time complexity arises from the triple nested loops used to update the shortest path matrix, while the space complexity is determined by the need to store the distances between all pairs of vertices in a 2D array.

Aspect Complexity
Time Complexity O(V3)
Space Complexity O(V2)

Let’s explore the detailed time and space complexity of the Floyd Warshall Algorithm:

Time Complexity of Floyd Warshall Algorithm:

Best Case: O(V3)

  • In the best-case scenario, the algorithm completes all iterations of the nested loops without any relaxation steps needed.
  • This implies that the shortest paths between all pairs of vertices are already determined and no updates are necessary.
  • Because of this, the three nested loops each of which iterates through every vertex—are the only factors influencing the algorithm’s time complexity.
  • With V vertices, each loop iterates V times, resulting in a time complexity of O(V3) for the best case.

Average Case: O(V3)

  • The average-case time complexity of the Floyd-Warshall algorithm is also O(V3).
  • This complexity holds true across various graph structures and densities, as the algorithm’s performance primarily depends on the number of vertices and the number of iterations needed to compute shortest paths between all pairs of vertices.
  • The algorithm’s inherent nature of iterating through all pairs of vertices and updating distances contributes to the cubic time complexity, regardless of the specific graph characteristics.

Worst Case: O(V3)

  • Conversely, in the worst-case scenario, the algorithm performs relaxation steps for each pair of vertices during all iterations of the nested loops.
  • This means that the algorithm needs to update distances between vertices multiple times until the shortest paths are determined.
  • Again, the time complexity is determined by the three nested loops, each iterating through all vertices.
  • With V vertices, each loop iterates V times, resulting in a time complexity of O(V3) for the worst case as well.

Auxiliary Space Complexity of Floyd Warshall Algorithm:

The auxiliary space complexity of the Floyd-Warshall algorithm is O(V2), where V is the number of vertices in the graph. To create a 2-D matrix in order to store the shortest distance for each pair of nodes.

Distance Matrix: Utilizes a 2D array to store shortest distances between all pairs of vertices.

  • Matrix size: V x V, where V is the number of vertices.
  • Each entry represents the shortest distance between two vertices.
  • Space complexity: O(V2).

Additional Variables: May require supplementary variables for iteration and calculation.

  • Space occupied by these variables is typically minimal compared to the distance matrix.
  • Doesn’t significantly affect overall space complexity.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads