******** fig10.53 ********** /* Compute All-Shortest Paths */ /* A[] contains the adjacency matrix with A[i][i] presumed to be zero */ /* D[] contains the values of the shortest path */ /* |V| is the number of vertices */ /* A negative cycle exists iff d[i][j] is set to a negative value at line 9 */ /* Actual path can be computed via another procedure using path[] */ /* All arrays are indexed starting at 0 */ void all_pairs( two_d_array A, two_d_array D, two_d_array path ) { int i, j, k; /*1*/ for( i=0; i < |V|; i++ ) /* Initialize D and path */ /*2*/ for( j=0; j < |V|; j++ ) { /*3*/ D[i][j] = A[i][j]; /*4*/ path[i][j] = NOT_A_VERTEX; } /*5*/ for( k=0; k < |V|; k++ ) /* Consider each vertex as an intermediate */ /*6*/ for( i=0; i < |V|; i++ ) /*7*/ for( j=0; j < |V|; j ++ ) /*8*/ if( d[i][k] + d[k][j] < d[i][j] ) /*update min */ { /*9*/ d[i][j] = d[i][k] + d[k][j]; /*10*/ path[i][j] = k; } }