Skip to main content

Floyd Warshall Algorithm

CODE

#include <stdio.h>
#define INF 99999
#define V 4

void path(int graph[V][V])
{
int dist[V][V];

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] != INF && dist[k][j] != INF &&
dist[i][j] > dist[i][k] + dist[k][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}

for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][j] == INF)
{
printf("INF ");
}
else
{
printf("%d ", dist[i][j]);
}
}
printf("\n");
}
}

int main()
{
int graph[V][V] = {
{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}};

path(graph);

return 0;
}