0% found this document useful (0 votes)
18 views3 pages

Alg 1

The document presents a C program implementing Floyd's algorithm to find the shortest paths between all pairs of vertices in a graph. It initializes a distance matrix from an adjacency matrix input by the user and updates it using the Floyd-Warshall algorithm. The program outputs the shortest distances or indicates infinity for unreachable paths.

Uploaded by

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

Alg 1

The document presents a C program implementing Floyd's algorithm to find the shortest paths between all pairs of vertices in a graph. It initializes a distance matrix from an adjacency matrix input by the user and updates it using the Floyd-Warshall algorithm. The program outputs the shortest distances or indicates infinity for unreachable paths.

Uploaded by

astrophileion369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like