0% found this document useful (0 votes)
6 views12 pages

AOA EXPT 8

The document outlines an experiment to implement the Floyd-Warshall algorithm for finding the shortest paths between all pairs of vertices in a weighted graph. It describes the algorithm's theory, steps, pseudocode, and provides a sample implementation in C. The algorithm operates with a time complexity of O(n³) and a space complexity of O(n²), making it efficient for dense graphs with a smaller number of vertices.

Uploaded by

ashcraftgamer8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

AOA EXPT 8

The document outlines an experiment to implement the Floyd-Warshall algorithm for finding the shortest paths between all pairs of vertices in a weighted graph. It describes the algorithm's theory, steps, pseudocode, and provides a sample implementation in C. The algorithm operates with a time complexity of O(n³) and a space complexity of O(n²), making it efficient for dense graphs with a smaller number of vertices.

Uploaded by

ashcraftgamer8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Experiment No.

To implement All pair shortest Path Algorithm


(Floyd Warshall Algorithm)

Date of Performance: 12/03/2025

Date of Submission: 19/03/2025


Experiment No. 8

Title: All Pair Shortest Path


Aim: To study and implement All Pair Shortest Path Algorithm
Objective: To introduce dynamic programming-based algorithm

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:

Floyd-Warshall(w, n){ // w: weights, n: number of vertices


for i = 1 to n do // initialize, D (0) = [wij]
for j = 1 to n do{
d[i, j] = w[i, j];
}
for k = 1 to n do // Compute D (k) from D (k-1)
for i = 1 to n do
for j = 1 to n do
if (d[i, k] + d[k, j] < d[i, j]){
d[i, j] = d[i, k] + d[k, j];
}
return d[1..n, 1..n];
}

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].

Time Complexity Analysis:

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>

#define INF 99999999


#define V 4

void printMatrix(int dist[V][V]) {


for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
printf("INF ");
else
printf("%3d ", dist[i][j]);
}
printf("\n");
}
printf("\n");
}

void floydWarshall(int graph[V][V]) {


int dist[V][V];

for (int i = 0; i < V; i++) {


for (int j = 0; j < V; j++) {
if (graph[i][j] == 0 && i != j)
dist[i][j] = INF;
else
dist[i][j] = graph[i][j];
}
}

for (int k = 0; k < V; k++) {


printf("Iteration %d:\n", k + 1);

for (int i = 0; i < V; i++) {


for (int j = 0; j < V; j++) {
if (dist[i][k] != INF && dist[k][j] != INF && dist[i][k] + dist[k][j] <
dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy