AOA EXPT 8
AOA EXPT 8
Theory: The Floyd-Warshall algorithm is a graph algorithm that is deployed to find the
shortest path between all the vertices present in a weighted graph. This algorithm is
different from other shortest path algorithms; to describe it simply, this algorithm uses
each vertex in the graph as a pivot to check if it provides the shortest way to travel from
one point to another.
Floyd-Warshall algorithm is one of the methods in All-pairs shortest path algorithms and
it is solved using the Adjacency Matrix representation of graphs.
Floyd-Warshall Algorithm
Consider a graph, G = {V, E} where V is the set of all vertices present in the graph and E
is the set of all the edges in the graph. The graph, G, is represented in the form of an
adjacency matrix, A, that contains all the weights of every edge connecting two vertices.
Algorithm:
1. Construct an adjacency matrix A with all the costs of edges present
in the graph. If there is no path between two vertices, mark
the value as ∞.
2. Derive another adjacency matrix A1 from A keeping the first row and first column
of the original adjacency matrix intact in A1. And for the remaining values,
say A1[i,j], if A[i,j]>A[i,k]+A[k,j] then replace A1[i,j] with A[i,k]+A[k,j].
Otherwise, do not change the values. Here, in this step, k = 1 (first vertex acting as
pivot).
3. Repeat Step 2 for all the vertices in the graph by changing the k value for every
pivot vertex until the final matrix is achieved.
4. The final adjacency matrix obtained is the final solution with all the shortest paths.
Pseudocode:
Example:
Consider the following directed weighted graph G = {V, E}. Find the shortest paths
between all the vertices of the graphs using the Floyd-Warshall algorithm.
Step 1: Construct an adjacency matrix A with all the distances as values.
Step 2: Considering the above adjacency matrix as the input, derive another matrix A0 by
keeping only first rows and columns intact. Take k = 1, and replace all the other values
by A[i,k]+A[k,j].
Step 3:
Considering the above adjacency matrix as the input, derive another matrix A0 by keeping
only first rows and columns intact. Take k = 1, and replace all the other values
by A[i,k]+A[k,j].
Step 4: Considering the above adjacency matrix as the input, derive another matrix A0 by
keeping only first rows and columns intact. Take k = 1, and replace all the other values
by A[i,k]+A[k,j].
Step 5: Considering the above adjacency matrix as the input, derive another matrix A0 by
keeping only first rows and columns intact. Take k = 1, and replace all the other values
by A[i,k]+A[k,j].
Step 6: Considering the above adjacency matrix as the input, derive another matrix A0 by
keeping only first rows and columns intact. Take k = 1, and replace all the other values
by A[i,k]+A[k,j].
The algorithm uses three for loops to find the shortest distance between all pairs of
vertices within a graph. Therefore, the time complexity is O(n3), where ‘n’ is the number
of vertices in the graph. The space complexity of the algorithm is O(n2).
Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)
Program:
#include <stdio.h>
printMatrix(dist);
}
}
int main() {
int graph[V][V] = {
{0, 3, INF, 7},
{8, 0, 2, INF},
{5, 7, 0, 1},
{2, INF, INF, 0}
};
printf("Initial graph matrix:\n");
printMatrix(graph);
floydWarshall(graph);
return 0;
}
Output:
Conclusion:
The Floyd-Warshall algorithm is a powerful dynamic programming approach to solve
the All-Pairs Shortest Path problem in a weighted graph. It works by iteratively
updating the adjacency matrix, using each vertex as an intermediate pivot to check if a
shorter path exists between any two vertices. The algorithm runs in cubic time, O(n³),
and uses O(n²) space, making it efficient for dense graphs. This method guarantees that
after the completion of all iterations, the final matrix contains the shortest paths
between all pairs of vertices. It is particularly useful for graphs with a smaller number
of vertices, where computing all-pairs shortest paths is necessary.