0% found this document useful (0 votes)
9 views1 page

Floyd Warshall

The document contains a C program that implements the Floyd-Warshall algorithm to find the shortest paths between all pairs of vertices in a graph represented by an adjacency matrix. It prompts the user to input the number of vertices and the adjacency matrix, then calculates and displays the shortest distances. The program uses a predefined constant to represent 'infinity' for unreachable paths.

Uploaded by

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

Floyd Warshall

The document contains a C program that implements the Floyd-Warshall algorithm to find the shortest paths between all pairs of vertices in a graph represented by an adjacency matrix. It prompts the user to input the number of vertices and the adjacency matrix, then calculates and displays the shortest distances. The program uses a predefined constant to represent 'infinity' for unreachable paths.

Uploaded by

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

#include <stdio.

h>

#define INF 1000000 // A large number to represent "infinity"


#define MAX 100

int main() {
int V;
int graph[MAX][MAX];

printf("Enter number of vertices: ");


scanf("%d", &V);

printf("Enter the adjacency matrix (use %d for INF):\n", INF);


for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
scanf("%d", &graph[i][j]);
}
}

// Floyd-Warshall algorithm
int dist[MAX][MAX];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph[i][j];

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


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

// Output the result


printf("\nShortest distances between every pair of vertices:\n");
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");
}

return 0;
}

You might also like