Professional Documents
Culture Documents
YEAR: - 2NDYEAR
Introduction:
The All-Pair Shortest Path (APSP) problem is a fundamental problem in graph theory and
computer science. It involves finding the shortest paths between all pairs of vertices in a
weighted graph. One of the classic algorithms to solve this problem is the Floyd Warshall
Algorithm. In this report, we delve into the intricacies of the Floyd Warshall Algorithm, its
implementation, and its applications.
Suppose we have a graph G[][] with V vertices from 1 to N. Now we have to evaluate a
shortestPathMatrix [][] where shortestPathMatrix[i][j] represents the shortest path between
vertices i and j.
Obviously, the shortest path between i to j will have some k number of intermediate nodes.
The idea behind Floyd warshall algorithm is to treat each and every vertex from 1 to N as an
intermediate node one by one.
1. Initialize the solution matrix same as the input graph matrix as a first step.
2. Then update the solution matrix by considering all vertices as an intermediate vertex.
3. The idea is to pick all vertices one by one and updates all shortest paths which include
the picked vertex as an intermediate vertex in the shortest path.
4. When we pick vertex number k as an intermediate vertex, we already have considered
vertices {0, 1, 2, ... k-1} as intermediate vertices.
5. For every pair (i, j) of the source and destination vertices respectively, there are two
possible cases.
6. k is not an intermediate vertex in shortest path from i to j. We keep the value of
dist[i][j] as it is.
7. k is an intermediate vertex in shortest path from i to j. We update the value of dist[i][j]
as dist[i][k] + dist[k][j], if dist[i][j] > dist[i][k] + dist[k][j].
Advantages of the Floyd-Warshall algorithm:
1. All-Pairs Shortest Path: The Floyd-Warshall algorithm finds the shortest paths
between all pairs of vertices in a graph. This means that once the algorithm has been
executed, you can quickly determine the shortest path between any two vertices in the
graph.
Time Complexity:
The time complexity of the Floyd-Warshall algorithm is O(n3), where n is the number
of vertices in the graph.
This cubic time complexity arises because the algorithm considers all possible pairs of
vertices and all possible intermediate vertices to update the shortest path distances.
Despite its cubic time complexity, the Floyd-Warshall algorithm is often practical for
small to moderate-sized graphs and can be efficient in practice due to its simplicity
and matrix-based approach.
Space Complexity:
In summary, while the Floyd-Warshall algorithm has a cubic time complexity, it offers
quadratic space complexity, making it practical for a wide range of graph sizes and suitable
for scenarios where memory efficiency is important.
Conclusion:
The Floyd Warshall algorithm efficiently computes the shortest paths between all pairs of
vertices in a graph. It is a versatile algorithm that can handle graphs with both positive and
negative edge weights, and it is suitable for dense graphs where other algorithms like
Dijkstra’s may be less efficient. The provided C implementation demonstrates the simplicity
and effectiveness of the algorithm. The Floyd-Warshall algorithm's advantages make it an
indispensable tool for solving complex shortest path problems across multiple domains. Its
efficiency, versatility, and reliability contribute to its enduring relevance in both theoretical
studies and practical applications.
References:
https://www.gatevidyalay.com/floyd-warshall-algorithm-shortest-path-algorithm/
https://blog.devgenius.io/floyd-warshall-algorithm-f004a01ae40e
https://www.geeksforgeeks.org/floyd-warshall-algorithm-dp-16/