// Floyd-Warshall algorithm // // Author: Rahul Simha // Date: Oct, 2001. public class FloydWarshall { // Debugging for internal use. private static final boolean debug = false; int numVertices; // Number of vertices (when initialized). double[][] adjMatrix; // The adjacency matrix (given as input). double[][] Dk, Dk_minus_one; // Matrices used in dynamic programming. public void initialize (int numVertices) { this.numVertices = numVertices; // Initialize Dk matrices. Dk = new double [numVertices][]; Dk_minus_one = new double [numVertices][]; for (int i=0; i < numVertices; i++){ Dk[i] = new double [numVertices]; Dk_minus_one[i] = new double [numVertices]; } } public void allPairsShortestPaths (double[][] adjMatrix) { // Dk_minus_one = weights when k = -1 for (int i=0; i 0) Dk_minus_one[i][j] = adjMatrix[i][j]; else Dk_minus_one[i][j] = Double.MAX_VALUE; // NOTE: we have set the value to infinity and will exploit // this to avoid a comparison. } } // Now iterate over k. for (int k=0; k