Time and Space Complexity of Floyd Warshall Algorithm (original) (raw)

Last Updated : 23 Jul, 2025

The **Floyd Warshall Algorithm has a **time complexity of **O(V 3 ) and a **space complexity of **O(V 2 ), 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(V 3 )

**Average Case: O(V 3 )

**Worst Case: O(V 3 )

**Auxiliary Space Complexity of Floyd Warshall Algorithm:

The auxiliary space complexity of the **Floyd-Warshall algorithm is O(V 2 ), 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.

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