Program
#include <stdio.h> #include <conio.h> void floyd(int [10][10], int); int min(int, int); void main() { int n, a[10][10], i, j; printf("****************************************************************"); printf("\n****************************************************************"); printf("\n** WAP to find shortest path between any two pairs of nodes **"); printf("\n** using Floyd Warshall's Algorithm in C **"); printf("\n** Created by Sheetal Garg **"); printf("\n** Assistant Professor **"); printf("\n** Phone No:9467863365 **"); printf("\n****************************************************************"); printf("\n****************************************************************"); printf("\nEnter the no.of nodes : "); scanf("%d", &n); printf("\nEnter the initial cost adjacency matrix\n"); for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) scanf("%d", &a[i][j]); floyd(a, n); } void floyd(int a[10][10], int n) { int d[10][10], i, j, k; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) d[i][j] = a[i][j]; for (k = 1; k <= n; k++) for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); printf("\nThe Shortest Path distance matrix is\n"); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) printf("%d\t", d[i][j]); printf("\n"); } } int min(int a, int b) { if (a < b) return a; else return b; }
Output
**************************************************************** **************************************************************** ** WAP to find shortest path between any two pairs of nodes ** ** using Floyd Warshall's Algorithm in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter the no.of nodes : 4 Enter the initial cost adjacency matrix 0 8 6 999 8 0 999 5 999 9 0 999 8 9 4 0 The Shortest Path distance matrix is 0 8 6 13 8 0 9 5 17 9 0 14 8 9 4 0