0% found this document useful (0 votes)
4 views4 pages

3a FloydWarshall

The document contains a C program that implements the Floyd-Warshall algorithm to solve the All-Pairs Shortest Paths problem. It initializes a distance matrix from a given graph and updates it to find the shortest paths between all pairs of vertices. The program outputs the shortest distances in a formatted matrix.
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)
4 views4 pages

3a FloydWarshall

The document contains a C program that implements the Floyd-Warshall algorithm to solve the All-Pairs Shortest Paths problem. It initializes a distance matrix from a given graph and updates it to find the shortest paths between all pairs of vertices. The program outputs the shortest distances in a formatted matrix.
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/ 4

C:\Users\jayap\Desktop\2025-2025-Even Semester\BCS401-Analysis and Design of Algorithms\404 Lab Programs\03 Prg\3a-FloydWarshall.

c Friday, 18 April, 2025 11:46 AM

/* 3(a). 3 a) Design and implement C/C++ Program to solve All-Pairs


Shortest Paths problem using Floyd's algorithm.
*/
#include <stdio.h>

#define V 4
#define INF 99999 // Represents infinity

// Function to print the final shortest distance matrix


void printSolution(int dist[V][V])
{
int i, j;
printf("Shortest distances between every pair of vertices:\n");
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
if (dist[i][j] == INF)
printf("INF\t");
else
printf("%d\t", dist[i][j]);
}
printf("\n");
}
}

// Floyd-Warshall algorithm
-1-
C:\Users\jayap\Desktop\2025-2025-Even Semester\BCS401-Analysis and Design of Algorithms\404 Lab Programs\03 Prg\3a-FloydWarshall.c Friday, 18 April, 2025 11:46 AM

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


{
int dist[V][V];
int i, j, k;

// Step 1: Initialize distance matrix with input graph


for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
dist[i][j] = graph[i][j];
}
}

// Step 2: Update the shortest paths


for (k = 0; k < V; k++)
{
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
if (dist[i][k] + dist[k][j] < dist[i][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
-2-
C:\Users\jayap\Desktop\2025-2025-Even Semester\BCS401-Analysis and Design of Algorithms\404 Lab Programs\03 Prg\3a-FloydWarshall.c Friday, 18 April, 2025 11:46 AM

// Step 3: Print the result


printSolution(dist);
}

int main()
{
// Example graph represented as adjacency matrix
int graph[V][V] = {
{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}
};

floydWarshall(graph);
return 0;
}

/* OUTPUT

Shortest distances between every pair of vertices:


0 3 7 5
2 0 6 4
3 1 0 5
5 3 2 0
-3-
C:\Users\jayap\Desktop\2025-2025-Even Semester\BCS401-Analysis and Design of Algorithms\404 Lab Programs\03 Prg\3a-FloydWarshall.c Friday, 18 April, 2025 11:46 AM

*/

-4-

You might also like