EX.9: FLOYEDS ALGORITHM.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#define MAX 10
#define INF 9999
void floydWarshall(int graph[MAX][MAX], int n) {
int dist[MAX][MAX];
int i, j, k
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
dist[i][j] = graph[i][j];
}
}
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
printf("\nShortest distances between every pair of vertices:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (dist[i][j] == INF)
printf("%4s", "INF");
else
printf("%4d", dist[i][j]);
}
printf("\n");
}
}
void main() {
int graph[MAX][MAX], n, i, j;
clrscr();
printf("Enter the number of vertices (max %d): ", MAX);
scanf("%d", &n);
printf("Enter the adjacency matrix (use %d for INF):\n", INF);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
}
}
floydWarshall(graph, n);
getch();
}
OUTPUT:
Enter the number of vertices (max 10): 4
Enter the adjacency matrix (use 9999 for INF):
0 5 9999 10
9999 0 3 9999
9999 9999 0 1
7 9999 9999 0
Shortest distances between every pair of vertices:
0 5 8 9
16 0 3 4
8 13 0 1
7 12 15 0