Related Topics

Data Structure
for each i from 1 to n:
for each j from 1 to n:
if i == j:
dist[i][j] = 0
else if there is an edge from i to j:
dist[i][j] = weight of the edge from i to j
else:
dist[i][j] = infinity
for each k from 1 to n:
for each i from 1 to n:
for each j from 1 to n:
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
The time complexity of the Floyd-Warshall algorithm is O(n^3), where n is the number of nodes in the graph. The space complexity is also O(n^3), since the algorithm requires a 2D matrix of size n x n to store the distances between all pairs of nodes.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.