Floyd's Shortest Path Algorithm

In this algorithm, W is the adjacency matrix for the graph, and D is an array of the same size as the adjacency matrix.

void floyd (int n, int W [][], int D[][])
{
    D = W;

    for (k = 0; k < n; k++)
        for (i = 0; i < n; i++)
            for (j = 0; j < n; j++)
                D[i][j] = min (D[i][j], D[i][k] + D[k][j]);
}

 

 

Modification to Floyd's Shortest Path Algorithm

void floyd2 (int n, int W [][], int D[][], int P[][])
{
    D = W;

    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            P[i][j] = 0;

    for (k = 0; k < n; k++)
        for (i = 0; i < n; i++)
            for (j = 0; j < n; j++)
                if (D[i][k] + D[k][j] < D[i][j])
                {
                    P[i][j] = k + 1;
                    D[i][j] = D[i][k] + D[k][j];
                }
}