MC4101 - ADVANCED DATA STRUCTURES AND ALGORITHMS
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the
pairs of vertices in a weighted graph. This algorithm works for both the directed and undirected
weighted graphs. But, it does not work for the graphs with negative cycles (where the sum of the
edges in a cycle is negative).
A weighted graph is a graph in which each edge has a numerical value associated with it.
Floyd-Warhshall algorithm is also called as Floyd's algorithm, Roy-Floyd algorithm,
Roy-Warshall algorithm, or WFI algorithm.
This algorithm follows the dynamic programming approach to find the shortest paths.
How Floyd-Warshall Algorithm Works?
Let the given graph be:
Follow the steps below to find the shortest path between all the pairs of vertices.
1. Create a matrix A0 of dimension n*n where n is the number of vertices. The row and the
column are indexed as i and j respectively. i and j are the vertices of the graph.
Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If there is no path
from ith vertex to jth vertex, the cell is left as infinity.
2. Now, create a matrix A1 using matrix A0. The elements in the first column and the first
row are left as they are. The remaining cells are filled in the following way.
K. KULUNTHAN – SMIT MCA 12
MC4101 - ADVANCED DATA STRUCTURES AND ALGORITHMS
Let k be the intermediate vertex in the shortest path from source to destination. In this step, k is
the first vertex. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k][j]).
That is, if the direct distance from the source to the destination is greater than the path through
the vertex k, then the cell is filled with A[i][k] + A[k][j].
In this step, k is vertex 1. We calculate the distance from source vertex to destination vertex
through this vertex k.
For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the
sum of the distance from vertex 2 to 4 through vertex (ie. from vertex 2 to 1 and from vertex 1 to
4) is 7. Since 4 < 7, A0[2, 4] is filled with 4.
3. Similarly, A2 is created using A1. The elements in the second column and the second row
are left as they are.
In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as in step 2.
4. Similarly, A3 and A4 is also created.
5. A4 gives the shortest path between each pair of vertices.
K. KULUNTHAN – SMIT MCA 13
MC4101 - ADVANCED DATA STRUCTURES AND ALGORITHMS
Algorithm
n = no of vertices
A = matrix of dimension n*n
for k = 1 to n
for i = 1 to n
for j = 1 to n
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
return A
// Floyd-Warshall Algorithm program in C++
#include <iostream>
using namespace std;
// defining the number of vertices
#define nV 4
#define INF 999
void printMatrix(int matrix[][nV]);
// Implementing floyd warshall algorithm
void floydWarshall(int graph[][nV]) {
int matrix[nV][nV], i, j, k;
for (i = 0; i < nV; i++)
for (j = 0; j < nV; j++)
matrix[i][j] = graph[i][j];
// Adding vertices individually
for (k = 0; k < nV; k++) {
for (i = 0; i < nV; i++) {
for (j = 0; j < nV; j++) {
K. KULUNTHAN – SMIT MCA 14
MC4101 - ADVANCED DATA STRUCTURES AND ALGORITHMS
if (matrix[i][k] + matrix[k][j] < matrix[i][j])
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
printMatrix(matrix);
}
void printMatrix(int matrix[][nV]) {
for (int i = 0; i < nV; i++) {
for (int j = 0; j < nV; j++) {
if (matrix[i][j] == INF)
printf("%4s", "INF");
else
printf("%4d", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int graph[nV][nV] = {{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}};
floydWarshall(graph);
}
Time Complexity
There are three loops. Each loop has constant complexities. So, the time complexity of the
Floyd-Warshall algorithm is O(n3).
Space Complexity
K. KULUNTHAN – SMIT MCA 15
MC4101 - ADVANCED DATA STRUCTURES AND ALGORITHMS
The space complexity of the Floyd-Warshall algorithm is O(n2).
Applications
● To find the shortest path of a directed graph
● To find the transitive closure of directed graphs
● To find the Inversion of real matrices
● For testing whether an undirected graph is bipartite
Dijkstra's Algorithm
It differs from the minimum spanning tree because the shortest distance between two vertices
might not include all the vertices of the graph.
How Dijkstra's Algorithm works
Dijkstra's Algorithm works on the basis that any subpath B -> D of the shortest path A -> D
between vertices A and D is also the shortest path between vertices B and D.
Djikstra used this property in the opposite direction i.e we overestimate the distance of each
vertex from the starting vertex. Then we visit each node and its neighbors to find the shortest
subpath to those neighbors.
The algorithm uses a greedy approach in the sense that we find the next best solution hoping that
the end result is the best solution for the whole problem.
Example of Dijkstra's algorithm
It is easier to start with an example and then think about the algorithm.
Step 1: Start with a weighted graph
Step 2 : Choose a starting vertex and assign infinity path values to all other devices
K. KULUNTHAN – SMIT MCA 16
MC4101 - ADVANCED DATA STRUCTURES AND ALGORITHMS
Step 3: Go to each vertex and update its path length
Step 4: If the path length of the adjacent vertex is
lesser than new path length, don't update it
Step 5 : Avoid updating path lengths of already visited
vertices
Step 6 : After each iteration, we pick the unvisited vertex with the least path length. So we
choose 5 before 7
Step 7 : After each iteration, we pick the unvisited vertex with the least path length. So we
choose 5 before 7
K. KULUNTHAN – SMIT MCA 17
MC4101 - ADVANCED DATA STRUCTURES AND ALGORITHMS
Step 8: Repeat until all the vertices have been visited
Djikstra's algorithm pseudocode
function dijkstra(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
distance[S] <- 0
while Q IS NOT EMPTY
U <- Extract MIN from Q
for each unvisited neighbour V of U
tempDistance <- distance[U] + edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
return distance[], previous[]
Time Complexity: O(E Log V), where, E is the number of edges and V is the number of
vertices.
Space Complexity: O(V)
K. KULUNTHAN – SMIT MCA 18
MC4101 - ADVANCED DATA STRUCTURES AND ALGORITHMS
Dijkstra's Algorithm Applications
● To find the shortest path
● In social networking applications
● In a telephone network
● To find the locations in the map
K. KULUNTHAN – SMIT MCA 19