The Floyd-Warshall algorithm calculates the distances between all pairs of vertices in a weighted graph. It is a dynamic programming algorithm very similar to Gauss-Jordan elimination.

Below is an implementation in C. The function takes an array of directed arcs, the size of the graph (number of arcs), and its order (number of vertices). It returns a dynamically allocated 2-dimensional array that is the distance table for all pairs of vertices.

One thing to notice about the implementation is that when you use `INT_MAX`

to represent infinity, you need to be very careful not to do arithmetic on it that will cause it to roll over and become a small number. This is the reason for the checks for `INT_MAX`

in the main calculation.

#include <stdlib.h> #include <limits.h> typedef struct { unsigned int first; unsigned int second; int weight; } weighted_arc; int **floyd_warshall(const weighted_arc *arcs, unsigned int size, unsigned int order) { unsigned int i, j, k; int **distances = malloc(order * sizeof(int *)); /* Initialise the distance table */ for (i = 0; i < order; i++) { distances[i] = malloc(order * sizeof(int)); for (j = 0; j < order; j++) { if (i == j) { distances[i][j] = 0; } else { distances[i][j] = INT_MAX; } } } /* Add the distance for each arc */ for (i = 0; i < size; i++) { distances[arcs[i].first][arcs[i].second] = arcs[i].weight; } /* Calculate the rest of the distances */ for (i = 0; i < order; i++) { for (j = 0; j < order; j++) { for (k = 0; k < order; k++) { const int djk = distances[j][k]; const int dji = distances[j][i]; const int dik = distances[i][k]; if (dji != INT_MAX && dik != INT_MAX && djk > dji + dik) { distances[j][k] = dji + dik; } } } } return distances; }

Here is an example program that calculates the distance table for the graph shown at the top of the page:

#include <stdio.h> #include <stdlib.h> /* Connect two arcs */ void weighted_arc_connect(weighted_arc *arcs, unsigned int first, unsigned int second, int weight, unsigned int *pos) { arcs[*pos].first = first; arcs[*pos].second = second; arcs[*pos].weight = weight; (*pos)++; } /* Print a distance table */ void print_distances(int **distances, unsigned int order) { unsigned int i, j; for (i = 0; i < order; i++) { for (j = 0; j < order; j++) { printf("%d ", distances[i][j]); } putchar('\n'); } } int main(void) { unsigned int size = 5; /* Arcs */ unsigned int order = 4; /* Vertices */ unsigned int i = 0; int **distances; weighted_arc *arcs = malloc(size * sizeof(weighted_arc)); weighted_arc_connect(arcs, 0, 2, -2, &i); weighted_arc_connect(arcs, 1, 0, 4, &i); weighted_arc_connect(arcs, 1, 2, 3, &i); weighted_arc_connect(arcs, 2, 3, 2, &i); weighted_arc_connect(arcs, 3, 1, -1, &i); distances = floyd_warshall(arcs, size, order); print_distances(distances, order); free(arcs); for (i = 0; i < order; i++) { free(distances[i]); } free(distances); return 0; }

The output:

0 -1 -2 0 4 0 2 4 5 1 0 2 3 -1 1 0