# Floyd Warshall Algorithm

Jun 10, 2021, 11:11:27 AM Tech and Science

In this post, we'll look at what the Floyd Warshall Algorithm is and how it works in the field of Dynamic Programming.

What is the Floyd Warshall Algorithm, and how does it work?

The Floyd Warshall algorithm, like Dijkstra's, is used to discover the shortest path between all vertices in a weighted graph. This approach works with both directed and undirected graphs, but not with graphs that have negative cycles. As a result, if the distance between the vertex v and itself is negative, we can conclude the graph has a negative cycle. The dynamic programming methodology is used to implement this technique. The algorithm does not construct the path in this case, but it may reconstruct it with a simple change. Floyd Warshall algorithm (also known as Roy Warshall algorithm or Roy-Floyd algorithm) is a type of algorithm developed by Floyd Warshall. Let's look at how the Floyd Warshall algorithm works.

The Algorithm

We create the D matrix, which contains the length of the shortest path between any two nodes.

The algorithm sets D to L, or the direct distances between nodes, as the starting point. Following n iterations, D returns the length of the shortest pathways that exclusively use nodes in 1,2,...k as intermediate nodes.

D thus offers the length of shortest pathways utilising any of the nodes in N as an intermediary node after n repetitions. Dk can be implemented if it represents the matrix D after the kth iteration.

Dk [i , j] = min (Dk-1 [i , j], Dk-1 [i, k] + Dk-1 [k , j])

To calculate the length from I to j passing through k, we employ the principle of optimality.

Complexity of Time

The shortest path in the graph is computed using three loops, each of which has a constant complexity. As a result, the Floyd Warshall algorithm has a time complexity of O(n3). Furthermore, the Floyd Warshall algorithm has a space complexity O(n2).

Floyd Warshall Algorithm Application

The Floyd Warshall Algorithm is useful for determining the inversion of real matrices.

It can be used to see if an undirected graph is bipartite.

It aids in the discovery of the shortest path in a directed graph.

Different variations of the Floyd Warshall algorithm aid in the discovery of a directed graph's transitive closure.

Written by Mark Henry