You are on page 1of 4

CA2 EXAMINATION (REPORT WRITING), MAR-2023

NAME: - AVIJIT GHOSH

DEPARTMENT: - ELECTRONICS AND COMMUNICATION


ENGINEERING

YEAR: - 2NDYEAR

SEMESTER: - 4TH SEM

SUBJECT: - DESIGN AND ANALYSIS OF ALGORITHM

SUBJECT CODE: - ES-CS401

TOPIC: - ALL PAIR SHORTEST PATH USING FLOYD WARSHALL


ALGORITHM

UNIVERSITY ROLL NO: - 17600322047

SUBJECT TEACHER: - SUMANTA DAW


Title: Report on All Pair Shortest Path using Floyd Warshall Algorithm

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.

Definition of Floyd Warshall Algorithm:


The Floyd Warshall Algorithm is a dynamic programming-based algorithm used to solve the
All-Pair Shortest Path problem. It works for both directed and undirected graphs, including
graphs with negative weight edges (as long as there are no negative weight cycles). The
algorithm efficiently computes the shortest distances between all pairs of vertices in a graph.

The Floyd-Warshall algorithm is an efficient solution to this problem, capable of handling


graphs with both positive and negative edge weights, as long as there are no negative cycles.

Idea Behind Floyd Warshall Algorithm:

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.

Floyd Warshall Algorithm:

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.

2. Ease of Implementation: The algorithm is relatively simple to implement compared


to some other algorithms for finding shortest paths, such as Bellman-Ford or
Dijkstra's algorithm. It uses straightforward nested loops, making it easy to
understand and implement.
3. Matrix Representation: The algorithm's matrix-based approach makes it suitable for
parallel computing and implementation in hardware. This means it can be optimized
for efficiency in systems that support parallel processing.

Overall, the Floyd-Warshall algorithm is advantageous due to its ability to efficiently


find shortest paths between all pairs of vertices in a graph, including those with
negative edge weights (under the condition of no negative cycles), its ease of
implementation, and its suitability for parallel computing and hardware
implementation.

Time and space complexities:

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:

 The space complexity of the Floyd-Warshall algorithm is O(n2), where n is the


number of vertices in the graph.
 Despite the cubic time complexity, the space complexity of the Floyd-Warshall
algorithm is quadratic, making it efficient in terms of memory usage, especially for
dense graphs.

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/

You might also like