Floyd's All Pairs Shortest Path Algorithm

Introduction

Graph algorithms work on nodes and edges. Nodes and edges are represented in many ways. This well-known algorithm uses a connectivity matrix. The matrix is a square matrix with number of columns and rows same as many as there are Nodes. The diagonal elements are set to zero. The offdiagonals are 1 if connected 0 if not connected. (You could also use distances if you are dealing with something like road distances.) This matrix is modified to make the cost of the disconnected nodes to infinity. You can make slight modifications so that the inputs take STL vector< vector<int> > as a matrix data structure. You could drop the lower half of the matrix; you can also consider using the upper half for the original data and using the lower half for the distance matrix and save entirely on an extra copy of the matrix.

Since the earlier version, I have added code that stores the nodes in the path in addition to the length, so that the actual paths are also computed in addition to the path lengths, thus doing what Djikstra's algorithm was primarily meant to do, as well.

The output of the algorithm is a matrix of the shortest distances between every pair of points and a comma-separated list of nodes representing the paths.

infinity

Well, it looks like infinity, bot it is not, and quite easily happen that a path is longer than the value above.

One solution:
-------------

*(A+i*n+j) = (unsigned int)(-1);

which gives the variable the greatest possible value of type "int".

Other solution:
---------------

#include <limits.h>

*(A+i*n+j) = INT_MAX;

- or -

*(A+i*n+j) = LONG_LONG_MAX;

depending on which type of values you are working with. Here, in your code, INT_MAX is acceptable since A is declared as "int *". For an Alpha processor, LONG_LONG_MAX is the greates possible value, and it is 64 bit long.
"limits.h" is a part of ANSI Standard C and can be found in the "include" directory.