I am trying to implement the Floyd-Warshall algorithm in Java without using the “three looped” method, but I cannot figure out where I made a mistake in the code.
This is a map that shows how my peaks are connected. White numbers are vertices, and black numbers are distances between connected vertices.
Peaks Map: http://i.imgur.com/htcaA4y.png
After starting iterations, I get the following final matrixes of distances and sequences. What says "something is wrong" is column 8 on the last matrix of the sequence (the one on the right). To get to vertex 8 from any other vertex, the path must first go from vertex 8 to 9, and THEN to 10 (which does not correspond to the matrix - it goes straight from vertex 8 to 10).
Output Matrix: http://i.imgur.com/o6fQweH.png
Here is the code. What is the problem?
import java.util.ArrayList;
public class Main_Simple {
public static void main(String[] args) {
int inf = 1000000;
int[][] initialDistanceMatrix = {
{0, 1, inf, 1, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf},
{1, 0, 1, inf, 1, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf},
{inf, 1, 0, inf, inf, 1, inf, inf, inf, inf, inf, inf, inf, inf, inf},
{1, inf, inf, 0, 1, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf},
{inf, 1, inf, 1, 0, 1, inf, inf, inf, inf, inf, inf, inf, inf, inf},
{inf, inf, 1, inf, 1, 0, 2, inf, inf, 1, inf, inf, inf, inf, inf},
{inf, inf, inf, inf, inf, 2, 0, inf, inf, inf, inf, inf, inf, inf, inf},
{inf, inf, inf, inf, inf, inf, inf, 0, 1, inf, inf, inf, inf, inf, inf},
{inf, inf, inf, inf, inf, inf, inf, 1, 0, 1, inf, inf, inf, inf, inf},
{inf, inf, inf, inf, inf, 1, inf, inf, 1, 0, 2, 1, inf, inf, inf},
{inf, inf, inf, inf, inf, inf, inf, inf, inf, 2, 0, inf, inf, inf, 2},
{inf, inf, inf, inf, inf, inf, inf, inf, inf, 1, inf, 0, 1, inf, inf},
{inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, 1, 0, 1, inf},
{inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, 1, 0, 1},
{inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, 2, inf, inf, 1, 0}
};
int[][] initialSequenceMatrix = new int[initialDistanceMatrix.length][initialDistanceMatrix.length];
for (int row = 0; row < initialSequenceMatrix.length; row++) {
for (int column = 0; column < initialSequenceMatrix.length; column++) {
if (row == column) {
initialSequenceMatrix[row][column] = 0;
} else {
initialSequenceMatrix[row][column] = column + 1;
}
}
}
ArrayList<int[][]> distanceMatrices = new ArrayList<int[][]>();
distanceMatrices.add(initialDistanceMatrix);
ArrayList<int[][]> sequenceMatrices = new ArrayList<int[][]>();
sequenceMatrices.add(initialSequenceMatrix);
printMatrix(initialDistanceMatrix, "Initial distance matrix");
printMatrix(initialSequenceMatrix, "Initial sequence matrix");
for (int iteration = 1; iteration < initialDistanceMatrix.length; iteration++) {
int[][] currentDistanceMatrix = new int[initialDistanceMatrix.length][initialDistanceMatrix.length];
for (int row = 0; row < currentDistanceMatrix.length; row++) {
for (int column = 0; column < currentDistanceMatrix.length; column++) {
currentDistanceMatrix[row][column] = 0;
}
}
for (int row = 0; row < currentDistanceMatrix.length; row++) {
for (int column = 0; column < currentDistanceMatrix.length; column++) {
if (row == column) {
currentDistanceMatrix[row][column] = 0;
} else if (row == (iteration - 1) || column == (iteration - 1)) {
currentDistanceMatrix[row][column] = distanceMatrices.get(iteration - 1)[row][column];
} else {
int Dij = distanceMatrices.get(iteration - 1)[row][column];
int Dik_Dkj = distanceMatrices.get(iteration - 1)[row][iteration - 1] + distanceMatrices.get(iteration - 1)[iteration - 1][column];
if (Dij > Dik_Dkj) currentDistanceMatrix[row][column] = Dik_Dkj;
else currentDistanceMatrix[row][column] = Dij;
}
}
}
distanceMatrices.add(currentDistanceMatrix);
int[][] currentSequenceMatrix = new int[initialDistanceMatrix.length][initialDistanceMatrix.length];
for (int row = 0; row < currentSequenceMatrix.length; row++) {
for (int column = 0; column < currentSequenceMatrix.length; column++) {
if (row == column) {
currentSequenceMatrix[row][column] = 0;
} else if (row == (iteration - 1) || column == (iteration - 1)) {
currentSequenceMatrix[row][column] = sequenceMatrices.get(iteration - 1)[row][column];
} else {
if (distanceMatrices.get(distanceMatrices.size() - 1)[row][column] == distanceMatrices.get(distanceMatrices.size() - 2)[row][column]) {
currentSequenceMatrix[row][column] = sequenceMatrices.get(sequenceMatrices.size() - 1)[row][column];
} else {
currentSequenceMatrix[row][column] = iteration;
}
}
}
}
sequenceMatrices.add(currentSequenceMatrix);
}
System.out.println("-------------------------------------------------------");
printMatrix(distanceMatrices.get(distanceMatrices.size() - 1), "Final Distance Matrix");
printMatrix(sequenceMatrices.get(sequenceMatrices.size() - 1), "Final Sequence Matrix");
}
public static void printMatrix(int[][] matrix, String message) {
System.out.println("\n" + message);
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix.length; column++) {
System.out.print(matrix[row][column] + "\t");
}
System.out.println();
}
System.out.println();
}
}